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
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
)
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}"
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')
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)
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)
81 video_id
= matches
[1] if not (matches
.nil? || matches
.length
< 1)
88 def parse_request_signature(page_data
)
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)
94 return request_signature
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)
103 return request_signature_expires
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)
116 # Standard-definition
122 def get_page_data(url
)
125 response
= Net
::HTTP.start(uri
.host
, uri
.port
) do |http
|