def fetch(sid, fc, lc, callback=None): lst = [] if fc == lc: if callback is not None: callback(sid, fc) return [(fc, urllib2.urlopen("http://www.fanfiction.net/s/%s/%s/" % (sid, fc)).read())] if lc == 0: lst+=fetch(sid, fc, fc, callback) if not isValidChapter(lst[-1][1]): raise Error, "not a valid chapter" lc = parsechapter(lst[-1][1]) if lc == fc: return lst return lst+fetch(sid, fc+1, lc, callback) #normal - a range of chapters. if fc > lc: raise Error, "first chapter cannot be greater than last." for i in range(fc, lc+1): lst+=fetch(sid, i, i, callback) return lst