curl -sL xkcd.com | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd.pdf

Save xkcd to a pdf with captions

Saves to a PDF with title and alt text of comic. As asked for on http://bbs.archlinux.org/viewtopic.php?id=91100 Change xkcd.com to dynamic.xkcd.com/comics/random for a random comic.

6
2010-03-03 03:41:31

These Might Interest You

  • Only need to install Image Magick package. Display a xkcd comic with its title and save it in /tmp directory If you prefer to view the newest xkcd, use this command: wget -q http://xkcd.com/ -O-| sed -n '/<img src="http:\/\/imgs.xkcd.com\/comics/{s/.*\(http:.*\)" t.*/\1/;p}' | awk '{system ("wget -q " $1 " -O- | display -title $(basename " $1") -write /tmp/$(basename " $1")");}'


    0
    wget -q http://dynamic.xkcd.com/comic/random/ -O-| sed -n '/<img src="http:\/\/imgs.xkcd.com\/comics/{s/.*\(http:.*\)" t.*/\1/;p}' | awk '{system ("wget -q " $1 " -O- | display -title $(basename " $1") -write /tmp/$(basename " $1")");}'
    laugg · 2009-12-09 13:41:25 1

  • -4
    a=`curl http://xkcd.com 2>/dev/null | grep -iE 'src=.*imgs.xkcd.com/comics/'`; b=`echo ${a#*src=\"}`; eog ${b%%\"*}
    JollyJumper · 2010-08-27 11:06:14 0
  • Add an alias to your .bashrc that allows you to issue the command xkcd to view (with gwenview) the newest xkcd comic... I know there are thousands of them out there but this one is at least replete with installer and also uses a more concise syntax... plus, gwenview shows you the downloading progress as it downloads the comic and gives you a more full featured viewing experience.


    -5
    echo alias xkcd="gwenview `w3m -dump http://xkcd.com/|grep png | awk '{print $5}'` 2> /dev/null" >> .bashrc
    GinoMan2440 · 2010-01-30 20:38:16 0
  • I wasn't sure how to display the image, so I thought I'd try xml for a different twist. Show Sample Output


    0
    curl -s 'xkcd.com' | awk -F\" '/^<img/{printf("<?xml version=\"1.0\"?>\n<xkcd>\n<item>\n <title>%s</title>\n <comment>%s</comment>\n <image>%s</image>\n</item>\n</xkcd>\n", $6, $4, $2)}'
    putnamhill · 2009-12-03 19:49:37 0
  • This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic. To get a random xkcd comic use the following: xkcdrandom() { wget -qO- http://dynamic.xkcd.com/comic/random | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash; } These are just a bit shorter than the ones eigthmillion wrote, however his version didn't work as expected on my laptop for some reason (I got the title-tag first), so these build a command which is executed by bash.


    0
    xkcd() { wget -qO- http://xkcd.com/ | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash ; }
    John_W · 2010-08-25 15:44:31 3
  • Get http://xkcd.com/now image, inverts colors (I prefer dark backgrounds) and set it as wallpaper. You can replace the "convert" part by "wget -q http://imgs.xkcd.com/comics/now.png -O /tmp/now.png" if you prefer light backgrounds. The DISPLAY variable is here to work as a cron job. Show Sample Output


    0
    convert http://imgs.xkcd.com/comics/now.png -negate /tmp/now.png ; DISPLAY=:0.0 awsetbg -c /tmp/now.png
    hybris · 2014-02-26 15:37:15 0

What Others Think

Error: convert: Non-conforming drawing primitive definition `s' @ draw.c/DrawImage/3123. Result: Alt text garbled
oshazard · 429 weeks and 1 day ago
convert: unable to read font `/usr/share/fonts/default/TrueType/arial.ttf'. convert: Non-conforming drawing primitive definition `m'.
SuperFly · 429 weeks and 1 day ago
convert: unable to read font `/usr/share/fonts/default/TrueType/arial.ttf'. if you got this, try this: add -font "helvetica" to the convert command e.g.: ...| convert -font "helvetica" png:- ... this: convert: Non-conforming drawing primitive definition `m'. is still coming up, but you'll see the text in the pdf
nbk1971 · 429 weeks and 1 day ago
nice, but didn't work - same problem as comments above
leovailati · 429 weeks and 1 day ago
I think I've fixed those problems please try it again. Remember it saves to a pdf file so you should have an "xkcd.pdf" in your local directory.
matthewbauer · 429 weeks ago
It worked aces for me; I was the first to give it a thumbs up. :P Something I've considered giving a go, but have yet to - converting it to vector and doubling (or tripling) in size before saving to pdf. Also it would be pretty sweet to name each pdf "comicNumber.pdf" rather than just xkcd.pdf. If I could do that, and successfully achieve the vector "zoom" effect, i'd totally make this a daily cron. :)
trench · 429 weeks ago
There is no need for the temp file. You can use convert on a URL. So the major thing might be IMG=`curl -sL xkcd.com| grep '<img [^>]*/><br/>'|awk -F\" '{print $2}'`;convert $IMG -gravity south -draw "text 0,0 \"$IMG\"" pdf:- > xkcd.pdf
houghi · 429 weeks ago
@trench Try this: number=100; for a in $(seq 1 $number); do curl -sL xkcd.com/$a | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > /tmp/$a.pdf; done; gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=xkcd.pdf $(seq 1 $number | sed -r 's/(.*)/\/tmp\/\1.pdf/'); rm /tmp/a /tmp/*.pdf
matthewbauer · 428 weeks and 6 days ago
@houghi The problem is it gives the URL of the xkcd comic not the alt text @trench Actually this is more what you want: number=100; mkdir xkcd; for a in $(seq 1 $number); do curl -sL xkcd.com/$a | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd/$a.pdf; done
matthewbauer · 428 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: