Last active
December 17, 2015 18:09
-
-
Save np1/5651109 to your computer and use it in GitHub Desktop.
Parse YouTube channel RSS feed data without additional libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import urllib2 | |
import time | |
from xml.etree import ElementTree as etree | |
class YTFeedParser(list): | |
def __init__(self, url): | |
rawfeeddata = urllib2.urlopen(url).read() | |
tree = etree.fromstring(rawfeeddata) | |
items = tree.findall('channel/item') | |
self.items = [] | |
formatstring = "%a, %d %b %Y %H:%M:%S +0000" | |
for item in items: | |
d = dict() | |
for tag in "pubDate title link author".split(" "): | |
d[tag] = item.find(tag).text | |
d['parsed_date'] = time.strptime(d['pubDate'], formatstring) | |
self.append(d) | |
# example usage | |
link = ('http://gdata.youtube.com/feeds/base/users/LetsPlay/uploads?alt=rss&v=' | |
'2&orderby=published&client=ytapi-youtube-profile') | |
feed = YTFeedParser(link) | |
for feeditem in feed: | |
for k, v in feeditem.items(): | |
print k, ":", v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment