def main(): comicid = '' today_comic = urlopen('http://www.qwantz.com/index.php' + comicid) result = today_comic.read() todayparsed = fromstring(result) try: comicurl = todayparsed.cssselect('img.comic')[0].attrib['src'] comicnum = int(re.findall(r'comic2-([0-9]+)\.png',comicurl)[0]) except Exception as e: print "failed parsing qwantz.com: " + e.message tmpfilep = StringIO() comic_image = urlopen(comicurl).read() tmpfilep.write(comic_image) tmpfilep.seek(0) logging.debug("wrote a file of length %d" % len(comic_image)) d = Dinocr(tmpfilep) if d.erased_pixels > 2000: error_out('large amount of erases in a new comic (%d erases, %d uncertainty)' % (d.erased_pixels,d.uncertainty),True) trigram = d.choose_random_trigram() anything = False # don't acept a trigram that has a non-alphanumeric word in it if any([all([not x.isalnum() for x in word]) for word in trigram.split()]): error_out('words in this trigram are weird: %s' % trigram,True) return trigram
comicid = '' if len(sys.argv) == 2: comicid = '?comic=' + str(sys.argv[1]) today_comic = urlopen('http://www.qwantz.com/index.php' + comicid) result = today_comic.read() todayparsed = fromstring(result) try: comicurl = todayparsed.cssselect('img.comic')[0].attrib['src'] comicnum = int(re.findall(r'comic2-([0-9]+)\.png',comicurl)[0]) except Exception as e: error_out("failed parsing qwantz.com: " + e.message,True) if comicid == '': prev_comic = update_comicnum(comicnum) if comicnum == prev_comic: error_out("same comic") if comicnum != prev_comic + 1: error_out("unexpected comic number: previous was %d, this was %d" % (prev_comic,comicnum),True) system('curl %s > /tmp/comic.png' % comicurl) d = Dinocr('/tmp/comic.png') if d.erased_pixels > 2000: error_out('large amount of erases in a new comic (%d erases, %d uncertainty)' % (d.erased_pixels,d.uncertainty),True) trigram = d.choose_random_trigram() anything = False # don't acept a trigram that has a non-alphanumeric word in it if any([all([not x.isalnum() for x in word]) for word in trigram.split()]): error_out('words in this trigram are weird: %s' % trigram,True) result = post_to_twitter(trigram) infofile = open('trigramosaurus.txt','w') infofile.write(str(comicnum)) infofile.close()