]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/youtube.rb
c124f5339177f41c5a9360cde51700464d28be87
[dead/whatever-dl.git] / src / websites / youtube.rb
1 #
2 # Copyright Michael Orlitzky
3 #
4 # http://michael.orlitzky.com/
5 #
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.
10 #
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.
15 #
16 # http://www.fsf.org/licensing/licenses/gpl.html
17 #
18
19 require 'src/website'
20
21 # Needed to download the page, which is in turn
22 # needed because it contains the video URL.
23 require 'net/http'
24 require 'uri'
25
26
27 class Youtube < Website
28
29 VALID_YOUTUBE_URL_REGEX = /^(http:\/\/)?([a-z0-9]+\.)?youtube\.com\/((watch\?v=)|(v\/))[a-z0-9_\-]+(\&.*)?\#?$/i
30
31 def self.owns_url?(url)
32 return url =~ VALID_YOUTUBE_URL_REGEX
33 end
34
35
36 def get_video_url()
37 video_id = self.parse_video_id()
38
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)
46
47 # Magic.
48 t_parameter = self.parse_t_parameter(page_data)
49
50 video_url = "http://www.youtube.com/get_video?video_id=#{video_id}&t=#{t_parameter}"
51
52 return video_url
53 end
54
55
56 def get_video_filename()
57 return (self.parse_video_id() + '.flv')
58 end
59
60
61 protected;
62
63 # Get the video id from the URL. Should be relatively easy,
64 # unless Youtube supports some URL formats of which I'm unaware.
65 def parse_video_id()
66 # Return nil if we get no matches below.
67 video_id = nil
68
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)
77
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)
83
84 return video_id
85 end
86
87
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)
92 t_parameter = nil
93
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)
97
98 return t_parameter
99 end
100
101
102 end