Example #1
0
def update_feed(request):
    """
    Fetch a feed and sync each item with the database.
    """
    # Fetch the url
    url = request.GET['url']
    content = fetch(url).content
    d = feedparser.parse(StringIO.StringIO(content))
    # Loop through all the items
    for entry in d.entries:
        # See if this link already exists
        story_query = Story.all()
        story = story_query.filter('link =', entry.id).get()
        # And if it doesn't ...
        if not story:
            # Create a new Story object
            story = Story(
                link = entry.id,
                title = entry.title,
                updated_date = datetime.fromtimestamp(time.mktime(entry.updated_parsed)), 
            )
            # Prep the authors
            authors = entry.author.split(',')
            author_keys = []
            # Loop through the authors
            for author in authors:
                # Check if the author already exists
                this_slug = str(slugify(author))
                if not this_slug:
                    continue
                a = Author.get_by_key_name(this_slug)
                # If it does...
                if a:
                    # Sync updates
                    if story.updated_date > a.last_updated:
                        a.last_updated = story.updated_date
                        a.put()
                # Otherwise...
                else:
                    # Create a new Author obj
                    a = Author(
                        key_name = this_slug,
                        name = author,
                        slug = this_slug,
                        story_count = 1,
                        last_updated = story.updated_date
                    )
                    a.put()
                # Add this to the Author key list
                author_keys.append(a.key())
            # Add the author keys to the story object
            story.bylines = author_keys
            # Save the story
            story.put()
            # Schedule total updates for all the authors
            [taskqueue.add(
                url = '/_update_story_count_for_author/',
                params = {'key' : i},
                method='GET'
            ) for i in author_keys]
    return HttpResponse('ok!')