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
21 # Needed to download the page, which is in turn
22 # needed because it contains the video URL.
27 class Youtube
< Website
29 VALID_YOUTUBE_URL_REGEX
= /^(http:\/\
/)?([a-z0-9]+\.)?youtube\.com\/((watch
\?v
=)|(v\
/))[a-z0-9_\-]+(\&.*)?\#?$/i
31 def self.owns_url
?(url
)
32 return url
=~ VALID_YOUTUBE_URL_REGEX
37 video_id
= self.parse_video_id()
39 # The video's URL (the "page data" URL) may be different from the
40 # URL that was passed to the program. We support the /v/video_id
41 # URL format, but that is *not* the main video page where we can
42 # retrieve the "t" parameter. We can only get that from the
43 # /watch?v=video_id form.
44 page_data_url
= "http://www.youtube.com/watch?v=#{video_id}"
45 page_data
= self.get_page_data(page_data_url
)
48 t_parameter
= self.parse_t_parameter(page_data
)
50 video_url
= "http://www.youtube.com/get_video?video_id=#{video_id}&t=#{t_parameter}"
56 def get_video_filename()
57 return (self.parse_video_id() +
'.flv')
63 # Get the video id from the URL. Should be relatively easy,
64 # unless Youtube supports some URL formats of which I'm unaware.
66 # Return nil if we get no matches below.
69 # Both URLs are fairly easy to parse if you handle
70 # them one at a time. The only tricky situation is when
71 # parameters like "&hl=en" are tacked on to the end.
72 # We'll call /watch?v=video_id the "first form."
73 first_form_video_id_regex
= /v=([0-9a-z_\-]+)/i
74 first_form_matches
= first_form_video_id_regex
.match(@url)
75 return first_form_matches
[1] if not (first_form_matches
.nil? ||
76 first_form_matches
.length
< 2)
78 # First form didn't work? Try the second.
79 second_form_video_id_regex
= /\/v\
/([0-9a-z_\-]+)/i
80 second_form_matches
= second_form_video_id_regex
.match(@url)
81 video_id
= second_form_matches
[1] if not (second_form_matches
.nil? ||
82 second_form_matches
.length
< 2)
88 # Parse out the "t" parameter from the video's page. I'm not sure
89 # what "t" stands for, but it's located in some JSON, and is required
90 # for the final video URL to work.
91 def parse_t_parameter(page_data
)
94 t_parameter_regex
= /\"t\"\:[[:space:]]\"([^\"]+?)\"/
95 matches
= t_parameter_regex
.match(page_data
)
96 t_parameter
= matches
[1] if not (matches
.nil? || matches
.length
< 2)