changeset 144:74217db92993

updating feeds on the couchdb backend works now
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 24 Aug 2011 11:54:06 +0200
parents f0941f42314c
children 71c5dc02ff87
files backend/AbstractFeedUpdater.py backend/couchdb/CouchDbBackend.py backend/couchdb/Feed.py backend/couchdb/FeedEntry.py backend/couchdb/FeedUpdater.py backend/sqlalchemy/FeedUpdater.py
diffstat 6 files changed, 65 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/backend/AbstractFeedUpdater.py	Wed Aug 24 11:04:09 2011 +0200
+++ b/backend/AbstractFeedUpdater.py	Wed Aug 24 11:54:06 2011 +0200
@@ -21,7 +21,7 @@
         for entry in result.entries:
             self._normalize(entry)
             self._processEntry(entry)
-        self.feed.incrementNextUpdateDate()
+        self._incrementFeedUpdateDate()
 
     def _retrieveFeed(self):
         result = feedparser.parse(self.feed.rss_url)
@@ -49,6 +49,9 @@
     def _processEntry(self, entry):
         raise Exception("_processEntry is abstract, subclasses must override")
 
+    def _incrementFeedUpdateDate(self):
+        raise Exception("_incrementNextUpdateDate is abstract, subclasses must override")
+
 
 class FeedUpdateException(Exception):
     pass
--- a/backend/couchdb/CouchDbBackend.py	Wed Aug 24 11:04:09 2011 +0200
+++ b/backend/couchdb/CouchDbBackend.py	Wed Aug 24 11:54:06 2011 +0200
@@ -1,4 +1,5 @@
 
+from FeedUpdater import FeedUpdater
 from Preferences import Preferences
 from backend.couchdb.Feed import Feed
 import couchdb
@@ -44,7 +45,7 @@
         allFeeds = Feed.all(self.database)
         for feed in allFeeds:
             if feed.needsUpdate():
-                print("feed needs update " + str(feed))
+                FeedUpdater(feed, self.database).update()
 
     def expireFeedEntries(self):
         print("Expiring feeds is not yet implemented")
--- a/backend/couchdb/Feed.py	Wed Aug 24 11:04:09 2011 +0200
+++ b/backend/couchdb/Feed.py	Wed Aug 24 11:54:06 2011 +0200
@@ -1,5 +1,5 @@
 
-from datetime import datetime
+from datetime import datetime, timedelta
 
 DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
 
@@ -20,6 +20,12 @@
         delta = datetime.now() - updateDate
         return delta.total_seconds() > self._updateIntervalInSeconds()
 
+    def incrementedUpdateDate(self):
+        updateDate = self._nextUpdateDate()
+        updateInterval = self._updateIntervalInSeconds()
+        delta = timedelta(seconds=updateInterval)
+        return updateDate + delta
+
     def _nextUpdateDate(self):
         nextUpdateString = self.next_update
         return datetime.strptime(nextUpdateString, DATE_FORMAT)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/couchdb/FeedEntry.py	Wed Aug 24 11:54:06 2011 +0200
@@ -0,0 +1,18 @@
+
+class FeedEntry(object):
+    @staticmethod
+    def findByLink(link, database):
+        viewResults = database.view("feedtest/feedEntry_by_link")
+        resultsForKey = viewResults[link]
+        try:
+            row = iter(resultsForKey).next()
+            return FeedEntry(row)
+        except StopIteration:
+            return None
+
+    def __init__(self, row):
+        self.row = row
+
+    def __getattr__(self, key):
+        return self.row.value[key]
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/couchdb/FeedUpdater.py	Wed Aug 24 11:54:06 2011 +0200
@@ -0,0 +1,31 @@
+
+from FeedEntry import FeedEntry
+from backend.AbstractFeedUpdater import AbstractFeedUpdater
+from datetime import datetime
+import Feed
+import logging
+
+log = logging.getLogger("FeedUpdater")
+
+class FeedUpdater(AbstractFeedUpdater):
+    def __init__(self, feed, database):
+        AbstractFeedUpdater.__init__(self, feed)
+        self.database = database
+
+    def _processEntry(self, entry):
+        feedEntry = FeedEntry.findByLink(entry.link, self.database)
+        if feedEntry is None:
+            self._createFeedEntry(entry)
+
+    def _createFeedEntry(self, entry):
+        log.info("new feed entry: " + entry.title)
+        feedEntry = { "type" : "feedEntry", "feed" : self.feed._id, "link" : entry.link,
+             "title" : entry.title, "summary" : entry.summary, "read" : False,
+             "updated" : entry.updated}
+        self.database.save(feedEntry)
+
+    def _incrementFeedUpdateDate(self):
+        updateDate = self.feed.incrementedUpdateDate()
+        document = self.database.get(self.feed._id)
+        document["next_update"] = datetime.strftime(updateDate, Feed.DATE_FORMAT)
+        self.database.save(document)
--- a/backend/sqlalchemy/FeedUpdater.py	Wed Aug 24 11:04:09 2011 +0200
+++ b/backend/sqlalchemy/FeedUpdater.py	Wed Aug 24 11:54:06 2011 +0200
@@ -48,3 +48,6 @@
         new.feed = self.feed
         self.session.add(new)
         log.info("new feed entry: " + entry.title)
+
+    def _incrementFeedUpdateDate(self):
+        self.feed.incrementNextUpdateDate()