Posted by kahrn on Sun 28 Sep 17:25
report abuse | download | new post
- #!/usr/bin/env python
- # youtube-dl-simple
- # An adaption of the 'youtube-dl' script by Ricardo Garcia Gonzalez.
- # Slight adaptions by Ricky Hewitt, Original by Ricardo Garcia Gonzalez.
- # This adaption upon the original is merely intended to convert the URL, not download it.
- # License: Public domain code
- import urllib2, sys, re
- std_headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1',
- 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
- 'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
- 'Accept-Language': 'en-us,en;q=0.5',
- }
- def _real_extract(url):
- _VALID_URL = r'^((?:http://)?(?:\w+\.)?youtube\.com/(?:(?:v/)|(?:(?:watch(?:\.php)?)?\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
- # Extract video id from URL
- mobj = re.match(_VALID_URL, url)
- if mobj is None:
- print u'ERROR: invalid URL: %s' % (url)
- return [None]
- video_id = mobj.group(2)
- # Normalize URL, including format
- normalized_url = 'http://www.youtube.com/watch?v=%s' % video_id
- request = urllib2.Request(normalized_url, None, std_headers)
- try:
- video_webpage = urllib2.urlopen(request).read()
- except (urllib2.URLError, httplib.HTTPException, socket.error), err:
- print u'ERROR: unable to download video webpage: %s' % (str(err))
- return [None]
- # "t" param
- mobj = re.search(r', "t": "([^"]+)"', video_webpage)
- if mobj is None:
- print u'ERROR: unable to extract "t" parameter. Cannot download!'
- return [None]
- video_real_url = 'http://www.youtube.com/get_video?video_id=%s&t=%s' % (video_id, mobj.group(1))
- return video_real_url
- if __name__=="__main__":
- import os
- if len(sys.argv) == 2:
- print _real_extract(sys.argv[1])
- else:
- print "Usage: " + sys.argv[0][sys.argv[0].rfind("\/")+1:len(sys.argv[0])] + " url"
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.