Created
September 8, 2011 12:01
-
-
Save adammw/1203235 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/env python | |
| import xml.dom.minidom | |
| import feedparser | |
| import re | |
| import subprocess | |
| import sys | |
| import os | |
| import urllib2 | |
| PLAYLIST_ID = sys.argv[1] | |
| RSS_FEED_URL = "http://www.mtv.com/player/embed/AS3/fullepisode/rss/" | |
| SWF_URL = "http://media.mtvnservices.com/player/release/" | |
| def remove_html_tags(data): | |
| p = re.compile(r'<.*?>') | |
| return p.sub('', data) | |
| def getText(nodelist): | |
| rc = [] | |
| for node in nodelist: | |
| if node.nodeType == node.TEXT_NODE: | |
| rc.append(node.data) | |
| return ''.join(rc) | |
| rss = feedparser.parse(RSS_FEED_URL + "?id=" + PLAYLIST_ID) | |
| print remove_html_tags(rss.feed.title) | |
| for i in range(0,len(rss.entries)): | |
| media = xml.dom.minidom.parseString(urllib2.urlopen(urllib2.Request(rss.entries[i].media_content[0]['url'], headers={})).read()) | |
| renditions = media.getElementsByTagName("rendition") | |
| rendition = renditions[len(renditions)-1] | |
| #for rendition in renditions: | |
| #print rendition.attributes['width'].value + 'x' + rendition.attributes['height'].value | |
| rtmp_url = remove_html_tags(rendition.getElementsByTagName("src")[0].toxml()) | |
| filename = rtmp_url.rsplit('/',1)[1].rsplit('.',1)[0] + '.flv' | |
| args = ['rtmpdump','-o',filename,'-r',rtmp_url,'-W',SWF_URL] | |
| if os.path.exists(filename): | |
| args.append('--resume') | |
| process = subprocess.Popen(args, stderr=subprocess.PIPE)# | |
| linebuf = "" | |
| while process.returncode is None: | |
| char = process.stderr.read(1) | |
| if char == "\n" or char == "\r": | |
| if re.match('ERROR', linebuf): | |
| print linebuf | |
| sys.exit(1) | |
| if re.match('.+ kB / .+ sec', linebuf): | |
| sys.stdout.write("\r[%i/%i] Downloading... %s" % (i+1, len(rss.entries), linebuf)) | |
| if re.match('Download complete', linebuf): | |
| sys.stdout.write("\r[%i/%i] Download complete \n" % (i+1, len(rss.entries))) | |
| if process.returncode is None: | |
| try: | |
| process.kill() | |
| except: | |
| pass | |
| process.wait() | |
| break | |
| linebuf = "" | |
| else: | |
| linebuf += char | |
| #print "return code", process.returncode | |
| #sys.stdout.write("\r[%i/%i] Downloading..." % (i+1, len(rss.entries))) | |
| sys.stdout.write("\rAll %i Parts Downloaded." % len(rss.entries)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment