Source

copelco / lib / copelco / apps / aggregator / tasks.py

Full commit
import time
import datetime
import feedparser

from celery.task import Task
from celery.registry import tasks

from copelco.apps.aggregator.models import Feed, FeedItem


class UpdateFeed(Task):
    def run(self, feed_id):
        logger = self.get_logger()
        logger.debug("Looking up feed #{0}".format(feed_id))
        feed = Feed.objects.get(pk=feed_id)
        parsed_feed = feedparser.parse(feed.feed_url)
        for entry in parsed_feed.entries:
            # Parse out the entry, handling all the fun stuff that feeds can do.
            title = entry.title
            logger.debug("Title: {0}".format(title))
            guid = entry.get("id", entry.link)
            link = entry.link

            if not guid:
                guid = link
                        
            if hasattr(entry, "summary"):
                content = entry.summary
            elif hasattr(entry, "content"):
                content = entry.content[0].value
            elif hasattr(entry, "description"):
                content = entry.description
            else:
                content = u""

            try:
                if entry.has_key('modified_parsed'):
                    date_modified = datetime.datetime.fromtimestamp(time.mktime(entry.modified_parsed))
                elif parsed_feed.feed.has_key('modified_parsed'):
                    date_modified = datetime.datetime.fromtimestamp(time.mktime(parsed_feed.feed.modified_parsed))
                elif parsed_feed.has_key('modified'):
                    date_modified = datetime.datetime.fromtimestamp(time.mktime(parsed_feed.modified))
                else:
                    date_modified = datetime.datetime.now()
            except TypeError:
                date_modified = datetime.datetime.now()
            
            FeedItem.objects.create_or_update_by_guid(guid,
                feed = feed,
                title = title,
                link = link,
                summary = content,
                date_modified = date_modified
            )
tasks.register(UpdateFeed)