Source

feedzilla / feedzilla / management / commands / feedzilla_update.py

# -*- coding: utf-8 -*-
import logging
from datetime import datetime
import re

from django.core.management.base import BaseCommand
from django.conf import settings

from feedzilla.util.parse import parse_feed
from feedzilla.models import Feed, Post
from feedzilla import settings

class Command(BaseCommand):
    help = u'Update feeds'

    def handle(self, *args, **kwargs):
        logging.basicConfig(level=logging.DEBUG)

        processors = []
        for path in settings.FEEDZILLA_POST_PROCESSORS:
            module_path, cls = path.rsplit('.', 1)
            module = __import__(module_path, globals(), locals(), ['foo'])
            proc = getattr(module, cls)()
            processors.append(proc)

        qs = Feed.objects.filter(active=True)
        if len(args):
            qs = qs.filter(feed_url__icontains=args[0])

        for feed in qs:
            logging.debug('parsing %s' % feed.feed_url)

            resp = parse_feed(feed.feed_url, etag=feed.etag,
                              summary_size=settings.FEEDZILLA_SUMMARY_SIZE)
            if not resp['success']:
                logging.debug('Failure')
            else:
                new_posts = 0
                for entry in resp['entries']:

                    try:
                        Post.objects.get(guid=entry['guid'])
                    except Post.DoesNotExist:

                        tags = entry['tags']
                        if settings.FEEDZILLA_TAGS_LOWERCASE:
                            tags = set(x.lower() for x in tags)
                        # Sum of tags lengths should not be
                        # more than 255 chars
                        newtags = []
                        size = 0
                        for tag in tags:
                            size += len(tag)
                            if size <= 255:
                                newtags.append(tag)
                        tags = newtags

                        post = Post(
                            feed=feed,
                            tags=', '.join(tags),
                            title=entry['title'],
                            content=entry['content'],
                            summary=entry['summary'],
                            link=entry['link'],
                            guid=entry['guid'],
                            created=entry['created']
                        )
                        post.save()

                        # Remember post details
                        post_snapshot = post.__dict__

                        for proc in processors:
                            proc.process(post)

                        # If post was changed by some processor
                        # then save changes
                        if post.__dict__ != post_snapshot:
                            post.save()

                        new_posts += 1
                logging.debug('New posts: %d' % new_posts)

            feed.last_checked = datetime.now()
            feed.save()
            feed.update_counts()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.