]>
code.delx.au - monosys/blob - scripts/youtube.cgi
4 from lxml
. html
import document_fromstring
13 urllib
. URLopener
. version
= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
16 ( 38 , ".mp4" ), # 4096x3072
17 ( 37 , ".mp4" ), # 1920x1080
18 ( 22 , ".mp4" ), # 1280x720
19 ( 45 , ".webm" ), # 1280x720
20 ( 43 , ".webm" ), # 640x360
21 ( 35 , ".flv" ), # 854x480
22 ( 34 , ".flv" ), # 640x360
23 ( 18 , ".mp4" ), # 480x360
24 ( 5 , ".flv" ), # 400x240
25 ( 17 , ".3gp" ), # 176x144
29 def print_form ( url
= "" , msg
= "" ):
30 print "Content-Type: application/xhtml+xml \r\n\r\n "
32 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
33 <html xmlns="http://www.w3.org/1999/xhtml">
35 <title>delx.net.au - YouTube Scraper</title>
36 <link rel="stylesheet" type="text/css" href="/style.css"/>
37 <style type="text/css">
47 <h1>delx.net.au - YouTube Scraper</h1>
49 <form action="" method="get">
50 <p>This page will let you easily download YouTube videos to watch offline. It
51 will automatically grab the highest quality version.</p>
52 <div><input type="text" name="url" value=" {1} "/></div>
53 <div><input type="submit" value="Download!"/></div>
55 <p>Tip! Use this bookmarklet: <a href="javascript:(function(){window.location='http://delx.net.au/utils/youtube.cgi?url='+escape(location);})()">YouTube Download</a>
56 to easily download videos. Right-click the link and add it to bookmarks,
57 then when you're looking at a YouTube page select that bookmark from your
58 browser's bookmarks menu to download the video straight away.</p>
61 """ . replace ( " {0} " , msg
). replace ( " {1} " , url
)
64 f
= urllib
. urlopen ( url
)
65 doc
= document_fromstring ( f
. read ())
69 def get_video_url ( doc
):
70 embed
= doc
. xpath ( "//embed" )[ 0 ]
71 flashvars
= embed
. attrib
[ "flashvars" ]
72 flashvars
= cgi
. parse_qs ( flashvars
)
74 for pair
in flashvars
[ "fmt_url_map" ][ 0 ]. split ( "," ):
75 key
, value
= pair
. split ( "|" )
77 fmt_url_map
[ key
] = value
78 for fmt
, extension
in fmt_quality
:
80 video_url
= fmt_url_map
[ fmt
]
85 return None , None , None
87 title
= doc
. xpath ( "/html/head/title/text()" )[ 0 ]
88 title
= re
. sub ( "\s+" , " " , title
. strip ())
89 valid_chars
= frozenset ( "-_.() abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
90 filename
= "" . join ( c
for c
in title
. encode ( "ascii" , "ignore" ) if c
in valid_chars
)
93 return video_url
, filename
100 print_form ( url
= "http://www.youtube.com/watch?v=FOOBAR" )
105 video_url
, filename
= get_video_url ( doc
)
106 data
= urllib
. urlopen ( video_url
)
107 httpinfo
= data
. info ()
108 sys
. stdout
. write ( "Content-Disposition: attachment; filename= \" %s \"\r\n " % filename
)
109 sys
. stdout
. write ( "Content-Length: %s \r\n " % httpinfo
. getheader ( "Content-Length" ))
110 sys
. stdout
. write ( " \r\n " )
111 shutil
. copyfileobj ( data
, sys
. stdout
)
116 msg
= "<p class='error'>Sorry, there was an error. Check your URL?</p>"
124 print >> sys
. stderr
, "Usage: %s http://youtube.com/watch?v=FOOBAR" % sys
. argv
[ 0 ]
127 video_url
, filename
= get_video_url ( doc
)
128 data
= urllib
. urlopen ( video_url
)
129 outfile
= open ( filename
, "w" )
130 shutil
. copyfileobj ( data
, outfile
)
135 if __name__
== "__main__" :
136 if os
. environ
. has_key ( "SCRIPT_NAME" ):