|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Usage example: |
| 4 | +# python video_localizations.py --action='<action>' --video_id='<video_id>' --default_language='<default_language>' --language='<language>' --title='<title>' --description='<description>' |
| 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 full read/write access to the |
| 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)) |
| 51 | + |
| 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())) |
| 65 | + |
| 66 | + |
| 67 | +# Call the API's videos.update method to update an existing video's default language, |
| 68 | +# localized title and description in a specific language. |
| 69 | +def set_video_localization(youtube, video_id, default_language, language, title, description): |
| 70 | + results = youtube.videos().list( |
| 71 | + part="snippet,localizations", |
| 72 | + id=video_id |
| 73 | + ).execute() |
| 74 | + |
| 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 | + |
| 85 | + 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 | + |
| 96 | + |
| 97 | +# Call the API's videos.list method to retrieve an existing video localization. |
| 98 | +# 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): |
| 101 | + results = youtube.videos().list( |
| 102 | + part="snippet", |
| 103 | + id=video_id, |
| 104 | + hl=language |
| 105 | + ).execute() |
| 106 | + |
| 107 | + # The localized object contains localized text if the hl parameter specified |
| 108 | + # 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"] |
| 111 | + |
| 112 | + print ("Video title is '%s' and description is '%s' in language '%s'" |
| 113 | + % (localized["title"], localized["description"], language)) |
| 114 | + |
| 115 | + |
| 116 | +# Call the API's videos.list method to list the existing video localizations. |
| 117 | +def list_video_localizations(youtube, video_id): |
| 118 | + results = youtube.videos().list( |
| 119 | + part="snippet,localizations", |
| 120 | + id=video_id |
| 121 | + ).execute() |
| 122 | + |
| 123 | + localizations = results["items"][0]["localizations"] |
| 124 | + |
| 125 | + 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() |
| 149 | + |
| 150 | + if not args.video_id: |
| 151 | + exit("Please specify video id using the --video_id= parameter.") |
| 152 | + |
| 153 | + youtube = get_authenticated_service(args) |
| 154 | + try: |
| 155 | + if args.action == 'set': |
| 156 | + set_video_localization(youtube, args.video_id, args.default_language, args.language, args.title, args.description) |
| 157 | + elif args.action == 'get': |
| 158 | + get_video_localization(youtube, args.video_id, args.language) |
| 159 | + 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.") |
| 163 | + except HttpError, e: |
| 164 | + print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 165 | + else: |
| 166 | + print "Set and retrieved localized metadata for a video." |
0 commit comments