view FeedUpdater.py @ 4:e0199f383442

retrieve a feed for the given URL, store entries as feed_entry rows into the database
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 26 Apr 2010 19:33:07 +0200
parents
children bfd47f55d85b
line wrap: on
line source


from Feed import Feed
from FeedEntry import FeedEntry
import feedparser

def updateAllFeeds(session):
    allFeeds = session.query(Feed)
    for feed in allFeeds:
        FeedUpdater(session, feed).update()
        session.commit()

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

    def processEntry(self, entry):
        feedEntry = FeedEntry.findById(entry.id, self.session)
        if feedEntry is None:
            self.createFeedEntry(entry)
            
    def createFeedEntry(self, entry):
        newEntry = FeedEntry(id=entry.id, url=entry.link, title=entry.title, summary=entry.summary)
        newEntry.feed = self.feed
        self.session.add(newEntry)