4

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.

4
  • @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.)
    – PyRulez
    Aug 12, 2015 at 4:48
  • @Daniel (When you posted that, I fired up my laptop to check.)
    – PyRulez
    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
    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.
    – PyRulez
    Aug 12, 2015 at 12:20

1 Answer 1

5

Javascript version

Well... jQuery:

$.ajax({
    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:

javascript:(function()%7Bfunction%20callback()%7B(function(%24)%7Bvar%20jQuery%3D%24%3B%24.ajax(%7Burl%3A%20%22http%3A%2F%2Fdynamic.xkcd.com%2Fapi-0%2Fjsonp%2Fcomic%2F%22%2B%24(location).attr('href').split('%2F')%5B3%5D%2CdataType%3A%20%22json%22%2Csuccess%3A%20function(data)%20%7Bvar%20title%20%3D%20data.title%2Cimg%20%3D%20data.img.replace(%2F(%3F%3D%5B()%5D)%2Fg%2C%20'%5C%5C')%2Calt%20%3D%20data.alt%2Cnum%20%3D%20data.num%3Bwindow.prompt('Ctrl%2FCmd%2BC%20the%20following%3A'%2C%20'%5B!%5B'%2Btitle%2B'%5D('%2Bimg%2B'%20%22'%2Balt%2B'%22)%5D(http%3A%2F%2Fxkcd.com%2F'%2Bnum%2B')')%3B%7D%7D)%7D)(jQuery.noConflict(true))%7Dvar%20s%3Ddocument.createElement(%22script%22)%3Bs.src%3D%22https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fjquery%2F1.7.1%2Fjquery.min.js%22%3Bif(s.addEventListener)%7Bs.addEventListener(%22load%22%2Ccallback%2Cfalse)%7Delse%20if(s.readyState)%7Bs.onreadystatechange%3Dcallback%7Ddocument.body.appendChild(s)%3B%7D)()

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
  • 1
    It looks like you one uped mine.
    – PyRulez
    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 .