Skip to content

Commit 983084d

Browse files
committed
"Added sample: python/add_channel_section.py"
1 parent 4b1f8f6 commit 983084d

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

python/add_channel_section.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/python
2+
3+
import httplib2
4+
import os
5+
import re
6+
import sys
7+
8+
from apiclient.discovery import build
9+
from apiclient.errors import HttpError
10+
from oauth2client.client import flow_from_clientsecrets
11+
from oauth2client.file import Storage
12+
from oauth2client.tools import argparser, run_flow
13+
14+
15+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
16+
# the OAuth 2.0 information for this application, including its client_id and
17+
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
18+
# the {{ Google Cloud Console }} at
19+
# {{ https://cloud.google.com/console }}.
20+
# Please ensure that you have enabled the YouTube Data API for your project.
21+
# For more information about using OAuth2 to access the YouTube Data API, see:
22+
# https://developers.google.com/youtube/v3/guides/authentication
23+
# For more information about the client_secrets.json file format, see:
24+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
25+
26+
CLIENT_SECRETS_FILE = "client_secrets.json"
27+
28+
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
29+
# missing.
30+
MISSING_CLIENT_SECRETS_MESSAGE = """
31+
WARNING: Please configure OAuth 2.0
32+
33+
To make this sample run you will need to populate the client_secrets.json file
34+
found at:
35+
36+
%s
37+
38+
with information from the {{ Cloud Console }}
39+
{{ https://cloud.google.com/console }}
40+
41+
For more information about the client_secrets.json file format, please visit:
42+
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
43+
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
44+
CLIENT_SECRETS_FILE))
45+
46+
# This OAuth 2.0 access scope allows for full read/write access to the
47+
# authenticated user's account.
48+
YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube"
49+
YOUTUBE_API_SERVICE_NAME = "youtube"
50+
YOUTUBE_API_VERSION = "v3"
51+
52+
SECTION_TYPES = ("allPlaylists", "completedEvents", "likedPlaylists",
53+
"likes", "liveEvents", "multipleChannels", "multiplePlaylists",
54+
"popularUploads", "recentActivity", "recentPosts", "recentUploads",
55+
"singlePlaylist", "upcomingEvents",)
56+
SECTION_STYLES = ("horizontalRow", "verticalList",)
57+
58+
def get_authenticated_service(args):
59+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE,
60+
message=MISSING_CLIENT_SECRETS_MESSAGE)
61+
62+
storage = Storage("%s-oauth2.json" % sys.argv[0])
63+
credentials = storage.get()
64+
65+
if credentials is None or credentials.invalid:
66+
credentials = run_flow(flow, storage, args)
67+
68+
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
69+
http=credentials.authorize(httplib2.Http()))
70+
71+
def enable_browse_view(youtube):
72+
channels_list_response = youtube.channels().list(
73+
part="brandingSettings",
74+
mine=True
75+
).execute()
76+
77+
channel = channels_list_response["items"][0]
78+
channel["brandingSettings"]["channel"]["showBrowseView"] = True
79+
80+
youtube.channels().update(
81+
part="brandingSettings",
82+
body=channel
83+
).execute()
84+
85+
def add_channel_section(youtube, args):
86+
channels = None
87+
if args.channels:
88+
channels = re.split("\s*,\s*", args.channels)
89+
playlists = None
90+
if args.playlists:
91+
playlists = re.split("\s*,\s*", args.playlists)
92+
93+
body = dict(
94+
snippet=dict(
95+
type=args.type,
96+
style=args.style,
97+
title=args.title,
98+
position=args.position
99+
),
100+
contentDetails=dict(
101+
channels=channels,
102+
playlists=playlists
103+
)
104+
)
105+
106+
youtube.channelSections().insert(
107+
part="snippet,contentDetails",
108+
body=body
109+
).execute()
110+
111+
if __name__ == '__main__':
112+
argparser.add_argument("--type", choices=SECTION_TYPES, required=True,
113+
help="The type of the section to be added.")
114+
argparser.add_argument("--style", choices=SECTION_STYLES, required=True,
115+
help="The style of the section to be added.")
116+
argparser.add_argument("--title",
117+
help=("The title to display for the new section. This is only used "
118+
"with the multiplePlaylists or multipleChannels section types."))
119+
argparser.add_argument("--position", type=int,
120+
help=("The position of the new section. "
121+
"Use 0 for the top, or don't set a value for the bottom."))
122+
argparser.add_argument("--playlists",
123+
help="One or more playlist ids, comma-separated (e.g. PL...).")
124+
argparser.add_argument("--channels",
125+
help="One or more channel ids, comma-separated (e.g. UC...).")
126+
args = argparser.parse_args()
127+
128+
youtube = get_authenticated_service(args)
129+
try:
130+
# Before channel shelves will appear on your channel's web page, browse
131+
# view needs to be enabled. If you know that your channel already has
132+
# it enabled, or if you want to add a number of sections before enabling it,
133+
# you can skip the call to enable_browse_view().
134+
enable_browse_view(youtube)
135+
136+
add_channel_section(youtube, args)
137+
except HttpError, e:
138+
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
139+
else:
140+
print "Added new channel section."

0 commit comments

Comments
 (0)