|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Usage example: |
| 4 | +# python create_reporting_job.py --name='<name>' |
| 5 | + |
| 6 | +import httplib2 |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +from apiclient.discovery import build |
| 11 | +from apiclient.errors import HttpError |
| 12 | +from oauth2client.client import flow_from_clientsecrets |
| 13 | +from oauth2client.file import Storage |
| 14 | +from oauth2client.tools import argparser, run_flow |
| 15 | + |
| 16 | + |
| 17 | +# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains |
| 18 | + |
| 19 | +# the OAuth 2.0 information for this application, including its client_id and |
| 20 | +# client_secret. You can acquire an OAuth 2.0 client ID and client secret from |
| 21 | +# the {{ Google Cloud Console }} at |
| 22 | +# {{ https://cloud.google.com/console }}. |
| 23 | +# Please ensure that you have enabled the YouTube Data API for your project. |
| 24 | +# For more information about using OAuth2 to access the YouTube Data API, see: |
| 25 | +# https://developers.google.com/youtube/v3/guides/authentication |
| 26 | +# For more information about the client_secrets.json file format, see: |
| 27 | +# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 28 | +CLIENT_SECRETS_FILE = "client_secrets.json" |
| 29 | + |
| 30 | +# This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for |
| 31 | +# authenticated user's account. Any request that retrieves earnings or ad performance metrics must |
| 32 | +# use this scope. |
| 33 | +YOUTUBE_ANALYTICS_MONETARY_READ_SCOPE = ( |
| 34 | + "https://www.googleapis.com/auth/yt-analytics-monetary.readonly") |
| 35 | +YOUTUBE_REPORTING_API_SERVICE_NAME = "youtubereporting" |
| 36 | +YOUTUBE_REPORTING_API_VERSION = "v1" |
| 37 | + |
| 38 | +# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
| 39 | +# missing. |
| 40 | +MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 41 | +WARNING: Please configure OAuth 2.0 |
| 42 | +
|
| 43 | +To make this sample run you will need to populate the client_secrets.json file |
| 44 | +found at: |
| 45 | + %s |
| 46 | +with information from the APIs Console |
| 47 | +https://console.developers.google.com |
| 48 | +
|
| 49 | +For more information about the client_secrets.json file format, please visit: |
| 50 | +https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 51 | +""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 52 | + CLIENT_SECRETS_FILE)) |
| 53 | + |
| 54 | +# Authorize the request and store authorization credentials. |
| 55 | +def get_authenticated_service(args): |
| 56 | + flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_ANALYTICS_MONETARY_READ_SCOPE, |
| 57 | + message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 58 | + |
| 59 | + storage = Storage("%s-oauth2.json" % sys.argv[0]) |
| 60 | + credentials = storage.get() |
| 61 | + |
| 62 | + if credentials is None or credentials.invalid: |
| 63 | + credentials = run_flow(flow, storage, args) |
| 64 | + |
| 65 | + return build(YOUTUBE_REPORTING_API_SERVICE_NAME, YOUTUBE_REPORTING_API_VERSION, |
| 66 | + http=credentials.authorize(httplib2.Http())) |
| 67 | + |
| 68 | + |
| 69 | +# Call the YouTube Reporting API's reportTypes.list method to retrieve report types. |
| 70 | +def list_report_types(youtube_reporting): |
| 71 | + results = youtube_reporting.reportTypes().list().execute() |
| 72 | + reportTypes = results["reportTypes"] |
| 73 | + |
| 74 | + if "reportTypes" in results and results["reportTypes"]: |
| 75 | + reportTypes = results["reportTypes"] |
| 76 | + for reportType in reportTypes: |
| 77 | + print "Report type id: %s\n name: %s\n" % (reportType["id"], reportType["name"]) |
| 78 | + else: |
| 79 | + print "No report types found" |
| 80 | + return False |
| 81 | + |
| 82 | + return True |
| 83 | + |
| 84 | + |
| 85 | +# Call the YouTube Reporting API's jobs.create method to create a job. |
| 86 | +def create_reporting_job(youtube_reporting, report_type_id, name): |
| 87 | + reporting_job = youtube_reporting.jobs().create( |
| 88 | + body=dict( |
| 89 | + reportTypeId=report_type_id, |
| 90 | + name=name |
| 91 | + ) |
| 92 | + ).execute() |
| 93 | + |
| 94 | + print ("Reporting job '%s' created for reporting type '%s' at '%s'" |
| 95 | + % (reporting_job["name"], reporting_job["reportTypeId"], |
| 96 | + reporting_job["createTime"])) |
| 97 | + |
| 98 | + |
| 99 | +# Prompt the user to enter a report type id for the job. Then return the id. |
| 100 | +def get_report_type_id_from_user(): |
| 101 | + report_type_id = raw_input("Please enter the reportTypeId for the job: ") |
| 102 | + print ("You chose '%s' as the report type Id for the job." % report_type_id) |
| 103 | + return report_type_id |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + # The "name" option specifies the name that will be used for the reporting job. |
| 108 | + argparser.add_argument("--name", |
| 109 | + help="Required; name for the reporting job.") |
| 110 | + args = argparser.parse_args() |
| 111 | + |
| 112 | + if not args.name: |
| 113 | + exit("Please specify name using the --name= parameter.") |
| 114 | + |
| 115 | + youtube_reporting = get_authenticated_service(args) |
| 116 | + try: |
| 117 | + if list_report_types(youtube_reporting): |
| 118 | + create_reporting_job(youtube_reporting, get_report_type_id_from_user(), args.name) |
| 119 | + except HttpError, e: |
| 120 | + print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 121 | + else: |
| 122 | + print "Created reporting job." |
0 commit comments