Created
December 29, 2008 18:17
-
-
Save Constellation/41331 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -Ku | |
require 'rubygems' | |
require 'nokogiri' | |
require 'net/http' | |
require 'uri' | |
# こんな感じ? | |
class Veoh | |
@@client = '' | |
@@version = '3.0.0' | |
@@headers = { | |
"Content-type"=> "application/xml", | |
"User-agent"=> "veoh-3.0 service (MacOS; Safari; darwin)" | |
} | |
def initialize url | |
@video_id = get_video_id url | |
return unless @video_id | |
data = "<MediaIdList><MediaId permalinkId=\"#{@video_id}\"/></MediaIdList>" | |
uri = URI("http://www.veoh.com/service/getMediaInfo.xml?clientGUID=#{@@client}&version=#{@@version}") | |
xml = Nokogiri::XML.parse(Net::HTTP.new(uri.host).post(uri.request_uri, data, @@headers).body) | |
@piece_url = xml.at('//PieceHashFile').text.strip | |
@file_hash = xml.at('//FileHash').text.strip | |
#@file_size = xml.at('//Size').text.strip | |
@file_title = xml.at('//Video/Title').text.strip | |
@file_ext = xml.at('//Extension').text() | |
end | |
def get_video | |
#file = File.open("#{@file_title}.#{@file_ext}", 'w') | |
xml2 = Nokogiri::XML.parse(fetch(@piece_url).body) | |
ids = xml2.search('//piece/@id').to_ary | |
len = ids.length | |
counter = 0 | |
ids.inject(File.open("#{@file_title}.#{@file_ext}", 'w')) do |file, id| | |
p "download: #{counter+=1} / #{len}" | |
file.write(fetch("http://p-cache.veoh.com/cache/get.jsp?fileHash=#{@file_hash}&pieceHash=#{id.text.strip}").body) | |
file | |
end.close | |
p 'download: done' | |
end | |
def fetch( uri_str, limit = 10 ) | |
raise ArgumentError, 'http redirect too deep' if limit == 0 | |
response = Net::HTTP.get_response(URI.parse(uri_str)) | |
case response | |
when Net::HTTPSuccess then response | |
when Net::HTTPRedirection then fetch(response['location'], limit - 1) | |
else | |
response.error! | |
end | |
end | |
def get_video_id url | |
if(/permalinkId=(v[^&]+)/.match(url)) then | |
$1 | |
elsif(/videos\/(v[^&?]+)/.match(url)) then | |
$1 | |
end | |
end | |
end | |
veoh = Veoh.new(ARGV[0]) | |
veoh.get_video |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment