]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/vimeo.rb
72ed31301495386faa2606103a85e9752a4f36b8
[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
42 request_signature = parse_request_signature(details_data)
43 request_signature_expires = parse_request_signature_expires(details_data)
44 quality = parse_quality(details_data)
45
46 # Being slightly explicit about what we're doing here...
47 video_url = "http://www.vimeo.com/moogaloop/play/clip:#{video_id}/#{request_signature}/#{request_signature_expires}/?q=#{quality}"
48
49 return video_url
50 end
51
52
53 def get_video_filename()
54 # The default behavior is no good here, use the video
55 # id with an extension tacked onto it.
56 return (self.parse_video_id() + '.flv')
57 end
58
59
60 protected;
61
62 def parse_video_id()
63 video_id = nil
64
65 # First, try to get the video id from the end of the URL.
66 video_id_regex = /\/(\d+)$/
67 matches = video_id_regex.match(@url)
68
69 if matches.nil?
70 # If that didn't work, the URL must be of the clip_id= form.
71 video_id_regex = /clip_id\=(\d+)/
72 matches = video_id_regex.match(@url)
73 video_id = matches[1] if not (matches.nil? || matches.length < 1)
74 else
75 video_id = matches[1] if not (matches.nil? || matches.length < 1)
76 end
77
78 return video_id
79 end
80
81
82 def parse_request_signature(page_data)
83 # It's XML.
84 rs_regex = /<request_signature>(.*?)<\/request_signature>/
85 matches = rs_regex.match(page_data)
86 request_signature = matches[1] if not (matches.nil? || matches.length < 1)
87
88 return request_signature
89 end
90
91
92 def parse_request_signature_expires(page_data)
93 rse_regex = /<request_signature_expires>(.*?)<\/request_signature_expires>/
94 matches = rse_regex.match(page_data)
95 request_signature_expires = matches[1] if not (matches.nil? || matches.length < 1)
96
97 return request_signature_expires
98 end
99
100
101 def parse_quality(page_data)
102 quality_regex = /<isHD>([01])<\isHD>/
103 matches = quality_regex.match(page_data)
104 is_hd = matches[1] if not (matches.nil? || matches.length < 1)
105
106 if is_hd == '1' then
107 # High-definition
108 return "hd"
109 else
110 # Standard-definition
111 return "sd"
112 end
113 end
114
115
116 end