pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

kahrn private pastebin - collaborative debugging tool What's a private pastebin?


Posted by kahrn on Sun 28 Sep 17:25
report abuse | download | new post

  1. #!/usr/bin/env python
  2. # youtube-dl-simple
  3. # An adaption of the 'youtube-dl' script by Ricardo Garcia Gonzalez.
  4. # Slight adaptions by Ricky Hewitt, Original by Ricardo Garcia Gonzalez.
  5. # This adaption upon the original is merely intended to convert the URL, not download it.
  6. # License: Public domain code
  7.  
  8. import urllib2, sys, re
  9.  
  10. std_headers = { 
  11.         'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1',
  12.         'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  13.         'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
  14.         'Accept-Language': 'en-us,en;q=0.5',
  15. }
  16.  
  17. def _real_extract(url):
  18.     _VALID_URL = r'^((?:http://)?(?:\w+\.)?youtube\.com/(?:(?:v/)|(?:(?:watch(?:\.php)?)?\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
  19.    
  20.     # Extract video id from URL
  21.     mobj = re.match(_VALID_URL, url)
  22.     if mobj is None:
  23.         print u'ERROR: invalid URL: %s' % (url)
  24.         return [None]
  25.     video_id = mobj.group(2)
  26.  
  27.     # Normalize URL, including format
  28.     normalized_url = 'http://www.youtube.com/watch?v=%s' % video_id
  29.     request = urllib2.Request(normalized_url, None, std_headers)
  30.     try:
  31.         video_webpage = urllib2.urlopen(request).read()
  32.     except (urllib2.URLError, httplib.HTTPException, socket.error), err:
  33.         print u'ERROR: unable to download video webpage: %s' % (str(err))
  34.         return [None]
  35.    
  36.     # "t" param
  37.     mobj = re.search(r', "t": "([^"]+)"', video_webpage)
  38.     if mobj is None:
  39.         print u'ERROR: unable to extract "t" parameter. Cannot download!'
  40.         return [None]
  41.     video_real_url = 'http://www.youtube.com/get_video?video_id=%s&t=%s' % (video_id, mobj.group(1))
  42.     return video_real_url
  43.    
  44. if __name__=="__main__":
  45.     import os
  46.    
  47.     if len(sys.argv) == 2:
  48.         print _real_extract(sys.argv[1])
  49.     else:
  50.         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.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post