comparison FeedUpdater.py @ 62:abc0516a1c0c

FeedEntry provides a static method for creating new entries: better modularization and support for working with the class in interactive mode. FeedUpdater's normalize method is a module function now, again for ease of use in interactive scenarios
author dirk@xanthippe.ping.de
date Wed, 28 Jul 2010 20:30:34 +0200
parents dcee24702dd7
children 97c4e94f99cf
comparison
equal deleted inserted replaced
61:db35ab7753f0 62:abc0516a1c0c
11 def updateAllFeeds(session): 11 def updateAllFeeds(session):
12 allFeeds = findFeedsToUpdate(session) 12 allFeeds = findFeedsToUpdate(session)
13 for feed in allFeeds: 13 for feed in allFeeds:
14 try: 14 try:
15 FeedUpdater(session, feed).update() 15 FeedUpdater(session, feed).update()
16 except FeedUpdateException: 16 except FeedUpdateException, fue:
17 log.warn("problems while updating feed " + feed.rss_url) 17 log.warn("problems while updating feed " + feed.rss_url + ": " + str(fue))
18 session.commit() 18 session.commit()
19 19
20 def findFeedsToUpdate(session): 20 def findFeedsToUpdate(session):
21 return session.query(Feed).filter(Feed.next_update < datetime.now()) 21 return session.query(Feed).filter(Feed.next_update < datetime.now())
22 22
25 title = result["feed"].title 25 title = result["feed"].title
26 newFeed = Feed(title, url) 26 newFeed = Feed(title, url)
27 session.add(newFeed) 27 session.add(newFeed)
28 28
29 FeedUpdater(session, newFeed).update() 29 FeedUpdater(session, newFeed).update()
30
31 def normalize(entry):
32 if not hasattr(entry, "id"):
33 entry.id = entry.link
34 if not hasattr(entry, "updated_parsed"):
35 entry.updated_parsed = datetime.today()
36 else:
37 entry.updated_parsed = datetime(*entry.updated_parsed[:6])
38 if not hasattr(entry, "summary"):
39 if hasattr(entry, "content"):
40 entry.summary = entry.content[0].value
41 else:
42 entry.summary = ""
30 43
31 class FeedUpdater(object): 44 class FeedUpdater(object):
32 def __init__(self, session, feed): 45 def __init__(self, session, feed):
33 self.session = session 46 self.session = session
34 self.feed = feed 47 self.feed = feed
45 if result["status"] >= STATUS_ERROR: 58 if result["status"] >= STATUS_ERROR:
46 raise FeedUpdateException() 59 raise FeedUpdateException()
47 return result 60 return result
48 61
49 def processEntry(self, entry): 62 def processEntry(self, entry):
50 self.normalize(entry) 63 normalize(entry)
51 feedEntry = FeedEntry.findById(entry.id, self.session) 64 feedEntry = FeedEntry.findById(entry.id, self.session)
52 if feedEntry is None: 65 if feedEntry is None:
53 self.createFeedEntry(entry) 66 self.createFeedEntry(entry)
54
55 def normalize(self, entry):
56 if not hasattr(entry, "id"):
57 entry.id = entry.link
58 if not hasattr(entry, "updated_parsed"):
59 entry.updated_parsed = datetime.today()
60 else:
61 entry.updated_parsed = datetime(*entry.updated_parsed[:6])
62 if not hasattr(entry, "summary"):
63 entry.summary = ""
64 67
65 def createFeedEntry(self, entry): 68 def createFeedEntry(self, entry):
66 new = FeedEntry() 69 new = FeedEntry.create(entry)
67 new.id = entry.id
68 new.link = entry.link
69 new.title = entry.title
70 new.updated = entry.updated_parsed
71 new.summary = entry.summary
72 new.feed = self.feed 70 new.feed = self.feed
73 self.session.add(new) 71 self.session.add(new)
74 72
75 class FeedUpdateException(Exception): 73 class FeedUpdateException(Exception):
76 pass 74 pass