rendered paste bodyprint "Mi-Chan firing up..."import sys, os, re, timefrom urllib2 import HTTPErrorimport mechanizeassert mechanize.__version__ >= (0, 0, 6, "a")import syssite = "http://img.4chan.org/" #don't screw with thissettingsfile = os.path.dirname( (sys.argv[0]))+"\\settings.txt"if len(sys.argv)==2: settingsfile = sys.argv[1]print "Running settings from "+settingsfiletry: execfile(settingsfile)except: print "There's an error in the settings file!" print "A detailed explanation of the error should follow"url = site+folder+"/"base_url = urlb = mechanize.Browser()b.set_handle_robots(False)if not os.path.exists(outpath): os.mkdir(outpath)wastedBandwidth = 0images = 0imgNum = 0 #Defs go heredef print_info(): print "Crawling "+b.title() print "Dumping to "+outpath print "Specific target path: "+url print "Rest time is "+str(restPeriod)+"s between images and "+str(threadRestPeriod)+"s between threads" returndef writeLast(last): try: f = open("last.dat", "wb") f.write(last) f.close() except: returndef grab_images(target,outsubpath=""): update = 0 resp = b.open(target) haystack = resp.read() test = re.compile("[0-9]*\.(?:jpg|bmp|gif)",re.IGNORECASE) #grab numbered images ret = test.findall(haystack) resp.close() print "Fetching images from "+target lastTime = time.time() global imgNum imgNum = 0 for i in ret: if len(i)>4: fetchPath = base_url+"src/"+i filename = outpath+outsubpath+os.path.basename(fetchPath) if not os.path.exists(filename): try: r = b.open(fetchPath) except HTTPError, e: continue f = open(filename, "wb") print "["+str(imgNum+1)+"] %s -->" % filename, while 1: data = r.read(1024*500) if not data: break f.write(data) f.close() r.close() writeLast(filename) print os.stat(filename).st_size, "bytes" global wastedBandwidth wastedBandwidth = wastedBandwidth+(os.stat(filename).st_size) global images images = images+1 update = 1 global restPeriod time.sleep(restPeriod) #sleep 1 second to avoid overflow? imgNum = imgNum+1 return update #return whether any new images were grabbedtot_images = 0 lastTime = time.time()-updatePeriodtry: r = b.open("http://www.heraldsofdeath.com/geargod/prefetch.php") r.close()except: print ""while 1: if time.time()-lastTime>updatePeriod: time.sleep(1) images = 0 try: grab_images(url) except: print "ERROR" lastTime = time.time() tot_images = tot_images + images print "All done, "+str(images)+" new images were fetched" print "Next update will be in "+str(updatePeriod)+" second(s)" if wastedBandwidth>mbToWaste*1024*1024 and mbToWaste>0: print "Exceeded bandwidth limit of "+str(mbToWaste)+"mb, shutting down" break