changeset 35:aaec263f07ca

Feeds manage the point in time when the next update should happen. FeedUpdater only updates feeds that are due.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 05 May 2010 03:07:59 +0200
parents 5813e3c10f14
children 74b8c9a9d5de
files .hgignore Feed.py FeedUpdater.py Mapping.py feedupdate-main.py util.py
diffstat 6 files changed, 31 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed May 05 02:14:05 2010 +0200
+++ b/.hgignore	Wed May 05 03:07:59 2010 +0200
@@ -3,6 +3,4 @@
 Ui_*.py
 
 syntax: regexp
-
-syntax: regexp
-^feedworm\.sqlite$
\ No newline at end of file
+^feedworm\.sqlite$
--- a/Feed.py	Wed May 05 02:14:05 2010 +0200
+++ b/Feed.py	Wed May 05 03:07:59 2010 +0200
@@ -1,4 +1,5 @@
 
+from datetime import datetime, timedelta
 import FeedEntry
 
 class Feed(object):
@@ -9,6 +10,9 @@
     def __init__(self, title, rss_url):
         self.title = title
         self.rss_url = rss_url
+        # default: update every 60 minutes
+        self.update_interval = 60
+        self.incrementNextUpdateDate()
 
     def __repr__(self):
         return "<Feed (%d) %s>" % (self.pk, self.title)
@@ -19,4 +23,8 @@
     def entriesSortedByUpdateDate(self):
         sortedEntries = list(self.entries)
         sortedEntries.sort(FeedEntry.compareByUpdateDate)
-        return sortedEntries
\ No newline at end of file
+        return sortedEntries
+
+    def incrementNextUpdateDate(self):
+        delta = timedelta(minutes=self.update_interval)
+        self.next_update = datetime.now() + delta
--- a/FeedUpdater.py	Wed May 05 02:14:05 2010 +0200
+++ b/FeedUpdater.py	Wed May 05 03:07:59 2010 +0200
@@ -9,7 +9,7 @@
 log = logging.getLogger("FeedUpdater")
 
 def updateAllFeeds(session):
-    allFeeds = session.query(Feed)
+    allFeeds = findFeedsToUpdate(session)
     for feed in allFeeds:
         try:
             FeedUpdater(session, feed).update()
@@ -17,6 +17,9 @@
             log.warn("problems while updating feed " + feed.rss_url)
         session.commit()
 
+def findFeedsToUpdate(session):
+    return session.query(Feed).filter(Feed.next_update < datetime.now())
+
 def createNewFeed(url, session):
     result = feedparser.parse(url)
     title = result["feed"].title
@@ -37,6 +40,7 @@
         result = self.getFeed()
         for entry in result.entries:
             self.processEntry(entry)
+        self.feed.incrementNextUpdateDate()
 
     def getFeed(self):
         result = feedparser.parse(self.feed.rss_url)
--- a/Mapping.py	Wed May 05 02:14:05 2010 +0200
+++ b/Mapping.py	Wed May 05 03:07:59 2010 +0200
@@ -28,7 +28,9 @@
     feedTable = Table("feed", metadata,
         Column("pk", Integer, primary_key=True),
         Column("title", String(255), nullable=False),
-        Column("rss_url", String(255), nullable=False)
+        Column("rss_url", String(255), nullable=False),
+        Column("update_interval", Integer, nullable=False),
+        Column("next_update", DateTime, nullable=False)
     )
     
     feedEntryTable = Table("feed_entry", metadata,
@@ -49,6 +51,6 @@
     mapper(FeedEntry, feedEntryTable)
     mapper(Feed, feedTable,
         properties = {
-            "entries" : relation(FeedEntry, backref = "feed")
+            "entries" : relation(FeedEntry, backref="feed")
         }
     )
--- a/feedupdate-main.py	Wed May 05 02:14:05 2010 +0200
+++ b/feedupdate-main.py	Wed May 05 03:07:59 2010 +0200
@@ -2,18 +2,12 @@
 
 import Database
 from Feed import Feed
-from FeedUpdater import updateAllFeeds
+import FeedUpdater
 import logging
 import util
 
 logger = logging.getLogger("feedupdater")
 
-def createFeed(session):
-    if len(session.query(Feed).all()) == 0:
-        newFeed = Feed("Joel on Software", "http://www.joelonsoftware.com/rss.xml")
-        session.add(newFeed)
-        session.commit()
-
 def listFeeds(session):
     allFeeds = session.query(Feed)
     for feed in allFeeds:
@@ -25,7 +19,9 @@
     util.configureLogging()
     session = Database.createSession()
     
-    #createFeed(session)
-    #updateAllFeeds(session)
+    #util.loadFeeds(session)
+    #util.forceUpdateAllFeeds(session)
     #listFeeds(session)
-    #session.close()
+    FeedUpdater.updateAllFeeds(session)
+    
+    session.close()
--- a/util.py	Wed May 05 02:14:05 2010 +0200
+++ b/util.py	Wed May 05 03:07:59 2010 +0200
@@ -1,5 +1,6 @@
 
 from ConfigParser import ConfigParser
+from datetime import datetime, timedelta
 from Feed import Feed
 import logging
 import socket
@@ -30,3 +31,8 @@
         session.add(feed)
     file.close()
     session.commit()
+
+def forceUpdateAllFeeds(session=None):
+    for feed in Feed.all(session):
+        feed.next_update = datetime.now() - timedelta(minutes=1)
+    session.commit()