]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/vimeo.rb
56243fad3ecd5d0616db0d5fdd5aa605bf01d2b2
[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()
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 = self.parse_video_id()
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 def get_video_filename()
60 # The default behavior is no good here, use the video
61 # id with an extension tacked onto it.
62 return (self.parse_video_id() + '.flv')
63 end
64
65
66 protected;
67
68 def parse_video_id()
69 video_id = nil
70
71 # First, try to get the video id from the end of the URL.
72 video_id_regex = /\/(\d+)$/
73 matches = video_id_regex.match(@url)
74
75 if matches.nil?
76 # If that didn't work, the URL must be of the clip_id= form.
77 video_id_regex = /clip_id\=(\d+)/
78 matches = video_id_regex.match(@url)
79 video_id = matches[1] if not (matches.nil? || matches.length < 1)
80 else
81 video_id = matches[1] if not (matches.nil? || matches.length < 1)
82 end
83
84 return video_id
85 end
86
87
88 def parse_request_signature(page_data)
89 # It's XML.
90 rs_regex = /<request_signature>(.*?)<\/request_signature>/
91 matches = rs_regex.match(page_data)
92 request_signature = matches[1] if not (matches.nil? || matches.length < 1)
93
94 return request_signature
95 end
96
97
98 def parse_request_signature_expires(page_data)
99 rse_regex = /<request_signature_expires>(.*?)<\/request_signature_expires>/
100 matches = rse_regex.match(page_data)
101 request_signature_expires = matches[1] if not (matches.nil? || matches.length < 1)
102
103 return request_signature_expires
104 end
105
106
107 def parse_quality(page_data)
108 quality_regex = /<isHD>([01])<\isHD>/
109 matches = quality_regex.match(page_data)
110 is_hd = matches[1] if not (matches.nil? || matches.length < 1)
111
112 if is_hd == '1' then
113 # High-definition
114 return "hd"
115 else
116 # Standard-definition
117 return "sd"
118 end
119 end
120
121
122 def get_page_data(url)
123 uri = URI.parse(url)
124
125 response = Net::HTTP.start(uri.host, uri.port) do |http|
126 http.get(uri.path)
127 end
128
129 return response.body
130 end
131
132 end