2 # Copyright Michael Orlitzky
4 # http://michael.orlitzky.com/
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.
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.
16 # http://www.fsf.org/licensing/licenses/gpl.html
21 # Needed to download the video XML file, which is in turn
22 # needed because it contains the junk we need to construct
30 VALID_VIMEO_URL_REGEX
= /^(http:\/\
/)?(www\.)?vimeo\.com\/(moogaloop\
.swf
\?clip_id
=)?(\d+
)/
32 def self.owns_url
?(url
)
33 return url
=~ VALID_VIMEO_URL_REGEX
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
)
48 request_signature
= parse_request_signature(details_data
)
49 request_signature_expires
= parse_request_signature_expires(details_data
)
50 quality
= parse_quality(details_data
)
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}"
61 def parse_video_id(url
)
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
)
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)
74 video_id
= matches
[1] if not (matches
.nil? || matches
.length
< 1)
81 def parse_request_signature(page_data
)
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)
87 return request_signature
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)
96 return request_signature_expires
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)
109 # Standard-definition
115 def get_page_data(url
)
118 response
= Net
::HTTP.start(uri
.host
, uri
.port
) do |http
|