# HG changeset patch # User Dirk Olmes # Date 1273021679 -7200 # Node ID aaec263f07ca326d59e0ad90ab033692ea35fb4e # Parent 5813e3c10f1410967c0f48121040df10ec258f23 Feeds manage the point in time when the next update should happen. FeedUpdater only updates feeds that are due. diff -r 5813e3c10f14 -r aaec263f07ca .hgignore --- 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$ diff -r 5813e3c10f14 -r aaec263f07ca Feed.py --- 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 "" % (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 diff -r 5813e3c10f14 -r aaec263f07ca FeedUpdater.py --- 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) diff -r 5813e3c10f14 -r aaec263f07ca Mapping.py --- 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") } ) diff -r 5813e3c10f14 -r aaec263f07ca feedupdate-main.py --- 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() diff -r 5813e3c10f14 -r aaec263f07ca util.py --- 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()