Blog /Python Talks to Questionable Content

August 06, 2008 10:55 +0000  |  Geek Stuff Programming Python 2

Yeah, I know it's late, but I got obsessed and couldn't turn away. Now I have a really slick python script that pulls down the latest Questionable Content strip and adds it to my collection to read later:

#!/usr/bin/env python

import os, sys, re
import pycurl, urllib2

destination = "/path/to/qc/repository"

class QC:

  def __init__(self):

    self.contents = ''
    self.baseurl  = 'http://questionablecontent.net/'


  def callback(self,buf):

    self.contents = self.contents + buf


  def end(self):

    c = pycurl.Curl()
    c.setopt(c.URL, self.baseurl)
    c.setopt(c.WRITEFUNCTION, self.callback)
    c.perform()
    c.close()

    try:
      return int(re.search('.*\/comics\/(\d+)\.png', self.contents).group(1))
    except:
      print ""
      print "  The regular expression no longer works."
      print "  You might want to check into that."
      print ""
      exit()


  def fetch(self,n):

    strip = urllib2.build_opener().open(self.baseurl + 'comics/' + str(n) + ".png").read()

    f = "%s%s%04d%s" % (destination, "/", n, ".png")

    fout = open(f, "wb")
    fout.write(strip)
    fout.close()


# Main -----------------------------------------------------------------------

qc = QC()

for i in range(1, qc.end() + 1):
  f = "%s%s%04d%s" % (destination, "/", i, ".png")
  if not os.path.exists(f):
    print "Fetching strip #" + str(i)
    qc.fetch(i)

Comments

Melanie
9 Aug 2008, 11:54 p.m.  | 

okay, this comment is not sarcastic. If you're really a fan of the comic and want to show support, you might want to reconsider your methods of reading the strip. It's helpful for the artist to know how many people are visiting his website, and it's only considerate of you to expose yourself to any advertising that may be going on in exchange for this free bit of entertainment. I know that might sound kind of stupid, but it's a courtesy thing. As a reminder, I am in fully support of giving money to creative artists who actually need it. Doing QC is this guy's livelihood, and if we want him to keep it up, we should give him money.

plus if you went to the actually site you could read his little notes on each strip, which are often very cute and informative.

thend.

Daniel
10 Aug 2008, 12:29 a.m.  | 

Frankly, I just don't have the time to visit the site so often and I despise ads -- regardless of who's getting the revenue. I'd buy the guy's swag if he was selling something I liked (I check every couple of months), but he doesn't have anything cool enough.

You're right though. The guy needs a tip jar.

Post a Comment

Markdown will work here, if you're into that sort of thing.