]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/vimeo.rb
3836df1ae0edff133dc70a599a2a1951353c68bd
[dead/whatever-dl.git] / src / websites / vimeo.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 video XML file, which is in turn
22 # needed because it contains the junk we need to construct
23 # the video URL.
24 require 'net/http'
25 require 'uri'
26
27
28 class Vimeo < Website
29
30 VALID_VIMEO_URL_REGEX = /^(http:\/\/)?(www\.)?vimeo\.com\/(moogaloop\.swf\?clip_id=)?(\d+)/
31
32 def self.owns_url?(url)
33 return url =~ VALID_VIMEO_URL_REGEX
34 end
35
36
37 def get_video_url(url)
38 # First, figure out the video id from the URL.
39 # Then, use the video id to construct the video details URL.
40 # Get the video details page, and parse the resulting XML for
41 # the junk we need to construct the video URL. Note that the file
42 # URL given in the XML is *not* valid.
43 video_id = parse_video_id(url)
44 details_url = "http://www.vimeo.com/moogaloop/load/clip:#{video_id}/local"
45 details_data = get_page_data(details_url)
46
47
48 request_signature = parse_request_signature(details_data)
49 request_signature_expires = parse_request_signature_expires(details_data)
50 quality = parse_quality(details_data)
51
52 # Being slightly explicit about what we're doing here...
53 video_url = "http://www.vimeo.com/moogaloop/play/clip:#{video_id}/#{request_signature}/#{request_signature_expires}/?q=#{quality}"
54
55 return video_url
56 end
57
58
59 protected;
60
61 def parse_video_id(url)
62 video_id = nil
63
64 # First, try to get the video id from the end of the URL.
65 video_id_regex = /\/(\d+)$/
66 matches = video_id_regex.match(url)
67
68 if matches.nil?
69 # If that didn't work, the URL must be of the clip_id= form.
70 video_id_regex = /clip_id\=(\d+)/
71 matches = video_id_regex.match(url)
72 video_id = matches[1] if not (matches.nil? || matches.length < 1)
73 else
74 video_id = matches[1] if not (matches.nil? || matches.length < 1)
75 end
76
77 return video_id
78 end
79
80
81 def parse_request_signature(page_data)
82 # It's XML.
83 rs_regex = /<request_signature>(.*?)<\/request_signature>/
84 matches = rs_regex.match(page_data)
85 request_signature = matches[1] if not (matches.nil? || matches.length < 1)
86
87 return request_signature
88 end
89
90
91 def parse_request_signature_expires(page_data)
92 rse_regex = /<request_signature_expires>(.*?)<\/request_signature_expires>/
93 matches = rse_regex.match(page_data)
94 request_signature_expires = matches[1] if not (matches.nil? || matches.length < 1)
95
96 return request_signature_expires
97 end
98
99
100 def parse_quality(page_data)
101 quality_regex = /<isHD>([01])<\isHD>/
102 matches = quality_regex.match(page_data)
103 is_hd = matches[1] if not (matches.nil? || matches.length < 1)
104
105 if is_hd == '1' then
106 # High-definition
107 return "hd"
108 else
109 # Standard-definition
110 return "sd"
111 end
112 end
113
114
115 def get_page_data(url)
116 uri = URI.parse(url)
117
118 response = Net::HTTP.start(uri.host, uri.port) do |http|
119 http.get(uri.path)
120 end
121
122 return response.body
123 end
124
125 end