Skip to content

Commit 2217aa6

Browse files
"Added sample: python/channel_localizations.py"
1 parent 6781301 commit 2217aa6

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

python/channel_localizations.py

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/usr/bin/python
2+
3+
# Usage example:
4+
# python channel_localizations.py --action='<action>' --channel_id='<channel_id>' --default_language='<default_language>' --language='<language>' --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 channels.update method to update an existing channel's default language,
68+
# and localized description in a specific language.
69+
def set_channel_localization(youtube, channel_id, default_language, language, description):
70+
results = youtube.channels().list(
71+
part="brandingSettings,localizations",
72+
id=channel_id
73+
).execute()
74+
75+
channel = results["items"][0]
76+
# Ensure that a value is set for the resource's snippet.defaultLanguage property.
77+
# To set the snippet.defaultLanguage property for a channel resource,
78+
# you actually need to update the brandingSettings.channel.defaultLanguage property.
79+
channel["brandingSettings"]["channel"]["defaultLanguage"] = default_language
80+
if "localizations" not in channel:
81+
channel["localizations"] = {}
82+
channel["localizations"][language] = {
83+
"description": description
84+
}
85+
86+
update_result = youtube.channels().update(
87+
part="brandingSettings,localizations",
88+
body=channel
89+
).execute()
90+
91+
localization = update_result["localizations"][language]
92+
93+
print ("Updated channel '%s' default language to '%s', localized description"
94+
" to '%s' in language '%s'" % (channel_id, localization["description"], language))
95+
96+
97+
# Call the API's channels.list method to retrieve an existing channel 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_channel_localization(youtube, channel_id, language):
101+
results = youtube.channels().list(
102+
part="snippet",
103+
id=channel_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 "Channel description is '%s' in language '%s'" % (localized["description"], language)
113+
114+
115+
# Call the API's channels.list method to list the existing channel localizations.
116+
def list_channel_localizations(youtube, channel_id):
117+
results = youtube.channels().list(
118+
part="snippet,localizations",
119+
id=channel_id
120+
).execute()
121+
122+
localizations = results["items"][0]["localizations"]
123+
124+
for language, localization in localizations.iteritems():
125+
print "Channel description is '%s' in language '%s'" % (localization["description"], language)
126+
127+
128+
if __name__ == "__main__":
129+
# The "action" option specifies the action to be processed.
130+
argparser.add_argument("--action", help="Action")
131+
# The "channel_id" option specifies the ID of the selected YouTube channel.
132+
argparser.add_argument("--channel_id",
133+
help="ID for channel for which the localization will be applied.")
134+
# The "default_language" option specifies the language of the channel's default metadata.
135+
argparser.add_argument("--default_language", help="Default language of the channel to update.",
136+
default="en")
137+
# The "language" option specifies the language of the localization that is being processed.
138+
argparser.add_argument("--language", help="Language of the localization.", default="de")
139+
# The "description" option specifies the localized description of the chanel to be set.
140+
argparser.add_argument("--description", help="Localized description of the channel to be set.",
141+
default="Localized Description")
142+
143+
args = argparser.parse_args()
144+
145+
if not args.channel_id:
146+
exit("Please specify channel id using the --channel_id= parameter.")
147+
148+
youtube = get_authenticated_service(args)
149+
try:
150+
if args.action == 'set':
151+
set_channel_localization(youtube, args.channel_id, args.default_language, args.language, args.description)
152+
elif args.action == 'get':
153+
get_channel_localization(youtube, args.channel_id, args.language)
154+
elif args.action == 'list':
155+
list_channel_localizations(youtube, args.channel_id)
156+
else:
157+
exit("Please specify a valid action using the --action= parameter.")
158+
except HttpError, e:
159+
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
160+
else:
161+
print "Set and retrieved localized metadata for a channel."

0 commit comments

Comments
 (0)