Wrote script to upload bunch of videos to youtube through YouTube API:
To get developer’s key, visit: http://code.google.com/apis/youtube/dashboard/gwt/index.html
Thanks to kylejginavan@github for this cool gem: YouTube It
Usage:
ruby youtube-it.rb --user <user@gmail.com> --password <password_for_gmail> --key <your_dev_key> *.avi
Before run
gem install youtube_it -r
Code:
# encoding: utf-8
require "rubygems"
require "openssl"
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
require "youtube_it"
require "fileutils"
require "ostruct"
require "optparse"
$stdout.sync = true
options = OpenStruct.new
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] file1[ file2 ...]"
opts.on("-u", "--user USERNAME", "[REQUIRED] Specify username for youtube account") do |username|
options.username = username
end
opts.on(:REQUIRED, "-p", "--password PASSWORD", "[REQUIRED] Specify password for youtube account") do |password|
options.password = password
end
opts.on("-k", "--key KEY", "[REQUIRED] Specify developer's key for youtube account") do |dev_key|
options.dev_key = dev_key
end
opts.on_tail("-h", "--help", "Display this screen") do
puts opts
exit
end
end
optparse.parse!
if options.username.nil? || options.password.nil? || options.dev_key.nil? || ARGV.size == 0
puts optparse
exit
end
client = YouTubeIt::Client.new({
:username => options.username,
:password => options.password,
:dev_key => options.dev_key
})
ARGV.each do |filepath|
(puts "File #{filepath} doesn't exists. Skipping."; next) unless File.exists?(filepath)
begin
print filepath
title = File.basename(filepath, File.extname(filepath))
title[0] = title[0, 1].upcase
title.gsub!(/\s*(\-|_)\s*/iu, ' ')
client.video_upload(File.open(filepath), {
:title => title,
:description => title,
:category => "Travel"
})
puts " .. OK!"
rescue Exception => e
puts "", e, "Sleeping 30s", ""
sleep 30
retry
end
end