comparison backend/sqlalchemy/FeedUpdater.py @ 167:a3c945ce434c

adjust the sqlalchemy backend to the changes in AbstractFeedUpdater
author dirk
date Mon, 05 Sep 2011 19:39:08 +0200
parents 86f828096aaf
children f4708d38419c
comparison
equal deleted inserted replaced
166:04c3b9796b89 167:a3c945ce434c
1 1
2 from Feed import Feed
3 from FeedEntry import FeedEntry
2 from backend.AbstractFeedUpdater import AbstractFeedUpdater, FeedUpdateException 4 from backend.AbstractFeedUpdater import AbstractFeedUpdater, FeedUpdateException
3 from datetime import datetime 5 from datetime import datetime
4 from Feed import Feed
5 from FeedEntry import FeedEntry
6 import feedparser
7 import logging 6 import logging
8 7
9 log = logging.getLogger("FeedUpdater") 8 log = logging.getLogger("FeedUpdater")
10 9
11 def updateAllFeeds(session): 10 def updateAllFeeds(preferences, session):
12 allFeeds = findFeedsToUpdate(session) 11 allFeeds = findFeedsToUpdate(session)
13 for feed in allFeeds: 12 for feed in allFeeds:
14 try: 13 try:
15 FeedUpdater(session, feed).update() 14 FeedUpdater(preferences, session).update(feed)
16 except FeedUpdateException, fue: 15 except FeedUpdateException, fue:
17 log.warn("problems while updating feed " + feed.rss_url + ": " + str(fue)) 16 log.warn("problems while updating feed " + feed.rss_url + ": " + str(fue))
18 session.commit() 17 session.commit()
19 18
20 def findFeedsToUpdate(session): 19 def findFeedsToUpdate(session):
21 return session.query(Feed).filter(Feed.next_update < datetime.now()) 20 return session.query(Feed).filter(Feed.next_update < datetime.now())
22 21
23 def createNewFeed(url, session):
24 # when updating to python3 see http://code.google.com/p/feedparser/issues/detail?id=260
25 result = feedparser.parse(url)
26 if result.has_key("title"):
27 title = result["feed"].title
28 else:
29 title = url
30 newFeed = Feed(title, url)
31 session.add(newFeed)
32
33 FeedUpdater(session, newFeed).update(result)
34
35 22
36 class FeedUpdater(AbstractFeedUpdater): 23 class FeedUpdater(AbstractFeedUpdater):
37 def __init__(self, session, feed): 24 def __init__(self, preferences, session):
38 AbstractFeedUpdater.__init__(self, feed) 25 AbstractFeedUpdater.__init__(self, preferences)
39 self.session = session 26 self.session = session
40 27
41 def _processEntry(self, entry): 28 def _processEntry(self, entry):
42 feedEntry = FeedEntry.findById(entry.id, self.session) 29 feedEntry = FeedEntry.findById(entry.id, self.session)
43 if feedEntry is None: 30 if feedEntry is None: