Skip to content

Commit

Permalink
"Updating samples to reflect recent changes."
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Feb 12, 2014
1 parent 4f89d9c commit 74c7a81
Showing 1 changed file with 75 additions and 48 deletions.
123 changes: 75 additions & 48 deletions ruby/upload_video.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,91 @@
#!/usr/bin/ruby

require 'rubygems'
gem 'google-api-client', '>0.7'
require 'google/api_client'
# The oauth/oauth_util code is not part of the official Ruby client library.
# Download it from:
# http://samples.google-api-ruby-client.googlecode.com/git/oauth/oauth_util.rb
require 'oauth/oauth_util'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'
require 'trollop'


# This OAuth 2.0 access scope allows an application to upload files to the
# authenticated user's YouTube channel, but doesn't allow other types of access.
YOUTUBE_READ_WRITE_SCOPE = 'https://www.googleapis.com/auth/youtube.upload'
# A limited OAuth 2 access scope that allows for uploading files, but not other
# types of account access.
YOUTUBE_UPLOAD_SCOPE = 'https://www.googleapis.com/auth/youtube.upload'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'

client = Google::APIClient.new(:application_name => $0, :application_version => '1.0')
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
def get_authenticated_service
client = Google::APIClient.new(
:application_name => $PROGRAM_NAME,
:application_version => '1.0.0'
)
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)

auth_util = CommandLineOAuthHelper.new(YOUTUBE_READ_WRITE_SCOPE)
client.authorization = auth_util.authorize()
file_storage = Google::APIClient::FileStorage.new("#{$PROGRAM_NAME}-oauth2.json")
if file_storage.authorization.nil?
client_secrets = Google::APIClient::ClientSecrets.load
flow = Google::APIClient::InstalledAppFlow.new(
:client_id => client_secrets.client_id,
:client_secret => client_secrets.client_secret,
:scope => [YOUTUBE_UPLOAD_SCOPE]
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end

opts = Trollop::options do
opt :file, 'Video file to upload', :type => String
opt :title, 'Video title', :default => 'Test Title', :type => String
opt :description, 'Video description',
:default => 'Test Description', :type => String
opt :categoryId, 'Numeric video category. See https://developers.google.com/youtube/v3/docs/videoCategories/list',
:default => 22, :type => :int
opt :keywords, 'Video keywords, comma-separated',
:default => '', :type => String
opt :privacyStatus, 'Video privacy status: public, private, or unlisted',
:default => 'public', :type => String
return client, youtube
end

if opts[:file].nil? or not File.file?(opts[:file])
Trollop::die :file, 'does not exist'
end
def main
opts = Trollop::options do
opt :file, 'Video file to upload', :type => String
opt :title, 'Video title', :default => 'Test Title', :type => String
opt :description, 'Video description',
:default => 'Test Description', :type => String
opt :category_id, 'Numeric video category. See https://developers.google.com/youtube/v3/docs/videoCategories/list',
:default => 22, :type => :int
opt :keywords, 'Video keywords, comma-separated',
:default => '', :type => String
opt :privacy_status, 'Video privacy status: public, private, or unlisted',
:default => 'public', :type => String
end

if opts[:file].nil? or not File.file?(opts[:file])
Trollop::die :file, 'does not exist'
end

client, youtube = get_authenticated_service

body = {
:snippet => {
:title => opts[:title],
:description => opts[:description],
:tags => opts[:keywords].split(','),
:categoryId => opts[:categoryId],
},
:status => {
:privacyStatus => opts[:privacyStatus]
}
}
begin
body = {
:snippet => {
:title => opts[:title],
:description => opts[:description],
:tags => opts[:keywords].split(','),
:categoryId => opts[:category_id],
},
:status => {
:privacyStatus => opts[:privacy_status]
}
}

# Call the API's videos.insert method to create and upload the video.
videos_insert_response = client.execute!(
:api_method => youtube.videos.insert,
:body_object => body,
:media => Google::APIClient::UploadIO.new(opts[:file], 'video/*'),
:parameters => {
'uploadType' => 'multipart',
:part => body.keys.join(',')
}
)
videos_insert_response = client.execute!(
:api_method => youtube.videos.insert,
:body_object => body,
:media => Google::APIClient::UploadIO.new(opts[:file], 'video/*'),
:parameters => {
:uploadType => 'resumable',
:part => body.keys.join(',')
}
)

videos_insert_response.resumable_upload.send_all(client)

puts "Video id '#{videos_insert_response.data.id}' was successfully uploaded."
rescue Google::APIClient::TransmissionError => e
puts e.result.body
end
end

puts "'#{videos_insert_response.data.snippet.title}' (video id: #{videos_insert_response.data.id}) was successfully uploaded."
main

0 comments on commit 74c7a81

Please sign in to comment.