A Peek Behind How an API Brought an XKCD Cartoon to Life

So far, Paul Hunkin's bot has bought him two sets of camera batteries, a gift bag, a USB extension cord, a 1955 stamped envelope and a smattering of other items. Hunkin, you see, decided to bring an XKCD cartoon to life by programming a bot that automatically purchases cheap items using a New Zealand auction site's Trade Me API... because packages are exciting.

Hunkin explains the inner workings of the bot and the idea behind it on his blog. "I happened to stumble across this old comic from XKCD," he writes. "Google didn’t reveal anyone doing this already, and after asking a few people, the general opinion was that doing this was a terrible idea that would inevitably end badly. So obviously I had to do it."

Using the TradeMe API and writing using Python and python-oauth2, the script runs once a day and has a one in three chance of buying an item. The script gains $1 every day, so it can slowly accumulate savings and purchase "some (slightly) more expensive items". Hunkin explained the method he uses to select items:

  • It has a bunch of top-level categories it looks in.
  • For each of these categories, it searches for the term “Free shipping”, specifying both pay-now and buy-now, sorting by newest listings, with a maximum of 100 items returned per category.
  • For each of these items, it filters on buy-now price. It tries to spend at least 50% of its savings.
  • For each of the surviving items, it looks up the individual auction details to find its shipping information so it can filter on free shipping. Despite searching for the term ‘free shipping’ to start, only a small number of items have this.
  • At this point I have a list of items that match the price requirements, and can be bought with a credit card buy-now.
  • I then sort this list by ‘rarity’ – doing a search for the item title, and finding the item that returns theleast results. As the objective here is to buy strange and esoteric things, rarity is preferred.
  • Finally I buy the rarest item and subtract its cost from the bots savings.

After receiving his second set of watch batteries, Hunkin made some improvments, including automatic bidding on auctions, hourly increments to its spending limit and a "limited understanding of shipping information". In addition, it checks to make sure it hasn't bought the same item before.

Even a cursory look at the comments on Hacker News show the potential for automated auction-site scripts. From automatically purchasing and selling items on margin to a counter-bot that submits cheap items for Hunkin's bot to buy.

To keep up with Hunkin's bot and it's semi-daily purchases, you can follow it on Twitter, Facebook and on his blog.

Be sure to read the next eCommerce article: Google Base Axed in Favor of New Shopping APIs