]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blobdiff - src/websites/vimeo.rb
Added the ability to download Vimeo videos.
[dead/whatever-dl.git] / src / websites / vimeo.rb
diff --git a/src/websites/vimeo.rb b/src/websites/vimeo.rb
new file mode 100644 (file)
index 0000000..3836df1
--- /dev/null
@@ -0,0 +1,125 @@
+#
+# Copyright Michael Orlitzky
+#
+# http://michael.orlitzky.com/
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# http://www.fsf.org/licensing/licenses/gpl.html
+#
+
+require 'src/website'
+
+# Needed to download the video XML file, which is in turn
+# needed because it contains the junk we need to construct
+# the video URL.
+require 'net/http'
+require 'uri'
+
+
+class Vimeo < Website
+
+  VALID_VIMEO_URL_REGEX = /^(http:\/\/)?(www\.)?vimeo\.com\/(moogaloop\.swf\?clip_id=)?(\d+)/
+  
+  def self.owns_url?(url)
+    return url =~ VALID_VIMEO_URL_REGEX
+  end
+
+  
+  def get_video_url(url)
+    # First, figure out the video id from the URL.
+    # Then, use the video id to construct the video details URL.
+    # Get the video details page, and parse the resulting XML for
+    # the junk we need to construct the video URL. Note that the file
+    # URL given in the XML is *not* valid.
+    video_id = parse_video_id(url)
+    details_url = "http://www.vimeo.com/moogaloop/load/clip:#{video_id}/local"
+    details_data = get_page_data(details_url)
+
+    
+    request_signature = parse_request_signature(details_data)
+    request_signature_expires = parse_request_signature_expires(details_data)
+    quality = parse_quality(details_data)
+    
+    # Being slightly explicit about what we're doing here...
+    video_url = "http://www.vimeo.com/moogaloop/play/clip:#{video_id}/#{request_signature}/#{request_signature_expires}/?q=#{quality}"
+    
+    return video_url
+  end
+
+  
+  protected;
+
+  def parse_video_id(url)
+    video_id = nil
+    
+    # First, try to get the video id from the end of the URL.
+    video_id_regex = /\/(\d+)$/
+    matches = video_id_regex.match(url)
+
+    if matches.nil?
+      # If that didn't work, the URL must be of the clip_id= form.
+      video_id_regex = /clip_id\=(\d+)/
+      matches = video_id_regex.match(url)
+      video_id = matches[1] if not (matches.nil? || matches.length < 1)
+    else
+      video_id = matches[1] if not (matches.nil? || matches.length < 1)
+    end
+    
+    return video_id
+  end
+
+  
+  def parse_request_signature(page_data)
+    # It's XML.
+    rs_regex = /<request_signature>(.*?)<\/request_signature>/
+    matches = rs_regex.match(page_data)
+    request_signature = matches[1] if not (matches.nil? || matches.length < 1)
+    
+    return request_signature
+  end
+  
+
+  def parse_request_signature_expires(page_data)
+    rse_regex = /<request_signature_expires>(.*?)<\/request_signature_expires>/
+    matches = rse_regex.match(page_data)
+    request_signature_expires = matches[1] if not (matches.nil? || matches.length < 1)
+    
+    return request_signature_expires
+  end
+
+
+  def parse_quality(page_data)
+    quality_regex = /<isHD>([01])<\isHD>/
+    matches = quality_regex.match(page_data)
+    is_hd = matches[1] if not (matches.nil? || matches.length < 1)
+
+    if is_hd == '1' then
+       # High-definition
+      return "hd"
+    else
+      # Standard-definition
+      return "sd"
+    end
+  end
+
+  
+  def get_page_data(url)
+    uri = URI.parse(url)
+    
+    response = Net::HTTP.start(uri.host, uri.port) do |http|
+      http.get(uri.path)
+    end
+    
+    return response.body
+  end
+  
+end