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 22f8710 commit e095077
Showing 1 changed file with 75 additions and 60 deletions.
135 changes: 75 additions & 60 deletions ruby/yt_analytics_report.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
#!/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 code assumes that there's a client_secrets.json file in your current directory,
# which contains OAuth 2.0 information for this application, including client_id and
# client_secret. You can acquire an ID/secret pair from the API Access tab on the
# {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
# For more information about using OAuth2 to access Google APIs, please visit:
# <https://developers.google.com/accounts/docs/OAuth2>
# For more information about the client_secrets.json file format, please visit:
# <https://developers.google.com/api-client-library/python/guide/aaa_client_secrets>
# Please ensure that you have enabled the YouTube Data & Analytics APIs for your project.

# 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',
Expand All @@ -28,61 +17,87 @@
YOUTUBE_ANALYTICS_API_SERVICE_NAME = 'youtubeAnalytics'
YOUTUBE_ANALYTICS_API_VERSION = 'v1'

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')
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

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'
return client, youtube, youtube_analytics
end

client = Google::APIClient.new
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)
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')

auth_util = CommandLineOAuthHelper.new(YOUTUBE_SCOPES)
client.authorization = auth_util.authorize()
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

# Retrieve the channel resource for the authenticated user's channel.
channels_response = client.execute!(
:api_method => youtube.channels.list,
:parameters => {
:mine => true,
:part => 'id'
}
)
client, youtube, youtube_analytics = get_authenticated_services

channels_response.data.items.each do |channel|
opts[:ids] = "channel==#{channel.id}"
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'
}
)

# 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
)
channels_response.data.items.each do |channel|
opts[:ids] = "channel==#{channel.id}"

puts "Analytics Data for 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
)

analytics_response.data.columnHeaders.each do |column_header|
printf '%-20s', column_header.name
end
puts
puts "Analytics Data for Channel #{channel.id}"

analytics_response.data.rows.each do |row|
row.each do |value|
printf '%-20s', value
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
puts
rescue Google::APIClient::TransmissionError => e
puts e.result.body
end
end

main

0 comments on commit e095077

Please sign in to comment.