Esempio n. 1
0
def create_feed_from_url(url):
    """ Query the given URL, parse the results and create a Feed object. If
        the response isn't RSS then throw an error """
    f = feedparser.parse(url)

    # Handle obvious errors
    if f.bozo:
        handle_bozo_exception(f.bozo_exception)

    if 'status' in f and f.status in (200, 301, 302):
        # If we received a bozo exception raise it
        if f.bozo:
            handle_bozo_exception(f.bozo_exception)

        # If the resource has permanently moved update it's URL
        feed_url = url
        if f.status == 301:
            feed_url = f.href

        feed = Feed(parent=root_key(), name=f.channel.title, url=feed_url)
        if len(f.entries) > 0:
            feed.date_of_last_entry = datetime.fromtimestamp(
                mktime(f.entries[0].updated_parsed))
        return feed
    else:
        raise Exception("Unexpected result from feedparser.parse(): %s" % f)