]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/vimeo.rb
c259b7c602e1cc5c8bf709a8444efbaa426d868f
[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
22 class Vimeo < Website
23
24 VALID_VIMEO_URL_REGEX = /^(http:\/\/)?(www\.)?vimeo\.com\/(moogaloop\.swf\?clip_id=)?(\d+)/
25
26 def self.owns_url?(url)
27 return url =~ VALID_VIMEO_URL_REGEX
28 end
29
30
31 def get_video_url()
32 # First, figure out the video id from the URL.
33 # Then, use the video id to construct the video details URL.
34 # Get the video details page, and parse the resulting XML for
35 # the junk we need to construct the video URL. Note that the file
36 # URL given in the XML is *not* valid.
37 video_id = self.parse_video_id()
38 details_url = "http://www.vimeo.com/moogaloop/load/clip:#{video_id}/local"
39 details_data = get_page_data(details_url)
40
41 request_signature = parse_request_signature(details_data)
42 request_signature_expires = parse_request_signature_expires(details_data)
43 quality = parse_quality(details_data)
44
45 referer = "http://a.vimeocdn.com/p/flash/moogaloop/5.1.15/moogaloop.swf"
46 referer += "?v=1.0.0"
47 referer += "&time=#{request_signature_expires}"
48 self.headers['Referer'] = referer
49
50 video_url = "http://player.vimeo.com/play_redirect?clip_id=#{video_id}"
51 video_url += "&sig=#{request_signature}"
52 video_url += "&time=#{request_signature_expires}"
53 video_url += "&quality=#{quality}"
54 video_url += "&codecs=H264,VP8,VP6"
55
56 return video_url
57 end
58
59
60 def get_video_filename()
61 # The default behavior is no good here, use the video
62 # id with an extension tacked onto it.
63 return (self.parse_video_id() + '.flv')
64 end
65
66
67 protected;
68
69 def parse_video_id()
70 video_id = nil
71
72 # First, try to get the video id from the end of the URL.
73 video_id_regex = /\/(\d+)$/
74 matches = video_id_regex.match(@url)
75
76 if matches.nil?
77 # If that didn't work, the URL must be of the clip_id= form.
78 video_id_regex = /clip_id\=(\d+)/
79 matches = video_id_regex.match(@url)
80 video_id = matches[1] if not (matches.nil? || matches.length < 1)
81 else
82 video_id = matches[1] if not (matches.nil? || matches.length < 1)
83 end
84
85 return video_id
86 end
87
88
89 def parse_request_signature(page_data)
90 # It's XML.
91 rs_regex = /<request_signature>(.*?)<\/request_signature>/
92 matches = rs_regex.match(page_data)
93 request_signature = matches[1] if not (matches.nil? || matches.length < 1)
94
95 return request_signature
96 end
97
98
99 def parse_request_signature_expires(page_data)
100 rse_regex = /<request_signature_expires>(.*?)<\/request_signature_expires>/
101 matches = rse_regex.match(page_data)
102 request_signature_expires = matches[1] if not (matches.nil? || matches.length < 1)
103
104 return request_signature_expires
105 end
106
107
108 def parse_quality(page_data)
109 quality_regex = /<isHD>([01])<\isHD>/
110 matches = quality_regex.match(page_data)
111 is_hd = matches[1] if not (matches.nil? || matches.length < 1)
112
113 if is_hd == '1' then
114 # High-definition
115 return "hd"
116 else
117 # Standard-definition
118 return "sd"
119 end
120 end
121
122
123 end