Ruby के लिए Google API क्लाइंट लाइब्रेरी का इस्तेमाल करने वाले नीचे दिए गए कोड सैंपल, YouTube Analytics API के लिए उपलब्ध हैं. आप GitHub पर YouTube एपीआई कोड सैंपल रिपॉज़िटरी के ruby
फ़ोल्डर से इन कोड के सैंपल डाउनलोड कर सकते हैं.
अनुरोध को अनुमति देना
यह कोड सैंपल, OAuth 2.0 की अनुमति देने का काम करता है. इसके लिए, यह एक ऐसी स्थानीय फ़ाइल की मौजूदगी की जांच करता है जिसमें ऑथराइज़ेशन क्रेडेंशियल होते हैं. अगर फ़ाइल मौजूद नहीं है, तो स्क्रिप्ट ब्राउज़र को खोलती है और जवाब का इंतज़ार करती है. इसके बाद, वापस आने वाले क्रेडेंशियल को स्थानीय तौर पर सेव करती है.
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'json'
require 'launchy'
require 'thin'
RESPONSE_HTML = <<stop
<html>
<head>
<title>OAuth 2 Flow Complete</title>
</head>
<body>
You have successfully completed the OAuth 2 flow. Please close this browser window and return to your program.
</body>
</html>
stop
FILE_POSTFIX = '-oauth2.json'
# Small helper for the sample apps for performing OAuth 2.0 flows from the command
# line. Starts an embedded server to handle redirects.
class CommandLineOAuthHelper
def initialize(scope)
credentials = Google::APIClient::ClientSecrets.load
@authorization = Signet::OAuth2::Client.new(
:authorization_uri => credentials.authorization_uri,
:token_credential_uri => credentials.token_credential_uri,
:client_id => credentials.client_id,
:client_secret => credentials.client_secret,
:redirect_uri => credentials.redirect_uris.first,
:scope => scope
)
end
# Request authorization. Checks to see if a local file with credentials is present, and uses that.
# Otherwise, opens a browser and waits for response, then saves the credentials locally.
def authorize
credentialsFile = $0 + FILE_POSTFIX
if File.exist? credentialsFile
File.open(credentialsFile, 'r') do |file|
credentials = JSON.load(file)
@authorization.access_token = credentials['access_token']
@authorization.client_id = credentials['client_id']
@authorization.client_secret = credentials['client_secret']
@authorization.refresh_token = credentials['refresh_token']
@authorization.expires_in = (Time.parse(credentials['token_expiry']) - Time.now).ceil
if @authorization.expired?
@authorization.fetch_access_token!
save(credentialsFile)
end
end
else
auth = @authorization
url = @authorization.authorization_uri().to_s
server = Thin::Server.new('0.0.0.0', 8080) do
run lambda { |env|
# Exchange the auth code & quit
req = Rack::Request.new(env)
auth.code = req['code']
auth.fetch_access_token!
server.stop()
[200, {'Content-Type' => 'text/html'}, RESPONSE_HTML]
}
end
Launchy.open(url)
server.start()
save(credentialsFile)
end
return @authorization
end
def save(credentialsFile)
File.open(credentialsFile, 'w', 0600) do |file|
json = JSON.dump({
:access_token => @authorization.access_token,
:client_id => @authorization.client_id,
:client_secret => @authorization.client_secret,
:refresh_token => @authorization.refresh_token,
:token_expiry => @authorization.expires_at
})
file.write(json)
end
end
end
देखे जाने की संख्या के आधार पर शीर्ष 10 वीडियो फिर से पाएं
YouTube Analytics का डेटा फिर से पाने के लिए, इस नमूने में एपीआई के reports.query
तरीके का इस्तेमाल किया गया है.
डिफ़ॉल्ट रूप से, रिपोर्ट में कुल 10 वीडियो को देखे जाने की संख्या के हिसाब से फिर से पाया जाता है. साथ ही, यह उन वीडियो के लिए कई मेट्रिक दिखाता है, जो नतीजों को देखे जाने की संख्या के मुताबिक क्रम में लगाता है. कमांड लाइन पैरामीटर सेट करके, आप उसी कोड का इस्तेमाल करके
अन्य रिपोर्ट भी पा सकते हैं.
#!/usr/bin/ruby
require 'rubygems'
gem 'google-api-client', '>0.7'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'
require 'trollop'
# These OAuth 2.0 access scopes allow for read-only access to the authenticated
# user's account for both YouTube Data API resources and YouTube Analytics Data.
YOUTUBE_SCOPES = ['https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/yt-analytics.readonly']
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
YOUTUBE_ANALYTICS_API_SERVICE_NAME = 'youtubeAnalytics'
YOUTUBE_ANALYTICS_API_VERSION = 'v1'
def get_authenticated_services
client = Google::APIClient.new(
:application_name => $PROGRAM_NAME,
:application_version => '1.0.0'
)
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
youtube_analytics = client.discovered_api(YOUTUBE_ANALYTICS_API_SERVICE_NAME, YOUTUBE_ANALYTICS_API_VERSION)
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_SCOPES
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
return client, youtube, youtube_analytics
end
def main
now = Time.new.to_i
seconds_in_day = 60 * 60 * 24
seconds_in_week = seconds_in_day * 7
one_day_ago = Time.at(now - seconds_in_day).strftime('%Y-%m-%d')
one_week_ago = Time.at(now - seconds_in_week).strftime('%Y-%m-%d')
opts = Trollop::options do
opt :metrics, 'Report metrics', :type => String, :default => 'views,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares'
opt :dimensions, 'Report dimensions', :type => String, :default => 'video'
opt 'start-date', 'Start date, in YYYY-MM-DD format', :type => String, :default => one_week_ago
opt 'end-date', 'Start date, in YYYY-MM-DD format', :type => String, :default => one_day_ago
opt 'start-index', 'Start index', :type => :int, :default => 1
opt 'max-results', 'Max results', :type => :int, :default => 10
opt :sort, 'Sort order', :type => String, :default => '-views'
end
client, youtube, youtube_analytics = get_authenticated_services
begin
# Retrieve the channel resource for the authenticated user's channel.
channels_response = client.execute!(
:api_method => youtube.channels.list,
:parameters => {
:mine => true,
:part => 'id'
}
)
channels_response.data.items.each do |channel|
opts[:ids] = "channel==#{channel.id}"
# Call the Analytics API to retrieve a report. For a list of available
# reports, see:
# https://developers.google.com/youtube/analytics/v1/channel_reports
analytics_response = client.execute!(
:api_method => youtube_analytics.reports.query,
:parameters => opts
)
puts "Analytics Data for Channel #{channel.id}"
analytics_response.data.columnHeaders.each do |column_header|
printf '%-20s', column_header.name
end
puts
analytics_response.data.rows.each do |row|
row.each do |value|
printf '%-20s', value
end
puts
end
end
rescue Google::APIClient::TransmissionError => e
puts e.result.body
end
end
main