Skip to content

Commit 84d7973

Browse files
"Added sample: python/update_video.py"
1 parent 4d8495c commit 84d7973

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

python/update_video.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
CLIENT_SECRETS_FILE = "client_secrets.json"
25+
26+
# This OAuth 2.0 access scope allows for full read/write access to the
27+
# authenticated user's account.
28+
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
29+
YOUTUBE_API_SERVICE_NAME = "youtube"
30+
YOUTUBE_API_VERSION = "v3"
31+
32+
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
33+
# missing.
34+
MISSING_CLIENT_SECRETS_MESSAGE = """
35+
WARNING: Please configure OAuth 2.0
36+
37+
To make this sample run you will need to populate the client_secrets.json file
38+
found at:
39+
40+
%s
41+
42+
with information from the {{ Cloud Console }}
43+
{{ https://cloud.google.com/console }}
44+
45+
For more information about the client_secrets.json file format, please visit:
46+
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
47+
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
48+
CLIENT_SECRETS_FILE))
49+
50+
def get_authenticated_service(args):
51+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
52+
scope=YOUTUBE_READ_WRITE_SCOPE,
53+
message=MISSING_CLIENT_SECRETS_MESSAGE)
54+
55+
storage = Storage("%s-oauth2.json" % sys.argv[0])
56+
credentials = storage.get()
57+
58+
if credentials is None or credentials.invalid:
59+
credentials = run_flow(flow, storage, args)
60+
61+
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
62+
http=credentials.authorize(httplib2.Http()))
63+
64+
def update_video(youtube, options):
65+
# Call the API's videos.list method to retrieve the video resource.
66+
videos_list_response = youtube.videos().list(
67+
id=options.video_id,
68+
part='snippet'
69+
).execute()
70+
71+
# If the response does not contain an array of "items" then the video was
72+
# not found.
73+
if not videos_list_response["items"]:
74+
print "Video '%s' was not found." % options.video_id
75+
sys.exit(1)
76+
77+
# Since the request specified a video ID, the response only contains one
78+
# video resource. This code extracts the snippet from that resource.
79+
videos_list_snippet = videos_list_response["items"][0]["snippet"]
80+
81+
# Preserve any tags already associated with the video. If the video does
82+
# not have any tags, create a new array. Append the provided tag to the
83+
# list of tags associated with the video.
84+
if "tags" not in videos_list_snippet:
85+
videos_list_snippet["tags"] = []
86+
videos_list_snippet["tags"].append(options.tag)
87+
88+
# Update the video resource by calling the videos.update() method.
89+
videos_update_response = youtube.videos().update(
90+
part='snippet',
91+
body=dict(
92+
snippet=videos_list_snippet,
93+
id=options.video_id
94+
)).execute()
95+
96+
if __name__ == "__main__":
97+
argparser.add_argument("--video-id", help="ID of video to update.",
98+
required=True)
99+
argparser.add_argument("--tag", default="youtube",
100+
help="Additional tag to add to video.")
101+
args = argparser.parse_args()
102+
103+
youtube = get_authenticated_service(args)
104+
try:
105+
update_video(youtube, args)
106+
except HttpError, e:
107+
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
108+
else:
109+
print "Tag '%s' was added to video id '%s'." % (args.tag, args.video_id)

0 commit comments

Comments
 (0)