comparison backend/sqlalchemy/FeedUpdater.py @ 141:6ea813cfac33

pull out common code for updating a feed into an abstract class, have the sqlalchemy backend use that class.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 24 Aug 2011 10:53:46 +0200
parents 862760b161b4
children 1524e1cefd39
comparison
equal deleted inserted replaced
140:e927c6910c38 141:6ea813cfac33
1 1
2 from backend.AbstractFeedUpdater import AbstractFeedUpdater, FeedUpdateException
2 from datetime import datetime 3 from datetime import datetime
3 from Feed import Feed 4 from Feed import Feed
4 from FeedEntry import FeedEntry 5 from FeedEntry import FeedEntry
5 import feedparser 6 import feedparser
6 import logging 7 import logging
44 session.add(newFeed) 45 session.add(newFeed)
45 46
46 FeedUpdater(session, newFeed).update() 47 FeedUpdater(session, newFeed).update()
47 48
48 49
49 class FeedUpdater(object): 50 class FeedUpdater(AbstractFeedUpdater):
50 def __init__(self, session, feed): 51 def __init__(self, session, feed):
52 AbstractFeedUpdater.__init__(self, feed)
51 self.session = session 53 self.session = session
52 self.feed = feed
53 54
54 def update(self): 55 def _processEntry(self, entry):
55 log.info("updating " + self.feed.rss_url)
56 result = self.getFeed()
57 for entry in result.entries:
58 self.processEntry(entry)
59 self.feed.incrementNextUpdateDate()
60
61 def getFeed(self):
62 result = feedparser.parse(self.feed.rss_url)
63 # bozo flags if a feed is well-formed.
64 # if result["bozo"] > 0:
65 # raise FeedUpdateException()
66 status = result["status"]
67 if status >= STATUS_ERROR:
68 raise FeedUpdateException("HTTP status " + str(status))
69 return result
70
71 def processEntry(self, entry):
72 normalize(entry)
73 feedEntry = FeedEntry.findById(entry.id, self.session) 56 feedEntry = FeedEntry.findById(entry.id, self.session)
74 if feedEntry is None: 57 if feedEntry is None:
75 self.createFeedEntry(entry) 58 self._createFeedEntry(entry)
76 59
77 def createFeedEntry(self, entry): 60 def _createFeedEntry(self, entry):
78 new = FeedEntry.create(entry) 61 new = FeedEntry.create(entry)
79 new.feed = self.feed 62 new.feed = self.feed
80 self.session.add(new) 63 self.session.add(new)
81 log.info("new feed entry: " + entry.title) 64 log.info("new feed entry: " + entry.title)
82
83 class FeedUpdateException(Exception):
84 pass