from twisted.web import resource from twisted.internet import reactor import time from hatena import Log, NotFound from DB import Database from Hatenatools import UGO #makes a flipnote list of the newest flipnotes class PyResource(resource.Resource): isLeaf = True def __init__(self): resource.Resource.__init__(self) self.pages = []#10 first pages self.newestflip = None reactor.callLater(4, self.Update)#or it'll clash with the others in the terminal def render(self, request): page = int(request.args["page"][0]) if "page" in request.args else 1 if len(self.pages) >= page: path = "/".join(request.path.split("/")[3:]) Log(request, "%s page %i" % (path, page)) request.responseHeaders.setRawHeaders('content-type', ['text/plain']) return self.pages[page-1] #elif (page-1)*50 < len(Database.Newest): # pass else: return NotFound.render(request) #=== def Update(self):#called every minute reactor.callLater(60, self.Update) if self.newestflip <> Database.Newest[0]: self.newestflip = Database.Newest[0] reactor.callInThread(self.UpdateThreaded, Database.Newest) def UpdateThreaded(self, flipnotes):#run in an another thread #create pages: pages = [] pagecount = (len(flipnotes)-1)/50 + 1 if pagecount > 10: pagecount = 10#temp? flipcount = len(flipnotes) for i in xrange(min((pagecount, 10))): pages.append(self.MakePage(flipnotes[i*50:i*50+50], i+1, i