@@ -0,0 +1,1899 @@
+from twisted.internet.defer import inlineCallbacks%0Afrom twisted.internet import task%0Afrom twisted.internet import reactor%0Afrom twisted.web.client import getPage%0A%0Afrom autobahn import wamp%0Afrom autobahn.twisted.wamp import ApplicationSession%0A%0Aimport anime_parser%0A%0Aclass HorribleSubsBackend(ApplicationSession):%0A%0A    def __init__(self, config):%0A        super(HorribleSubsBackend, self).__init__(config)%0A        self.initialize()%0A%0A    def initialize(self):%0A        self.animes_cache = %7B%7D%0A        self.polling_task = task.LoopingCall(self.fetchAnimes)%0A        self.polling_task.start(0.5)%0A%0A    def fetchAnimes(self):%0A        d = getPage('http://horriblesubs.info/lib/latest.php')%0A        d.addCallbacks(self.parseAnime, self.onFetchError)%0A%0A    def parseAnime(self, pageContent):%0A        hasChanged = False%0A        for anime in anime_parser.parse_animes(pageContent):%0A            try:%0A                if anime.id in self.animes_cache:%0A                    # I need to update partially the object%0A                    pass%0A                else:%0A                    hasChanged = True%0A                    self.animes_cache%5Banime.id%5D = anime%0A                    self.publish(u'horriblesubs.new_anime_released', anime.serialize())%0A            except Exception as e:%0A                print ('Failed to update an anime. (%7B%7D)'.format(e))%0A%0A        if hasChanged:%0A            print ('Cache updated! (%7B%7D animes in the DB)'.format(len(self.animes_cache)))%0A%0A    def onFetchError(self, error):%0A        print (%22Error while fetching the page: %7B%7D%22.format(error))%0A%0A    @wamp.register(u'horriblesubs.get_latest')%0A    def getLatest(self, limit=50):%0A        return dict(map(lambda tpl: (tpl%5B0%5D, tpl%5B1%5D.serialize()), self.animes_cache.items()%5B:limit%5D))%0A%0A    @inlineCallbacks%0A    def onJoin(self, details):%0A        res = yield self.register(self)%0A        print (%22HorribleSubsBackend: %7B%7D procedures registered.%22.format(len(res)))%0A%0A

Add the session API