Mercurial > hg > Feedworm
view FeedUpdater.py @ 93:e030c6bd2d81
when selecting a feed, set the title column width to 80% of the total width
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Mon, 27 Dec 2010 07:41:11 +0100 |
parents | 479e8c06de49 |
children | 99807963d9e0 |
line wrap: on
line source
from datetime import datetime from Feed import Feed from FeedEntry import FeedEntry import feedparser import logging STATUS_ERROR = 400 log = logging.getLogger("FeedUpdater") def updateAllFeeds(session): allFeeds = findFeedsToUpdate(session) for feed in allFeeds: try: FeedUpdater(session, feed).update() except FeedUpdateException, fue: log.warn("problems while updating feed " + feed.rss_url + ": " + str(fue)) session.commit() def findFeedsToUpdate(session): return session.query(Feed).filter(Feed.next_update < datetime.now()) def createNewFeed(url, session): result = feedparser.parse(url) title = result["feed"].title newFeed = Feed(title, url) session.add(newFeed) FeedUpdater(session, newFeed).update() def normalize(entry): if not hasattr(entry, "id"): entry.id = entry.link if not hasattr(entry, "updated_parsed"): entry.updated_parsed = datetime.today() else: entry.updated_parsed = datetime(*entry.updated_parsed[:6]) if not hasattr(entry, "summary"): if hasattr(entry, "content"): entry.summary = entry.content[0].value else: entry.summary = "" class FeedUpdater(object): def __init__(self, session, feed): self.session = session self.feed = feed def update(self): log.info("updating " + self.feed.rss_url) result = self.getFeed() for entry in result.entries: self.processEntry(entry) self.feed.incrementNextUpdateDate() def getFeed(self): result = feedparser.parse(self.feed.rss_url) if result["bozo"] > 0: raise FeedUpdateException() if result["status"] >= STATUS_ERROR: raise FeedUpdateException() return result def processEntry(self, entry): normalize(entry) feedEntry = FeedEntry.findById(entry.id, self.session) if feedEntry is None: self.createFeedEntry(entry) def createFeedEntry(self, entry): new = FeedEntry.create(entry) new.feed = self.feed self.session.add(new) log.info("new feed entry: " + entry.title) class FeedUpdateException(Exception): pass