Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created September 29, 2014 08:31
Show Gist options
  • Save boxdot/c57f7855fc065c025631 to your computer and use it in GitHub Desktop.
Save boxdot/c57f7855fc065c025631 to your computer and use it in GitHub Desktop.
Generates a m3u playlist from the 4players video newsfeed. Already processed videos are cached. New videos are labeled with "NEW" prefix.
#!/usr/bin/env python
import feedparser, re, urllib2, os, sys
CACHEDIR = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'cache')
VIDEOFEEDURL = 'http://feeds.4players.de/Allgemein/videos/-/rss.xml'
def formatted(s):
return re.sub(u'[^\w]+', u'-', s.strip(), 0, re.UNICODE).lower()
print '#EXTM3U'
f = feedparser.parse(VIDEOFEEDURL)
for e in f['entries']:
title = e['title']
summary = e['summary']
m = re.search(r'href=[\'"]?[^\'" >]+redir=([^\'" >]+)[\'" >]+Weiter zum Video', summary)
if not m:
print "#error: no video link for '%s' found" % title.encode('utf-8')
continue
url = urllib2.unquote(m.group(1))
# cache
if not os.path.isdir(CACHEDIR):
os.mkdir(CACHEDIR)
filename = formatted(title)
fullname = os.path.join(CACHEDIR, filename)
try:
with open(fullname) as f:
page = ''.join(f.readlines())
except IOError:
title = "NEW: %s" % title
page = urllib2.urlopen(url).read()
with open(fullname, 'w') as f:
f.write(page)
# find video url
m = re.search(r'<meta.*swf\?file=([^&]+)', page)
if not m:
print "#error: video url for '%s' is not found" % title.encode('utf-8')
continue
video_url = m.group(1)
title = re.sub(u'[,-]', u' ', title, 0, re.UNICODE)
print '#EXTINF:0,%s' % title.encode('utf-8')
print video_url.encode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment