view FeedUpdater.py @ 51:8eee0e671696

set our custom window icon on all dialogs
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 18 May 2010 02:32:33 +0200
parents 4b0d686493fb
children dcee24702dd7
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:
            log.warn("problems while updating feed " + feed.rss_url)
        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()

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["status"] >= STATUS_ERROR:
            raise FeedUpdateException()
        return result

    def processEntry(self, entry):
        self.normalize(entry)
        feedEntry = FeedEntry.findById(entry.id, self.session)
        if feedEntry is None:
            self.createFeedEntry(entry)
            
    def normalize(self, entry):
        if not hasattr(entry, "id"):
            entry.id = entry.link        
    
    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