comparison FeedUpdater.py @ 9:fd4c8bfa62d6

FeedUpdater throws an exception if the URL could not be retrieved successfully. Includes unit tests.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 27 Apr 2010 10:22:35 +0200
parents 215c34f61e95
children 01a86b178e60
comparison
equal deleted inserted replaced
8:2da2b691345d 9:fd4c8bfa62d6
1 1
2 from datetime import datetime 2 from datetime import datetime
3 from Feed import Feed 3 from Feed import Feed
4 from FeedEntry import FeedEntry 4 from FeedEntry import FeedEntry
5 import feedparser 5 import feedparser
6
7 STATUS_OK = 200
6 8
7 def updateAllFeeds(session): 9 def updateAllFeeds(session):
8 allFeeds = session.query(Feed) 10 allFeeds = session.query(Feed)
9 for feed in allFeeds: 11 for feed in allFeeds:
10 FeedUpdater(session, feed).update() 12 FeedUpdater(session, feed).update()
14 def __init__(self, session, feed): 16 def __init__(self, session, feed):
15 self.session = session 17 self.session = session
16 self.feed = feed 18 self.feed = feed
17 19
18 def update(self): 20 def update(self):
19 result = feedparser.parse(self.feed.rss_url) 21 result = self.getFeed()
20 for entry in result.entries: 22 for entry in result.entries:
21 self.processEntry(entry) 23 self.processEntry(entry)
24
25 def getFeed(self):
26 result = feedparser.parse(self.feed.rss_url)
27 if result["status"] is not STATUS_OK:
28 raise FeedUpdateException()
29 return result
22 30
23 def processEntry(self, entry): 31 def processEntry(self, entry):
24 feedEntry = FeedEntry.findById(entry.id, self.session) 32 feedEntry = FeedEntry.findById(entry.id, self.session)
25 if feedEntry is None: 33 if feedEntry is None:
26 self.createFeedEntry(entry) 34 self.createFeedEntry(entry)
32 new.title = entry.title 40 new.title = entry.title
33 new.updated = datetime(*entry.updated_parsed[:6]) 41 new.updated = datetime(*entry.updated_parsed[:6])
34 new.summary = entry.summary 42 new.summary = entry.summary
35 new.feed = self.feed 43 new.feed = self.feed
36 self.session.add(new) 44 self.session.add(new)
45
46 class FeedUpdateException(Exception):
47 pass