forked from youtube/api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt_analytics_report.rb
103 lines (88 loc) · 3.54 KB
/
yt_analytics_report.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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