Got 20 minutes? WWW::xkcd is born!

Lately I've been having weird sleeping hours. I sleep for 5 hours here, then 2 hours there, I stay up till 4am. I know, it sounds like I'm back in school, but really, I'm not.

Yesterday at 1:50am I had the opportunity to actually go to sleep. Should I? Yes! Will I? Hmm... well... maybe I'll just watch another episode of a TV series and hack a bit.

I recently read the "about" page of xkcd and saw there is now (or has been for a while?) a proper JSON output for the comics. This means you don't have to scrape the website to get the comics. This also means you can get a lot of data right away that you might not necessarily get by scrapping. Definitely not as easy, at least.

I figured, "hey, why not?". I searched CPAN and saw only a few xkcd modules:
- WWW::XKCD::AsText: uses OhNoRobot to get the text of an xkcd comic. Cute, not what I was looking for though.
- WWW::Comic::Plugin::XKCD: by David Precious. This is from 2008 and scrapes the website.
- Bot::BasicBot::Pluggable::Module::XKCD: besides the long name, simply a plugin to a bot, and that one also scrapes.

Alright, I wonder if at the length of a TV series episode I'll be able to write this up. Hey, let's make it a bit more interesting. How about writing a both synchronous and asynchronous interface. Hmm.. how about allowing both in the same interface? Let's do it up!

So, in the course of about 20 minutes (including a few pauses to look at the TV), I was able to write:
- An interface to, using the JSON interface.
- Methods supporting callbacks for asynchronous operations, but not requiring.
- Tests for the whole she-bang.

It's already uploaded to CPAN and you can find it as WWW::xkcd. I've released three versions so far, each included a few fixes and clean ups. The second version actually stabilized the interface, allowing for both fetching of the metadata and the image itself, or only the metadata.

I honestly have no specific usage for this now, but it was a fun thing to do, and someone might somewhere sometime find it useful. If so, drop me a line, I'd like to know. :)

Damn, I love Perl.


I love that a comic strip provides a JSON interface! Fun stuff! That same type of "not falling asleep" is what has led to several of my modules, most recently Tie::Select.

Nice! Now when someone sends you an xkcd cartoon without the mouseover text, you can fetch it with a Perl one-liner!

perl -MWWW::xkcd -E 'say WWW::xkcd->new->fetch_metadata->{alt}'

And you ought to use 313 as the example in the man page!

Or at least its alt text in place of the "1;" :)

Leave a comment

About Sawyer X

user-pic Gots to do the bloggingz