import Globals from Globals import Persistent from OFS.Folder import Folder from OFS.SimpleItem import SimpleItem import fetcher import feedparser import RSSformatter from BTrees.OOBTree import OOBTree from utils import gen_key from Acquisition import aq_base, aq_inner, aq_parent, Explicit import time class Feeds(Folder, Persistent, Explicit): """ cached feeds container """ meta_type = 'FeedsContainer' id = 'feeds' manage_options = Folder.manage_options def __init__(self): self._feeds = OOBTree() # {'url' -> id } self._updated = OOBTree() # {'url' -> time.time() } def _check_cache(self, url): x = self._updated.get(url) print "comparing", x, ">", time.time()-3525 if x > time.time()-3525: try: return getattr(self, self._feeds.get(url)).content except AttributeError: return False except TypeError: return False else: # feedback enabled? c = self._getCacheObject(url) if c is not None: if c.isFeedbackEnabled(): print "FM: alrighty... feedback enabled!" return c.content return False def _cache(self, url, content): _id = None c = None create = 0 if self._feeds.has_key(url): c = self._getCacheObject(url) if c is None: create = 1 else: c._update(content) if create: _id = gen_key('cache') c = Cache(_id, url, content) self._setObject(c.id, c) c = getattr(self, _id) fburl = content.headers.get('x-feedback', None) if fburl is not None: token = gen_key('feedback') c._setFeedback(fburl) c._setFeedbackToken(token) import xmlrpclib s = xmlrpclib.Server(fburl) resp = s.feedback.request(url, c.absolute_url(), token) print resp self._feeds[url] = _id self._updated[url] = time.time() def _getCacheObject(self, url): _id = self._feeds.get(url) if _id is None: _id = '' c = getattr(self, _id, None) return c Globals.InitializeClass(Feeds) class Cache(SimpleItem, Persistent, Explicit): """ cached RSS """ meta_type = 'Cache' def __init__(self, id, url, content): """ init. set id, content """ self.id = id self.content = content self.url = url self.title = url self.updated = time.time() self._feedback_url = '' self._feedback_token = '' def isFeedbackEnabled(self): return len(self._feedback_url) > 0 def _setFeedback(self, url): self._feedback_url = url def _getFeedbackToken(self): return self._feedback_token def _setFeedbackToken(self, token): self._feedback_token = token def _update(self, content): self.content = content def update(self): """ update... """ import feedparser as fd f = fd.parse(self.url) self.content = f self.aq_parent._updated[self.url] = time.time() print "feed", self.url, " updated" def notify(self, rurl, token): if token == self._getFeedbackToken(): self.update() Globals.InitializeClass(Cache)