Sunday, February 12, 2012

Using FeedParser.py to Get Feed Info From a Website.

This is what I'm talking about with the documentation. I wanted to open the url and get the information from it. But how do you do that? I tried urllib2.urlopen(url[, data][, timeout]), but that didn't work. FeedParser.py comes with a nice example program - and that is what I shamelessly copied.


Now, when you press the button, this program reads the feed information and prints it out.

Next step will be to populate a text box or combo box with the data from the feed.

# -*- coding: utf-8 -*-
from generic2 import *
import urllib2, sys, feedparser

#List of uples (label, property-tag, truncation)
COMMON_CHANNEL_PROPERTIES = [
    ('Channel title:', 'title', None),
    ('Channel description:', 'description', 100),
    ('Channel URL:', 'link', None),
]

COMMON_ITEM_PROPERTIES = [
    ('Item title:', 'title', None),
    ('Item description:', 'description', 100),
    ('Item URL:', 'link', None),
]

INDENT = u' '*4


app=Application()
app.master.title("Hello There")
app.master.geometry("600x400+20+20")

#GLOBALS
url1="http://feeds.feedburner.com/antiwar-radio"
data=""

def feedinfo(url, output=sys.stdout):
    """
    Read an RSS or Atom feed from the given URL and output a feed
    report with all the key data
    """
    feed_data = feedparser.parse(url)
    channel, items = feed_data.feed, feed_data.entries
    #Display core feed data
    for label, prop, trunc in COMMON_CHANNEL_PROPERTIES:
        value = channel[prop]
        if trunc:
            value = value[:trunc] + u'...'
        print >> output, label, value
    print >> output
    print >> output, "Feed items:"
    for item in items:
        for label, prop, trunc in COMMON_ITEM_PROPERTIES:
            value = item[prop]
            if trunc:
                value = value[:trunc] + u'...'
            print >> output, INDENT, label, value
        print >> output, INDENT, u'---'
    return

def process(self):
     pass
     print ("process activated")
     print (url1)
     feedinfo(url1)
     print (data)


app.button1.bind("<Button-1>", process)

app.update()

app.mainloop()

UPDATE:
This is how it works for me. The guys who wrote feedparser.py and the example did a great job. And once I realized that feedparser results in a dictionary type, it was fairly easy to extract the data I wanted.

Here's the revised program:


from generic2 import *
import urllib2, sys, feedparser

app=Application()
app.master.title("Hello There")
app.master.geometry("600x400+20+20")

#GLOBALS
url1="http://feeds.feedburner.com/antiwar-radio"
data=""

def feedinfo(url, output=sys.stdout):

    feed_data = feedparser.parse(url)
    channel, items = feed_data.feed, feed_data.entries


    print("Feed Title: ",channel['title'])

    for item in items:
        print( "Title: ",item['title'])

    return

def process(self):

     pass
     print ("process activated")
     print (url1)
     feedinfo(url1)
     print (data)

app.button1.bind("<Button-1>", process)

app.update()

app.mainloop()

No comments:

Post a Comment