Created
October 31, 2011 09:25
-
-
Save reflog/1327175 to your computer and use it in GitHub Desktop.
A small script to store XKCD comics in PDF format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, urllib2,json | |
Base = 'http://xkcd.com/' | |
Tail = 'info.0.json' | |
def main(): | |
n = int(sys.argv[1]) | |
k = int(sys.argv[2]) | |
out = open("out3.html","w") | |
out.write("<html><body>\n") | |
for i in range(n,k+1): | |
url = '%s%d/%s' % (Base, i, Tail) | |
print url | |
f = urllib2.urlopen(url) | |
a = f.readline() | |
f.close() | |
d = json.loads(a) | |
out.write("<hr/>\n") | |
out.write(d['title'] + "<br/>\n") | |
out.write("<img src=\"%d.%s\"/> <br/>\n"%(i,d['img'][-3:])) | |
out.write(d['alt'].replace("\n","<br/>") + "</br>\n") | |
u = urllib2.urlopen(d['img']) | |
localFile = open('%d.%s'%(i,d['img'][-3:]), 'wb') | |
localFile.write(u.read()) | |
localFile.close() | |
out.flush() | |
out.write("</body></html>\n"); | |
out.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment