Mercurial > hg > Feedworm
comparison FeedUpdater.py @ 100:99807963d9e0
use the URL as feed title if the feed itself does not come with a title
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Mon, 14 Feb 2011 20:51:03 +0100 |
parents | 479e8c06de49 |
children | b2a51c24f209 |
comparison
equal
deleted
inserted
replaced
99:e59d722439b5 | 100:99807963d9e0 |
---|---|
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 |
23 def createNewFeed(url, session): | 23 def createNewFeed(url, session): |
24 result = feedparser.parse(url) | 24 result = feedparser.parse(url) |
25 title = result["feed"].title | 25 if result.has_key("title"): |
26 title = result["feed"].title | |
27 else: | |
28 title = url | |
26 newFeed = Feed(title, url) | 29 newFeed = Feed(title, url) |
27 session.add(newFeed) | 30 session.add(newFeed) |
28 | 31 |
29 FeedUpdater(session, newFeed).update() | 32 FeedUpdater(session, newFeed).update() |
30 | 33 |
31 def normalize(entry): | 34 def normalize(entry): |
32 if not hasattr(entry, "id"): | 35 if not hasattr(entry, "id"): |
33 entry.id = entry.link | 36 entry.id = entry.link |
43 | 46 |
44 class FeedUpdater(object): | 47 class FeedUpdater(object): |
45 def __init__(self, session, feed): | 48 def __init__(self, session, feed): |
46 self.session = session | 49 self.session = session |
47 self.feed = feed | 50 self.feed = feed |
48 | 51 |
49 def update(self): | 52 def update(self): |
50 log.info("updating " + self.feed.rss_url) | 53 log.info("updating " + self.feed.rss_url) |
51 result = self.getFeed() | 54 result = self.getFeed() |
52 for entry in result.entries: | 55 for entry in result.entries: |
53 self.processEntry(entry) | 56 self.processEntry(entry) |
64 def processEntry(self, entry): | 67 def processEntry(self, entry): |
65 normalize(entry) | 68 normalize(entry) |
66 feedEntry = FeedEntry.findById(entry.id, self.session) | 69 feedEntry = FeedEntry.findById(entry.id, self.session) |
67 if feedEntry is None: | 70 if feedEntry is None: |
68 self.createFeedEntry(entry) | 71 self.createFeedEntry(entry) |
69 | 72 |
70 def createFeedEntry(self, entry): | 73 def createFeedEntry(self, entry): |
71 new = FeedEntry.create(entry) | 74 new = FeedEntry.create(entry) |
72 new.feed = self.feed | 75 new.feed = self.feed |
73 self.session.add(new) | 76 self.session.add(new) |
74 log.info("new feed entry: " + entry.title) | 77 log.info("new feed entry: " + entry.title) |