Barrel - Part 1

We like to post xkcd comics here. So I decided to make a script that automatically creates markdown for a comic. Try it online here!

Teh Code and other details

import urllib.request as request
import json

print('This is "XKCD comic markdownifier for Stack Exchange."')
print('(Note that I\'m not affiliated with XKCD or Stack Exchange in anyway.)')
print('The posting of the code is at http://meta.stackexchange.com/q/263514/177681.')
print('This code will format an XKCD comic so it may be posted to Stack Exchange.')
num = input('What is the number of the comic?')

req = request.urlopen('https://xkcd.com/%s/info.0.json' % num)

str_response = req.read()

obj = json.loads(str_response)

print('[![%(title)s](%(img)s "%(alt)s")](https://www.xkcd.com/%(num)s)' % obj)

The current format is [![Title](imageUrl "Title Text")](comicUrl)].

This for example, when given 1, outputs [![Barrel - Part 1](https://imgs.xkcd.com/comics/barrel_cropped_(1).jpg "Don't we all.")](https://www.xkcd.com/1).

You can fork it here.

At the moment, you need to manually escape parentheses and such in the markdown. Luckily, this is usually unnecessary. Also, when run on bad input, it will through a syntax error (somehow.)

What do you guys think? Is it a good format? If you have any modifications, feel free to post it (although make sure it works as a runnable snippet. trinket.io is finky). String escaping would be nice. Error checking would also be nice.

  • @Daniel On what input? It has no error checking, so it won't work on bad input. (I only knew python 2, but trinket.io uses python 3. urllib happens to be a library that underwent a lot of changes.) Commented Aug 12, 2015 at 4:48
  • @Daniel (When you posted that, I fired up my laptop to check.) Commented Aug 12, 2015 at 4:53
  • 4
    how can I remember the number of the comic? If I have to google it, I think the traditional method is quicker.
    – Ooker
    Commented Aug 12, 2015 at 5:49
  • @Ooker But then you have to get the image url and copy the title text and remember image markdown. Commented Aug 12, 2015 at 12:20

1 Answer 1


Javascript version

Well... jQuery:

    url: "http://dynamic.xkcd.com/api-0/jsonp/comic/"+$(location).attr('href').split('/')[3],
    dataType: "json",
    success: function(data) {
        var title = data.title,
            img = data.img.replace(/(?=[()])/g, '\\'), //https://stackoverflow.com/a/22872990/3541881
            alt = data.alt,
            num = data.num;        
        window.prompt('Ctrl/Cmd+C the following:', '[!['+title+']('+img+' "'+alt+'")](http://xkcd.com/'+num+')');

It will prompt you with the markdown text and let you ctrl + c it!

As a bookmarlet:


To use it, go to your browser's bookmark manager and create a new bookmark, with the URL being the above JS (make sure the javascript: at the front is there!) and name as whatever you like. You can then click the bookmarlet to ues it on any XKCD page! :)

As an example to show parenthesis escaping, http://xkcd.com/4/ (URL https://imgs.xkcd.com/comics/landscape_cropped_(1).jpg) produces an escaped version:

[![Landscape (sketch)](https://imgs.xkcd.com/comics/landscape_cropped_\(1\).jpg "There's a river flowing through the ocean")](http://xkcd.com/4), which shows:

Landscape (sketch)

  • 1
    It looks like you one uped mine. Commented Aug 12, 2015 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .