Skip to content

Commit 3cf79a5

Browse files
"Added sample: python/add_featured_video.py"
1 parent 52cd7af commit 3cf79a5

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

python/add_featured_video.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/python
2+
3+
import httplib2
4+
import os
5+
import sys
6+
7+
from apiclient.discovery import build
8+
from apiclient.errors import HttpError
9+
from oauth2client.client import flow_from_clientsecrets
10+
from oauth2client.file import Storage
11+
from oauth2client.tools import argparser, run_flow
12+
13+
14+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
15+
# the OAuth 2.0 information for this application, including its client_id and
16+
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
17+
# the {{ Google Cloud Console }} at
18+
# {{ https://cloud.google.com/console }}.
19+
# Please ensure that you have enabled the YouTube Data API for your project.
20+
# For more information about using OAuth2 to access the YouTube Data API, see:
21+
# https://developers.google.com/youtube/v3/guides/authentication
22+
# For more information about the client_secrets.json file format, see:
23+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
24+
25+
CLIENT_SECRETS_FILE = "client_secrets.json"
26+
27+
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
28+
# missing.
29+
MISSING_CLIENT_SECRETS_MESSAGE = """
30+
WARNING: Please configure OAuth 2.0
31+
32+
To make this sample run you will need to populate the client_secrets.json file
33+
found at:
34+
35+
%s
36+
37+
with information from the {{ Cloud Console }}
38+
{{ https://cloud.google.com/console }}
39+
40+
For more information about the client_secrets.json file format, please visit:
41+
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
42+
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
43+
CLIENT_SECRETS_FILE))
44+
45+
# This OAuth 2.0 access scope allows for full read/write access to the
46+
# authenticated user's account.
47+
YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube"
48+
YOUTUBE_API_SERVICE_NAME = "youtube"
49+
YOUTUBE_API_VERSION = "v3"
50+
51+
# If offsetMs is not valid, the API will throw an error
52+
VALID_OFFSET_TYPES = ("offsetFromEnd", "offsetFromStart",)
53+
54+
def get_authenticated_service(args):
55+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE,
56+
message=MISSING_CLIENT_SECRETS_MESSAGE)
57+
58+
storage = Storage("%s-oauth2.json" % sys.argv[0])
59+
credentials = storage.get()
60+
61+
if credentials is None or credentials.invalid:
62+
credentials = run_flow(flow, storage, args)
63+
64+
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
65+
http=credentials.authorize(httplib2.Http()))
66+
67+
def add_featured_video(youtube, options):
68+
add_video_request = youtube.channels().update(
69+
part="invideoPromotion",
70+
# You can use the API Explorer to test API requests:
71+
# https://developers.google.com/youtube/v3/docs/channels/update#try-it
72+
body={
73+
"invideoPromotion": {
74+
"items": [{
75+
"id": {
76+
"type": "video",
77+
"videoId": options.video_id
78+
},
79+
"timing": {
80+
"offsetMs": options.offset_ms,
81+
"type": options.offset_type
82+
}
83+
}],
84+
},
85+
"id": options.channel_id
86+
}).execute()
87+
88+
if __name__ == '__main__':
89+
argparser.add_argument("--channel-id", required=True,
90+
help="Channel ID of the channel to add a featured video")
91+
argparser.add_argument("--video-id", required=True,
92+
help="Video ID to feature on your channel")
93+
argparser.add_argument("--offset-ms",
94+
help="Offset in milliseconds to show video.",
95+
default="10000")
96+
argparser.add_argument("--offset-type", choices=VALID_OFFSET_TYPES,
97+
help="Whether the offset is from the beginning or end of video playback.",
98+
default=VALID_OFFSET_TYPES[0])
99+
args = argparser.parse_args()
100+
101+
youtube = get_authenticated_service(args)
102+
try:
103+
add_featured_video(youtube, args)
104+
except HttpError, e:
105+
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
106+
else:
107+
print "Added featured video %s to channel %s." % (
108+
args.video_id, args.channel_id)

0 commit comments

Comments
 (0)