Skip to content

Commit b72cd98

Browse files
"Added sample: python/channel_section_localizations.py"
1 parent d1fa0b0 commit b72cd98

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/usr/bin/python
2+
3+
# Usage example:
4+
# python channel_sections_localizations.py --action='<action>' --channel_section_id='<channel_section_id>' --default_language='<default_language>' --language='<language>' --title='<title>'
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 channelSections.update method to update an existing channel section's
68+
# default language, and localized title in a specific language.
69+
def set_channel_section_localization(youtube, channel_section_id, default_language, language, title):
70+
results = youtube.channelSections().list(
71+
part="snippet,localizations",
72+
id=channel_section_id
73+
).execute()
74+
75+
channel_section = results["items"][0]
76+
# Ensure that a value is set for the resource's snippet.defaultLanguage property.
77+
channel_section["snippet"]["defaultLanguage"] = default_language
78+
if "localizations" not in channel_section:
79+
channel_section["localizations"] = {}
80+
channel_section["localizations"][language] = {
81+
"title": title
82+
}
83+
84+
update_result = youtube.channelSections().update(
85+
part="snippet,localizations",
86+
body=channel_section
87+
).execute()
88+
89+
localization = update_result["localizations"][language]
90+
91+
print ("Updated channel section '%s' default language to '%s', localized title"
92+
" to '%s' in language '%s'" % (channel_section_id, localization["title"], language))
93+
94+
95+
# Call the API's channelSections.list method to retrieve an existing channel section localization.
96+
# If the localized text is not available in the requested language,
97+
# this method will return text in the default language.
98+
def get_channel_section_localization(youtube, channel_section_id, language):
99+
results = youtube.channelSections().list(
100+
part="snippet",
101+
id=channel_section_id,
102+
hl=language
103+
).execute()
104+
105+
# The localized object contains localized text if the hl parameter specified
106+
# a language for which localized text is available. Otherwise, the localized
107+
# object will contain metadata in the default language.
108+
localized = results["items"][0]["snippet"]["localized"]
109+
110+
print "Channel section title is '%s' in language '%s'" % (localized["title"], language)
111+
112+
113+
# Call the API's channelSections.list method to list the existing channel section localizations.
114+
def list_channel_section_localizations(youtube, channel_section_id):
115+
results = youtube.channelSections().list(
116+
part="snippet,localizations",
117+
id=channel_section_id
118+
).execute()
119+
120+
localizations = results["items"][0]["localizations"]
121+
122+
for language, localization in localizations.iteritems():
123+
print "Channel section title is '%s' in language '%s'" % (localization["title"], language)
124+
125+
126+
if __name__ == "__main__":
127+
# The "action" option specifies the action to be processed.
128+
argparser.add_argument("--action", help="Action")
129+
# The "channel_section_id" option specifies the ID of the selected YouTube channel section.
130+
argparser.add_argument("--channel_section_id",
131+
help="ID for channel section for which the localization will be applied.")
132+
# The "default_language" option specifies the language of the channel section's default metadata.
133+
argparser.add_argument("--default_language",
134+
help="Default language of the channel section to update.", default="en")
135+
# The "language" option specifies the language of the localization that is being processed.
136+
argparser.add_argument("--language", help="Language of the localization.", default="de")
137+
# The "title" option specifies the localized title of the channel section to be set.
138+
argparser.add_argument("--title", help="Localized title of the channel section to be set.",
139+
default="Localized Title")
140+
141+
args = argparser.parse_args()
142+
143+
if not args.channel_section_id:
144+
exit("Please specify channel section id using the --channel_section_id= parameter.")
145+
146+
youtube = get_authenticated_service(args)
147+
try:
148+
if args.action == 'set':
149+
set_channel_section_localization(youtube, args.channel_section_id, args.default_language, args.language, args.title)
150+
elif args.action == 'get':
151+
get_channel_section_localization(youtube, args.channel_section_id, args.language)
152+
elif args.action == 'list':
153+
list_channel_section_localizations(youtube, args.channel_section_id)
154+
else:
155+
exit("Please specify a valid action using the --action= parameter.")
156+
except HttpError, e:
157+
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
158+
else:
159+
print "Set and retrieved localized metadata for a channel section."

0 commit comments

Comments
 (0)