DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

A Ruby Snippet To Download Vimeo Videos With ProgressBar

07.24.2009
| 5044 views |
  • submit to reddit
        A new version with ProgressBar ;)

gem install progressbar before.

Tested on Ubuntu and Windows :)

#!/usr/bin/ruby

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

if ARGV.size < 1
	puts "usage vimeo.rb <id_video>"
	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>(.*)<\/caption>/.match(response.body)
		title = $1
		/<request_signature>(.*)<\/request_signature>/.match(response.body)
		signature = $1
		/<request_signature_expires>(.*)<\/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