1414from apiclient .errors import HttpError
1515from oauth2client .file import Storage
1616from oauth2client .client import flow_from_clientsecrets
17- from oauth2client .tools import run
18- from optparse import OptionParser
17+ from oauth2client .tools import argparser , run_flow
1918
2019
2120# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
2221
2322# the OAuth 2.0 information for this application, including its client_id and
2423# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
25- # the {{ Google Cloud Console }} at
26- # {{ https://cloud. google.com/console }}.
24+ # the Google Developers Console at
25+ # https://console.developers. google.com/project/_/apiui/credential
2726# Please ensure that you have enabled the YouTube Data API for your project.
2827# For more information about using OAuth2 to access the YouTube Data API, see:
2928# https://developers.google.com/youtube/v3/guides/authentication
4544To make this sample run you will need to populate the client_secrets.json file
4645found at:
4746 %s
48- with information from the APIs Console
49- https://developers.google.com/console
47+ with information from the Developers Console
48+ https://console. developers.google.com
5049
5150For more information about the client_secrets.json file format, please visit:
5251https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
5352""" % os .path .abspath (os .path .join (os .path .dirname (__file__ ),
5453 CLIENT_SECRETS_FILE ))
5554
5655# Authorize the request and store authorization credentials.
57- def get_authenticated_service ():
56+ def get_authenticated_service (args ):
5857 flow = flow_from_clientsecrets (CLIENT_SECRETS_FILE ,
5958 scope = YOUTUBE_READ_WRITE_SCOPE ,
6059 message = MISSING_CLIENT_SECRETS_MESSAGE )
@@ -63,7 +62,7 @@ def get_authenticated_service():
6362 credentials = storage .get ()
6463
6564 if credentials is None or credentials .invalid :
66- credentials = run (flow , storage )
65+ credentials = run_flow (flow , storage , args )
6766
6867 return build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION ,
6968 http = credentials .authorize (httplib2 .Http ()))
@@ -76,38 +75,37 @@ def set_watermark(youtube, channel_id, file, metadata):
7675 youtube .watermarks ().set (
7776 channelId = channel_id ,
7877 media_body = file ,
79- body = metadata ,
78+ body = metadata ,
8079 ).execute ()
8180 except HttpError as e :
8281 print "Error while setting watermark: %s" % e .content
8382 raise e
8483
8584
8685if __name__ == "__main__" :
87- parser = OptionParser ()
8886 # The "channelid" option specifies the YouTube channel ID that uniquely
8987 # identifies the channel for which the watermark image is being updated.
90- parser . add_option ("--channelid" , dest = "channelid" ,
88+ argparser . add_argument ("--channelid" , dest = "channelid" ,
9189 help = "Required; ID for channel that is having its watermark updated." )
9290 # The "file" option specifies the path to the image being uploaded.
93- parser . add_option ("--file" , dest = "file" ,
91+ argparser . add_argument ("--file" , dest = "file" ,
9492 help = "Required; path to watermark image file." )
9593 # The "metadata" option specifies the JSON for the watermark resource
9694 # provided with the request.
97- parser . add_option ("--metadata" , dest = "metadata" ,
95+ argparser . add_argument ("--metadata" , dest = "metadata" ,
9896 help = "Required; watermark metadata in JSON format." )
99- ( options , args ) = parser .parse_args ()
100-
101- if not options .channelid :
102- parser .print_help ()
97+ args = argparser .parse_args ()
98+
99+ if not args .channelid :
100+ argparser .print_help ()
103101 exit ()
104102
105- youtube = get_authenticated_service ()
103+ youtube = get_authenticated_service (args )
106104
107- if not options .file or not os .path .exists (options .file ):
105+ if not args .file or not os .path .exists (args .file ):
108106 exit ("Please specify a valid file using the --file= parameter." )
109- if not options .metadata :
107+ if not args .metadata :
110108 exit ("Please specify watermark metadata using the --metadata= parameter." )
111- set_watermark (youtube , options .channelid , options .file ,
112- json .loads (options .metadata ))
109+ set_watermark (youtube , args .channelid , args .file ,
110+ json .loads (args .metadata ))
113111 print "The watermark was successfully set."
0 commit comments