diff FeedUpdater.py @ 4:e0199f383442

retrieve a feed for the given URL, store entries as feed_entry rows into the database
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 26 Apr 2010 19:33:07 +0200
parents
children bfd47f55d85b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FeedUpdater.py	Mon Apr 26 19:33:07 2010 +0200
@@ -0,0 +1,30 @@
+
+from Feed import Feed
+from FeedEntry import FeedEntry
+import feedparser
+
+def updateAllFeeds(session):
+    allFeeds = session.query(Feed)
+    for feed in allFeeds:
+        FeedUpdater(session, feed).update()
+        session.commit()
+
+class FeedUpdater(object):
+    def __init__(self, session, feed):
+        self.session = session
+        self.feed = feed
+        
+    def update(self):
+        result = feedparser.parse(self.feed.url)
+        for entry in result.entries:
+            self.processEntry(entry)
+
+    def processEntry(self, entry):
+        feedEntry = FeedEntry.findById(entry.id, self.session)
+        if feedEntry is None:
+            self.createFeedEntry(entry)
+            
+    def createFeedEntry(self, entry):
+        newEntry = FeedEntry(id=entry.id, url=entry.link, title=entry.title, summary=entry.summary)
+        newEntry.feed = self.feed
+        self.session.add(newEntry)