Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created January 27, 2011 17:43
Show Gist options
  • Save Garciat/798870 to your computer and use it in GitHub Desktop.
Save Garciat/798870 to your computer and use it in GitHub Desktop.
YouTube subscriptions feed parser/simplifier. mod_wsgi version
#! /usr/bin/env python
from time import gmtime, strftime
import urllib2 as urllib
import re
from BeautifulSoup import BeautifulSoup, Tag
###
user = 'gbrlgrct'
###
feed_url = 'http://gdata.youtube.com/feeds/base/users/%s/newsubscriptionvideos?client=ytapi-youtube-user&v=2'
base = '<?xml version="1.0" encoding="UTF-8" ?>\
<rss version="2.0">\
<channel></channel>\
</rss>'
class Subscriptions(object):
def __init__(self, user):
global feed_url
self.user = user
self.feed_url = feed_url % user
def out(self):
global base
# self-closing category tags make BeautifulSoup act weird
a = BeautifulSoup(re.sub('<category[^>]*>', '', urllib.urlopen(self.feed_url).read()))
b = BeautifulSoup(base)
b.rss.channel.append('<title>YouTube feed for %s</title>' % self.user)
b.rss.channel.append('<description></description>')
b.rss.channel.append('<link>%s</link>' % self.feed_url.replace('&', '&amp;'))
b.rss.channel.append('<lastBuildDate>%s</lastBuildDate>' % strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
items = a.findAll('entry')
for item in items:
new = Tag(b, 'item')
b.rss.channel.append(new)
new.append('<title>[%s] %s</title>' % (item.find('name').renderContents(), item.find('title').renderContents()))
new.append('<description></description>')
new.append('<link>'+item.find('link', rel='alternate')['href'].split('&')[0]+'</link>')
new.append('<guid isPermaLink="false">%s</guid>' % item.find('id').renderContents())
# Too lazy to convert date to TFC-822
new.append('<pubDate>%s</pubDate>' % item.find('published').renderContents())
return b.prettify()
def application(environ, respond):
global user
out = Subscriptions(user).out()
respond('200 OK', [('Content-Type', 'application/rss+xml')])
return [out]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment