diff FeedUpdater.py @ 28:72dfae865899

better logging when updating feeds, handle entries that have no id
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 29 Apr 2010 07:04:42 +0200
parents bdd1296a4b8c
children aaec263f07ca
line wrap: on
line diff
--- a/FeedUpdater.py	Thu Apr 29 06:47:47 2010 +0200
+++ b/FeedUpdater.py	Thu Apr 29 07:04:42 2010 +0200
@@ -5,8 +5,8 @@
 import feedparser
 import logging
 
-STATUS_OK = 200
-logger = logging.getLogger("FeedUpdater")
+STATUS_ERROR = 400
+log = logging.getLogger("FeedUpdater")
 
 def updateAllFeeds(session):
     allFeeds = session.query(Feed)
@@ -14,7 +14,7 @@
         try:
             FeedUpdater(session, feed).update()
         except FeedUpdateException:
-            logger.warn("problems while updating feed " + feed.rss_url)
+            log.warn("problems while updating feed " + feed.rss_url)
         session.commit()
 
 def createNewFeed(url, session):
@@ -23,6 +23,7 @@
     newFeed = Feed(title, url)
     session.add(newFeed)
     session.commit()
+    
     FeedUpdater(newFeed).update()
     session.commit()
 
@@ -32,21 +33,27 @@
         self.feed = feed
         
     def update(self):
+        log.info("updating " + self.feed.rss_url)
         result = self.getFeed()
         for entry in result.entries:
             self.processEntry(entry)
 
     def getFeed(self):
         result = feedparser.parse(self.feed.rss_url)
-        if result["status"] is not STATUS_OK:
+        if result["status"] >= STATUS_ERROR:
             raise FeedUpdateException()
         return result
 
     def processEntry(self, entry):
+        self.normalize(entry)
         feedEntry = FeedEntry.findById(entry.id, self.session)
         if feedEntry is None:
             self.createFeedEntry(entry)
             
+    def normalize(self, entry):
+        if not hasattr(entry, "id"):
+            entry.id = entry.link        
+    
     def createFeedEntry(self, entry):
         new = FeedEntry()
         new.id = entry.id