Beispiel #1
0
def add_new_posts(last_updated=None):
    """Run this on a cron"""
    for blog in Blog.objects.all():
        try:
            document = feedparser.parse(blog.feed_url)
        except:
            print "error parsing"
            continue

        if last_updated is None:
            print("- Adding %i articles from %s" % (len(document['entries']), blog.title))

            for entry in document['entries']:
                # now we create a new post
                post = Post()
                post.blog = blog
                post.title = entry['title']

                if 'summary' in entry:
                    post.content = entry['summary']
                if 'content' in entry:
                    post.content = entry['content']

                post.link = entry['link']
                post.save()
            else:
                # TODO: only parse from a date
                pass
Beispiel #2
0
    def handle(self, *args, **kwargs):
        for feed in Feed.objects.all():
            parsed = feedparser.parse(self.construct_feed_url(feed))
            icon = self.prep_feedname(feed.name) + "." + feed.favicon_ext
            newcount = 0

            if not self.have_logo(feed):
                self.save_logo(feed)

            for entry in parsed.entries:
                if Post.objects.filter(post_id=entry.id).exists():
                    continue

                dt = entry.updated_parsed or entry.published_parsed

                if dt:
                    updated = datetime.datetime(dt.tm_year, dt.tm_mon, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec)
                else:
                    updated = datetime.datetime.now()

                if "content" in entry.keys():
                    content = entry.content[0]["value"]
                else:
                    content = entry.summary

                if feed.uses_markdown:
                    content = markdown.markdown(content)

                if feed.convert_newlines:
                    content = re.sub("\n", "</br>\n", content)

                post = Post(
                    post_id=entry.id,
                    title=entry.title if feed.uses_titles else "",
                    link=entry.link,
                    updated=updated,
                    icon=icon,
                    content=content,
                    feed=feed,
                )

                post.save()
                newcount += 1

            print "Grabbed %d new posts from %s" % (newcount, feed.name)