From a0e45b621f6f6d516b13bc8960050a954e7f4624 Mon Sep 17 00:00:00 2001 From: Andy Diamondstein Date: Fri, 27 Oct 2017 14:00:29 -0400 Subject: [PATCH] Update script options, functionality, docs, auth Functionality updates: - Add ability to run script for channel ID or content owner - Add option to set filters - Change default metrics, remove default max results - Remove additional call to YouTube Data API to get channel ID and just set ids=channel==MINE - Add function to properly set "ids" parameter depending on whether content-owner or channel-id command-line arguments are set - Pass API request parameters as keyword arguments (**args) rather than listing them line by line The functionality in the script is more thoroughly documented. Also see the Prerequisites section of the README file for more information about running this sample. Finally, the oauth2client library is deprecated. This update changes the sample to use the google-auth and google-auth-oauthlib libraries instead. --- python/yt_analytics_report.py | 245 ++++++++++++++++++++-------------- 1 file changed, 142 insertions(+), 103 deletions(-) diff --git a/python/yt_analytics_report.py b/python/yt_analytics_report.py index 6e816106..1df122bf 100644 --- a/python/yt_analytics_report.py +++ b/python/yt_analytics_report.py @@ -1,15 +1,41 @@ #!/usr/bin/python -from datetime import datetime, timedelta -import httplib2 +### +# +# Retrieves YouTube Analytics report data. The script's default behavior +# is to retrieve data for the authenticated user's channel. However, if you +# set a value for the --content-owner command-line argument, then the script +# retrieves data for that content owner. Note that the user running the script +# must be authorized to retrieve data for that content owner. +# +# Note that when you retrieve Analytics data for a content owner, your API +# request must set a value for the "filters" request parameter as explained +# in the API documentation here: +# https://developers.google.com/youtube/analytics/v1/content_owner_reports#Filters +# +# By default, if you set a value for the --content-owner argument, then the +# "filters" parameter is set to "claimedStatus==claimed". (On the other hand, +# this value is not set if you are retrieving data for your own channel.) +# +# You can use the --filters command-line argument to set the "filters" parameter +# for any request. For example: +# * --filters="channel==CHANNEL_ID" +# * --filters="channel==CHANNEL_ID;country==COUNTRY_CODE" +# * --filters="video==VIDEO_ID" +# " --filters="claimedStatus==claimed;uploaderType==thirdParty" +# and so forth. +# +### + +import argparse import os -import sys -from apiclient.discovery import build -from apiclient.errors import HttpError -from oauth2client.client import flow_from_clientsecrets -from oauth2client.file import Storage -from oauth2client.tools import argparser, run_flow +import google.oauth2.credentials +import google_auth_oauthlib.flow +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError +from google_auth_oauthlib.flow import InstalledAppFlow +from datetime import datetime, timedelta # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains @@ -17,115 +43,128 @@ # client_secret. You can acquire an OAuth 2.0 client ID and client secret from # the {{ Google Cloud Console }} at # {{ https://cloud.google.com/console }}. -# Please ensure that you have enabled the YouTube Data and YouTube Analytics -# APIs for your project. -# For more information about using OAuth2 to access the YouTube Data API, see: -# https://developers.google.com/youtube/v3/guides/authentication -# For more information about the client_secrets.json file format, see: +# Please ensure that you have enabled the YouTube Analytics API for your project. +# For more information about using OAuth2 to access the YouTube Analytics API, see: +# https://developers.google.com/youtube/reporting/guides/authorization +# For more information about the client_secret.json file format, see: # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets -CLIENT_SECRETS_FILE = "client_secrets.json" +CLIENT_SECRETS_FILE = 'client_secret.json' # 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" - -# This variable defines a message to display if the CLIENT_SECRETS_FILE is -# missing. -MISSING_CLIENT_SECRETS_MESSAGE = """ -WARNING: Please configure OAuth 2.0 - -To make this sample run you will need to populate the client_secrets.json file -found at: - - %s - -with information from the {{ Cloud Console }} -{{ https://cloud.google.com/console }} - -For more information about the client_secrets.json file format, please visit: -https://developers.google.com/api-client-library/python/guide/aaa_client_secrets -""" % os.path.abspath(os.path.join(os.path.dirname(__file__), - CLIENT_SECRETS_FILE)) - - -def get_authenticated_services(args): - flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, - scope=" ".join(YOUTUBE_SCOPES), - message=MISSING_CLIENT_SECRETS_MESSAGE) - - storage = Storage("%s-oauth2.json" % sys.argv[0]) - credentials = storage.get() - - if credentials is None or credentials.invalid: - credentials = run_flow(flow, storage, args) - - http = credentials.authorize(httplib2.Http()) - - youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, - http=http) - youtube_analytics = build(YOUTUBE_ANALYTICS_API_SERVICE_NAME, - YOUTUBE_ANALYTICS_API_VERSION, http=http) - - return (youtube, youtube_analytics) - -def get_channel_id(youtube): - channels_list_response = youtube.channels().list( - mine=True, - part="id" - ).execute() - - return channels_list_response["items"][0]["id"] - -def run_analytics_report(youtube_analytics, channel_id, options): - # Call the Analytics API to retrieve a report. For a list of available - # reports, see: +SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly'] +API_SERVICE_NAME = 'youtubeAnalytics' +API_VERSION = 'v1' + +# Authorize the request and store authorization credentials. +def get_authenticated_service(): + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) + credentials = flow.run_console() + + api_service = build(API_SERVICE_NAME, API_VERSION, credentials=credentials) + return api_service + +# Remove keyword arguments that are not set. +def remove_empty_args(args): + original_args = vars(args) + good_args = {} + if original_args is not None: + for key, value in original_args.iteritems(): + # The channel_id and content_owner arguments are provided as a means + # of properly setting the "ids" parameter value. However, they should + # not be included in the API request (since they're not parameters + # supported by this method). + if value and key != 'channel_id' and key != 'content_owner': + good_args[key] = value + return good_args + +# Set the "ids" request parameter for the YouTube Analytics API request. +def set_ids_parameter(args): + if args.content_owner: + args.ids = 'contentOwner==' + args.content_owner + if args.filters == '': + args.filters = 'claimedStatus==claimed' + elif args.channel_id: + args.ids = 'channel==' + args.channel_id + else: + args.ids = 'channel==MINE' + args = remove_empty_args(args) + print args + return args + +def run_analytics_report(youtube_analytics, args): + # Call the Analytics API to retrieve a report. Pass args in as keyword + # arguments to set values for the following parameters: + # + # * ids + # * metrics + # * dimensions + # * filters + # * start_date + # * end_date + # * sort + # * max_results + # + # For a list of available reports, see: # https://developers.google.com/youtube/analytics/v1/channel_reports + # https://developers.google.com/youtube/analytics/v1/content_owner_reports analytics_query_response = youtube_analytics.reports().query( - ids="channel==%s" % channel_id, - metrics=options.metrics, - dimensions=options.dimensions, - start_date=options.start_date, - end_date=options.end_date, - max_results=options.max_results, - sort=options.sort + **args ).execute() - print "Analytics Data for Channel %s" % channel_id + #print 'Analytics Data for Channel %s' % channel_id - for column_header in analytics_query_response.get("columnHeaders", []): - print "%-20s" % column_header["name"], + for column_header in analytics_query_response.get('columnHeaders', []): + print '%-20s' % column_header['name'], print - for row in analytics_query_response.get("rows", []): + for row in analytics_query_response.get('rows', []): for value in row: - print "%-20s" % value, + print '%-20s' % value, print -if __name__ == "__main__": +if __name__ == '__main__': now = datetime.now() - one_day_ago = (now - timedelta(days=1)).strftime("%Y-%m-%d") - one_week_ago = (now - timedelta(days=7)).strftime("%Y-%m-%d") - - argparser.add_argument("--metrics", help="Report metrics", - default="views,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares") - argparser.add_argument("--dimensions", help="Report dimensions", - default="video") - argparser.add_argument("--start-date", default=one_week_ago, - help="Start date, in YYYY-MM-DD format") - argparser.add_argument("--end-date", default=one_day_ago, - help="End date, in YYYY-MM-DD format") - argparser.add_argument("--max-results", help="Max results", default=10) - argparser.add_argument("--sort", help="Sort order", default="-views") - args = argparser.parse_args() - - (youtube, youtube_analytics) = get_authenticated_services(args) + one_day_ago = (now - timedelta(days=1)).strftime('%Y-%m-%d') + one_week_ago = (now - timedelta(days=7)).strftime('%Y-%m-%d') + + parser = argparse.ArgumentParser() + + # Set channel ID or content owner. Default is authenticated user's channel. + parser.add_argument('--channel-id', default='', + help='YouTube channel ID for which data should be retrieved. ' + + 'Note that the default behavior is to retrieve data for ' + + 'the authenticated user\'s channel.') + parser.add_argument('--content-owner', default='', + help='The name of the content owner for which data should be ' + + 'retrieved. If you retrieve data for a content owner, then ' + + 'your API request must also set a value for the "filters" ' + + 'parameter. See the help for that parameter for more details.') + + # Metrics, dimensions, filters + parser.add_argument('--metrics', help='Report metrics', + default='views,estimatedMinutesWatched,averageViewDuration') + parser.add_argument('--dimensions', help='Report dimensions', default='') + parser.add_argument('--filters', default='', + help='Filters for the report. Note that the filters request parameter ' + + 'must be set in YouTube Analytics API requests for content owner ' + + 'reports. The script sets "filters=claimedStatus==claimed" if a ' + + 'content owner is specified and filters are not specified.') + + # Report dates. Defaults to start 7 days ago, end yesterday. + parser.add_argument('--start-date', default=one_week_ago, + help='Start date, in YYYY-MM-DD format') + parser.add_argument('--end-date', default=one_day_ago, + help='End date, in YYYY-MM-DD format') + + parser.add_argument('--max-results', help='Max results') + parser.add_argument('--sort', help='Sort order', default='') + + args = parser.parse_args() + args = set_ids_parameter(args) + + youtube_analytics = get_authenticated_service() try: - channel_id = get_channel_id(youtube) - run_analytics_report(youtube_analytics, channel_id, args) + run_analytics_report(youtube_analytics, args) except HttpError, e: - print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)