view FeedUpdater.py @ 17:5fda8bd94fa8

make the model used to display feeds generic (so it can be used to display FeedEntries, too)
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 28 Apr 2010 03:37:01 +0200
parents e87c54b3a216
children bdd1296a4b8c
line wrap: on
line source


from datetime import datetime
from Feed import Feed
from FeedEntry import FeedEntry
import feedparser
import logging

STATUS_OK = 200
logger = logging.getLogger("FeedUpdater")

def updateAllFeeds(session):
    allFeeds = session.query(Feed)
    for feed in allFeeds:
        try:
            FeedUpdater(session, feed).update()
        except FeedUpdateException:
            logger.warn("problems while updating feed " + feed.rss_url)
        session.commit()

class FeedUpdater(object):
    def __init__(self, session, feed):
        self.session = session
        self.feed = feed
        
    def update(self):
        result = self.getFeed()
        for entry in result.entries:
            self.processEntry(entry)

    def getFeed(self):
        result = feedparser.parse(self.feed.rss_url)
        if result["status"] is not STATUS_OK:
            raise FeedUpdateException()
        return result

    def processEntry(self, entry):
        feedEntry = FeedEntry.findById(entry.id, self.session)
        if feedEntry is None:
            self.createFeedEntry(entry)
            
    def createFeedEntry(self, entry):
        new = FeedEntry()
        new.id = entry.id
        new.link = entry.link
        new.title = entry.title
        new.updated = datetime(*entry.updated_parsed[:6])
        new.summary = entry.summary
        new.feed = self.feed
        self.session.add(new)

class FeedUpdateException(Exception):
    pass