Sunday, July 26, 2009

A Ruby snippet to download Vimeo videos with ProgressBar

#!/usr/bin/ruby

require 'rubygems'
require 'progressbar'
require 'net/http'

if ARGV.size < 1
puts "usage vimeo.rb "
exit 1
else
id = ARGV[0]
Net::HTTP.start('www.vimeo.com') {|http|
req = Net::HTTP::Get.new("/moogaloop/load/clip:#{id}", nil)
response = http.request(req)
/(.*)<\/caption>/.match(response.body)
title = $1
/(.*)<\/request_signature>/.match(response.body)
signature = $1
/(.*)<\/request_signature_expires>/.match(response.body)
signatureExp = $1
puts title
req = Net::HTTP::Get.new("/moogaloop/play/clip:#{id}/#{signature}/#{signatureExp}/?q=hd", nil)
http.request(req) { |response|
/(mp4|flv)/.match(response['location'])
ext = $1
/http:\/\/(.*\.vimeo\.com)(\/.*)/.match(response['location'])
Net::HTTP.start($1) {|http|
req = Net::HTTP::Get.new($2)
alreadyDL = 0
http.request(req) { |response|
pBar = ProgressBar.new(title,100)
size = response.content_length
File.open("#{title}.#{ext}",'w') {|file|
response.read_body {|segment|
alreadyDL += segment.length
if(alreadyDL != 0)
aPercent = (alreadyDL * 100) / size
pBar.set(aPercent)
end
file.write(segment)
}
pBar.finish
}
}
}
}
}
end
gem install progressbar before.

http://snippets.dzone.com/posts/show/7630

No comments: