|
3 | 3 | # Usage example:
|
4 | 4 | # python video_localizations.py --action='<action>' --video_id='<video_id>' --default_language='<default_language>' --language='<language>' --title='<title>' --description='<description>'
|
5 | 5 |
|
6 |
| -import httplib2 |
| 6 | +import argparse |
7 | 7 | import os
|
8 |
| -import sys |
| 8 | +import re |
9 | 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 |
| 10 | +import google.oauth2.credentials |
| 11 | +import google_auth_oauthlib.flow |
| 12 | +from googleapiclient.discovery import build |
| 13 | +from googleapiclient.errors import HttpError |
| 14 | +from google_auth_oauthlib.flow import InstalledAppFlow |
15 | 15 |
|
16 | 16 |
|
17 | 17 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
|
|
25 | 25 | # https://developers.google.com/youtube/v3/guides/authentication
|
26 | 26 | # For more information about the client_secrets.json file format, see:
|
27 | 27 | # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
|
28 |
| -CLIENT_SECRETS_FILE = "client_secrets.json" |
| 28 | +CLIENT_SECRETS_FILE = 'client_secret.json' |
29 | 29 |
|
30 | 30 | # This OAuth 2.0 access scope allows for full read/write access to the
|
31 | 31 | # authenticated user's account.
|
32 |
| -YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" |
33 |
| -YOUTUBE_API_SERVICE_NAME = "youtube" |
34 |
| -YOUTUBE_API_VERSION = "v3" |
35 |
| - |
36 |
| -# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
37 |
| -# missing. |
38 |
| -MISSING_CLIENT_SECRETS_MESSAGE = """ |
39 |
| -WARNING: Please configure OAuth 2.0 |
40 |
| -
|
41 |
| -To make this sample run you will need to populate the client_secrets.json file |
42 |
| -found at: |
43 |
| - %s |
44 |
| -with information from the APIs Console |
45 |
| -https://console.developers.google.com |
46 |
| -
|
47 |
| -For more information about the client_secrets.json file format, please visit: |
48 |
| -https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
49 |
| -""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
50 |
| - CLIENT_SECRETS_FILE)) |
| 32 | +SCOPES = ['https://www.googleapis.com/auth/youtube'] |
| 33 | +API_SERVICE_NAME = 'youtube' |
| 34 | +API_VERSION = 'v3' |
51 | 35 |
|
52 |
| -# Authorize the request and store authorization credentials. |
53 |
| -def get_authenticated_service(args): |
54 |
| - flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SCOPE, |
55 |
| - message=MISSING_CLIENT_SECRETS_MESSAGE) |
56 |
| - |
57 |
| - storage = Storage("%s-oauth2.json" % sys.argv[0]) |
58 |
| - credentials = storage.get() |
59 |
| - |
60 |
| - if credentials is None or credentials.invalid: |
61 |
| - credentials = run_flow(flow, storage, args) |
62 |
| - |
63 |
| - return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
64 |
| - http=credentials.authorize(httplib2.Http())) |
| 36 | +# Supported actions |
| 37 | +ACTIONS = ('get', 'list', 'set') |
65 | 38 |
|
| 39 | +# Authorize the request and store authorization credentials. |
| 40 | +def get_authenticated_service(): |
| 41 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
| 42 | + credentials = flow.run_console() |
| 43 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
66 | 44 |
|
67 | 45 | # Call the API's videos.update method to update an existing video's default language,
|
68 | 46 | # localized title and description in a specific language.
|
69 |
| -def set_video_localization(youtube, video_id, default_language, language, title, description): |
| 47 | +def set_video_localization(youtube, args): |
| 48 | + |
| 49 | + # Retrieve the snippet and localizations for the video. |
70 | 50 | results = youtube.videos().list(
|
71 |
| - part="snippet,localizations", |
72 |
| - id=video_id |
| 51 | + part='snippet,localizations', |
| 52 | + id=args.video_id |
73 | 53 | ).execute()
|
74 | 54 |
|
75 |
| - video = results["items"][0] |
76 |
| - # Ensure that a value is set for the resource's snippet.defaultLanguage property. |
77 |
| - video["snippet"]["defaultLanguage"] = default_language |
78 |
| - if "localizations" not in video: |
79 |
| - video["localizations"] = {} |
80 |
| - video["localizations"][language] = { |
81 |
| - "title": title, |
82 |
| - "description": description |
83 |
| - } |
84 |
| - |
| 55 | + video = results['items'][0] |
| 56 | + |
| 57 | + # If the language argument is set, set the localized title and description |
| 58 | + # for that language. The "title" and "description" arguments have default |
| 59 | + # values to make the script simpler to run as a demo. In an actual app, you |
| 60 | + # would likely want to set those arguments also. |
| 61 | + if args.language and args.language != '': |
| 62 | + if 'localizations' not in video: |
| 63 | + video['localizations'] = {} |
| 64 | + |
| 65 | + video['localizations'][args.language] = { |
| 66 | + 'title': args.title, |
| 67 | + 'description': args.description |
| 68 | + } |
| 69 | + |
| 70 | + # If the default language is set AND there is localized metadata for that |
| 71 | + # language, set the video's title and description to match the localized |
| 72 | + # title and description for the newly set default language. |
| 73 | + if args.default_language and args.default_language in video['localizations']: |
| 74 | + video['snippet']['defaultLanguage'] = args.default_language |
| 75 | + video['snippet']['title'] = ( |
| 76 | + video['localizations'][args.default_language]['title']) |
| 77 | + video['snippet']['description'] = ( |
| 78 | + video['localizations'][args.default_language]['description']) |
| 79 | + |
| 80 | + # Update the video resource. |
85 | 81 | update_result = youtube.videos().update(
|
86 |
| - part="snippet,localizations", |
87 |
| - body=video |
88 |
| - ).execute() |
89 |
| - |
90 |
| - localization = update_result["localizations"][language] |
91 |
| - |
92 |
| - print ("Updated video '%s' default language to '%s', localized title to '%s'" |
93 |
| - " and description to '%s' in language '%s'" |
94 |
| - % (video_id, default_language, localization["title"], localization["description"], language)) |
95 |
| - |
| 82 | + part='snippet,localizations', |
| 83 | + body=video |
| 84 | + ).execute() |
| 85 | + |
| 86 | + # Print the actions taken by running the script. |
| 87 | + if args.language: |
| 88 | + for language in update_result['localizations']: |
| 89 | + # Languages with locales, like "pt-br" are returned as pt-BR in metadata. |
| 90 | + # This ensures that the language specified when running the script can be |
| 91 | + # matched to the language returned in the metadata. |
| 92 | + if language.lower() == args.language.lower(): |
| 93 | + localization = update_result['localizations'][args.language] |
| 94 | + print ('Updated video \'%s\' localized title to \'%s\'' |
| 95 | + ' and description to \'%s\' in language \'%s\'' % |
| 96 | + (args.video_id, |
| 97 | + localization['title'], |
| 98 | + localization['description'], |
| 99 | + args.language)) |
| 100 | + break |
| 101 | + |
| 102 | + if (args.default_language and |
| 103 | + args.default_language == update_result['snippet']['defaultLanguage']): |
| 104 | + print 'Updated default language to %s' % args.default_language |
96 | 105 |
|
97 | 106 | # Call the API's videos.list method to retrieve an existing video localization.
|
98 | 107 | # If the localized text is not available in the requested language,
|
99 |
| -# this method will return text in the default language. |
100 |
| -def get_video_localization(youtube, video_id, language): |
| 108 | +# this method returns text in the default language. |
| 109 | +def get_video_localization(youtube, args): |
101 | 110 | results = youtube.videos().list(
|
102 |
| - part="snippet", |
103 |
| - id=video_id, |
104 |
| - hl=language |
| 111 | + part='snippet', |
| 112 | + id=args.video_id, |
| 113 | + hl=args.language |
105 | 114 | ).execute()
|
106 | 115 |
|
107 | 116 | # The localized object contains localized text if the hl parameter specified
|
108 | 117 | # a language for which localized text is available. Otherwise, the localized
|
109 |
| - # object will contain metadata in the default language. |
110 |
| - localized = results["items"][0]["snippet"]["localized"] |
| 118 | + # object contains metadata in the default language. |
| 119 | + localized = results['items'][0]['snippet']['localized'] |
111 | 120 |
|
112 |
| - print ("Video title is '%s' and description is '%s' in language '%s'" |
113 |
| - % (localized["title"], localized["description"], language)) |
| 121 | + print ('Video title is \'%s\' and description is \'%s\' in language \'%s\'' |
| 122 | + % (localized['title'], localized['description'], args.language)) |
114 | 123 |
|
115 | 124 |
|
116 | 125 | # Call the API's videos.list method to list the existing video localizations.
|
117 |
| -def list_video_localizations(youtube, video_id): |
| 126 | +def list_video_localizations(youtube, args): |
118 | 127 | results = youtube.videos().list(
|
119 |
| - part="snippet,localizations", |
120 |
| - id=video_id |
| 128 | + part='snippet,localizations', |
| 129 | + id=args.video_id |
121 | 130 | ).execute()
|
122 | 131 |
|
123 |
| - localizations = results["items"][0]["localizations"] |
| 132 | + localizations = results['items'][0]['localizations'] |
124 | 133 |
|
125 | 134 | for language, localization in localizations.iteritems():
|
126 |
| - print ("Video title is '%s' and description is '%s' in language '%s'" |
127 |
| - % (localization["title"], localization["description"], language)) |
128 |
| - |
129 |
| - |
130 |
| -if __name__ == "__main__": |
131 |
| - # The "action" option specifies the action to be processed. |
132 |
| - argparser.add_argument("--action", help="Action") |
133 |
| - # The "video_id" option specifies the ID of the selected YouTube video. |
134 |
| - argparser.add_argument("--video_id", |
135 |
| - help="ID for video for which the localization will be applied.") |
136 |
| - # The "default_language" option specifies the language of the video's default metadata. |
137 |
| - argparser.add_argument("--default_language", help="Default language of the video to update.", |
138 |
| - default="en") |
139 |
| - # The "language" option specifies the language of the localization that is being processed. |
140 |
| - argparser.add_argument("--language", help="Language of the localization.", default="de") |
141 |
| - # The "title" option specifies the localized title of the video to be set. |
142 |
| - argparser.add_argument("--title", help="Localized title of the video to be set.", |
143 |
| - default="Localized Title") |
144 |
| - # The "description" option specifies the localized description of the video to be set. |
145 |
| - argparser.add_argument("--description", help="Localized description of the video to be set.", |
146 |
| - default="Localized Description") |
147 |
| - |
148 |
| - args = argparser.parse_args() |
| 135 | + print ('Video title is \'%s\' and description is \'%s\' in language \'%s\'' |
| 136 | + % (localization['title'], localization['description'], language)) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + parser = argparse.ArgumentParser() |
| 141 | + # The action to be processed: 'get', 'list', and 'set' are supported. |
| 142 | + parser.add_argument('--action', help='Action', choices=ACTIONS, required=True) |
| 143 | + # The ID of the selected YouTube video. |
| 144 | + parser.add_argument('--video_id', |
| 145 | + help='The video ID for which localizations are being set or retrieved.', |
| 146 | + required=True) |
| 147 | + # The language of the video's default metadata. |
| 148 | + parser.add_argument('--default_language', |
| 149 | + help='Default language of the video to update.') |
| 150 | + # The language of the localization that is being processed. |
| 151 | + parser.add_argument('--language', help='Language of the localization.') |
| 152 | + # The localized video title for the specified language. |
| 153 | + parser.add_argument('--title', |
| 154 | + help='Localized title of the video to be set.', |
| 155 | + default='Localized Title') |
| 156 | + # The localized description for the specified language. |
| 157 | + parser.add_argument('--description', |
| 158 | + help='Localized description of the video to be set.', |
| 159 | + default='Localized Description') |
| 160 | + |
| 161 | + args = parser.parse_args() |
149 | 162 |
|
150 | 163 | if not args.video_id:
|
151 |
| - exit("Please specify video id using the --video_id= parameter.") |
| 164 | + exit('Please specify video id using the --video_id= parameter.') |
152 | 165 |
|
153 |
| - youtube = get_authenticated_service(args) |
| 166 | + youtube = get_authenticated_service() |
154 | 167 | try:
|
155 | 168 | if args.action == 'set':
|
156 |
| - set_video_localization(youtube, args.video_id, args.default_language, args.language, args.title, args.description) |
| 169 | + set_video_localization(youtube, args) |
157 | 170 | elif args.action == 'get':
|
158 |
| - get_video_localization(youtube, args.video_id, args.language) |
| 171 | + get_video_localization(youtube, args) |
159 | 172 | elif args.action == 'list':
|
160 |
| - list_video_localizations(youtube, args.video_id) |
161 |
| - else: |
162 |
| - exit("Please specify a valid action using the --action= parameter.") |
| 173 | + list_video_localizations(youtube, args) |
163 | 174 | except HttpError, e:
|
164 |
| - print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 175 | + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content) |
165 | 176 | else:
|
166 |
| - print "Set and retrieved localized metadata for a video." |
| 177 | + print 'Set and retrieved localized metadata for a video |
0 commit comments