Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2014-03-26 15:18:35

Agnishom
Real Member
From: The Complex Plane
Registered: 2011-01-29
Posts: 16,834
Website

XKCD Downloader

Python 2.x Code

"""
Downloads all XKCD comic strips with respective titles (except 404, guess why)
Author: Manan
Version 2172014
"""

#!/usr/bin/env python

import urllib
import os.path
import sys
import re

def download():
    directory = raw_input("Enter the filepath for the comics to be saved ")
    count = 0
    
    if not directory.endswith("/"):
        directory += str("/")

    pattern = re.compile("/comics/[a-z0-9_()]*.(jpg|png|gif)")
    
    while True:
        count += 1
        if count == 404:
	    # Comic number 404 is not supported.
            count +=1
        page = urllib.urlopen(str("http://www.xkcd.com/") + str(count))
        if "Last-Modified" not in page.headers:
            break
        search = pattern.search(page.read())
        if search:
            matcher = search.group()
            name = os.path.basename(matcher)
            if (not os.path.exists(str(directory) + str(name))):
                urllib.urlretrieve("http://imgs.xkcd.com/" + matcher, os.path.join(directory, name))
                print "Downloaded comic " + str(count) + " successfully"
            else:
                print "Comic was already present in the specified directory"
	
if __name__ == '__main__':
    download()

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
'Humanity is still kept intact. It remains within.' -Alokananda

Offline

Board footer

Powered by FluxBB