2 # Copyright Michael Orlitzky
4 # http://michael.orlitzky.com/
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # http://www.fsf.org/licensing/licenses/gpl.html
22 class Youtube
< Website
24 VALID_YOUTUBE_URL_REGEX
= /^(http:\/\
/)?([a-z0-9]+\.)?youtube\.com\/((watch
\?v
=)|(v\
/)|([a-z]+\#[a-z]\/[a-z
]\
/[0-9]\/))[a-z0-9_\
-]+
(\
&.*)?\
#?$/i
26 def self.owns_url
?(url
)
27 return url
=~ VALID_YOUTUBE_URL_REGEX
34 # The @format variable just caches the format of the video we're
35 # downloading. Storing it will prevent us from having to calculate
42 video_id
= self.parse_video_id()
44 # The video's URL (the "page data" URL) may be different from the
45 # URL that was passed to the program. We support the /v/video_id
46 # URL format, but that is *not* the main video page where we can
47 # retrieve the "t" parameter. We can only get that from the
48 # /watch?v=video_id form.
49 page_data_url
= "http://www.youtube.com/watch?v=#{video_id}"
50 page_data
= self.get_page_data(page_data_url
)
53 # Get the URL map from the page.
54 fmt_url_map
= get_format_url_map(page_data
)
56 # Figure out which formats are available, and if any are,
57 # choose the best one.
58 available_formats
= fmt_url_map
.keys()
59 desired_format
= get_desired_format(available_formats
)
61 # First we cache the format so that when we're asked for the
62 # video filename later, we don't have to recompute the format.
63 @format = desired_format
65 # And then use whatever URL is available for the desired format.
66 # We assume that all available formats will have an entry in the
68 video_url
= fmt_url_map
[desired_format
]
71 rescue StandardError
=> e
72 # If at first you do not succeed, maybe someone decided to
73 # change some shit. This alternate method parses
74 # url_encoded_fmt_stream_map.
75 fmt_streams
= get_fmt_stream_list(page_data
)
76 video_url
= self.unicode_unescape(fmt_streams
[0])
77 video_url
= CGI
::unescape(video_url
)
79 # Strip off everything after the first space in the URL.
80 # I don't know why this works, but if we leave the space
81 # in (encoded, even), Youtube throws us 403 errors.
82 video_url
.gsub!
(/ .+$/, '')
89 def get_video_filename()
90 # The format -> extension mapping is available on Wikipedia:
92 # http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs
94 # The default extension is .flv.
97 if [18, 22, 35, 37].include?(@format)
103 return (self.parse_video_id() + extension
)
109 def unicode_unescape(string
)
110 # Unescape sequences like '\u0026'.
111 # Ok, only '\u0026' for now.
112 return string
.gsub('\u0026', '&')
115 def get_fmt_stream_list(page_data
)
116 # This is another (new?) method of embedding the video URLs.
117 # The url_encoded_fmt_stream_map variable contains a list of URLs
118 # in the form url=foo1,url=foo2...
120 # It looks like the first one in the list is the highest
121 # quality? Let's just take that one for now.
122 fmt_stream_regex
= /\"url_encoded_fmt_stream_map\": \"(.+?)\"/
124 matches
= fmt_stream_regex
.match(page_data
)
126 if (matches
.nil? || matches
.length
< 2)
127 raise StandardError
.new("Could not parse the url_encoded_fmt_stream_map Flash variable.")
130 urlstring
= matches
[1]
131 urlstring
.gsub!
('url=', '')
132 urls
= urlstring
.split(',')
137 # Get the video id from the URL. Should be relatively easy,
138 # unless Youtube supports some URL formats of which I'm unaware.
140 # Both URLs are fairly easy to parse if you handle
141 # them one at a time. The only tricky situation is when
142 # parameters like "&hl=en" are tacked on to the end.
143 # We'll call /watch?v=video_id the "first form."
144 first_form_video_id_regex
= /v=([0-9a-z_\-]+)/i
145 first_form_matches
= first_form_video_id_regex
.match(@url)
146 return first_form_matches
[1] if not (first_form_matches
.nil? ||
147 first_form_matches
.length
< 2)
149 # First form didn't work? Try the second.
150 second_form_video_id_regex
= /\/v\
/([0-9a-z_\-]+)/i
151 second_form_matches
= second_form_video_id_regex
.match(@url)
152 return second_form_matches
[1] if not (second_form_matches
.nil? ||
153 second_form_matches
.length
< 2)
156 third_form_video_id_regex
= /\/([[:alnum:]]+
)$/i
157 third_form_matches
= third_form_video_id_regex
.match(@url)
158 return third_form_matches
[1] if not (third_form_matches
.nil? ||
159 third_form_matches
.length
< 2)
161 # If we made it here, we couldn't figure out the video id. Yes,
162 # this is fatal, since we don't know where the video file is
164 raise StandardError
.new("Could not parse the video id.")
169 def get_format_url_map(page_data
)
170 # Youtube has implemented a new fmt_url_map that (perhaps
171 # unsurprisingly) maps formats to video URLs. This makes it
172 # easyish to parse the video URLs.
174 url_map_regex
= /fmt_url_map=([^&\"]+)/
176 matches
= url_map_regex
.match(page_data
)
178 if (matches
.nil? || matches
.length
< 1)
179 raise StandardError
.new("Could not parse the fmt_url_map Flash variable.")
182 # The map is stored entirely in one Flash variable. The format is
183 # key|value,key|value,...
184 maptext
= CGI
::unescape(matches
[1])
185 entries
= maptext
.split(',')
186 entries
.each
do |entry
|
187 key
= entry
.split('|')[0].to_i
188 value
= entry
.split('|')[1]
192 if (url_map
.length
< 1)
193 raise StandardError
.new("Could not find any valid format URLs.")
200 def get_desired_format(available_formats
)
201 # Check for the presence of formats, in order of preference
202 # (quality). That is, we check for the best formats first. As soon
203 # as a format is found to be available, we return it as the
204 # desired format, since the first format we find is going to be
205 # the best available format.
206 return 37 if available_formats
.include?(37)
207 return 22 if available_formats
.include?(22)
208 return 35 if available_formats
.include?(35)
209 return 18 if available_formats
.include?(18)
210 return 34 if available_formats
.include?(34)
211 return 17 if available_formats
.include?(17)
213 # Available formats can't be empty (we would have raised an error
214 # in get_available_formats), so if there's some unknown format
215 # here we might as well return it as a last resort.
216 return available_formats
[0]