Skip to content

Commit 2ac0c7e

Browse files
Update auth libraries, add snippet metadata opts
The oauth2client library is deprecated. This update changes the sample to use the google-auth and google-auth-oauthlib libraries instead. In addition, the code now supports other arguments for updating snippet metadata (title, description, tags). The --tags argument replaces the existing tags, while the --add_tag argument appends a tag to the current list.
1 parent f9ea8bf commit 2ac0c7e

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

python/update_video.py

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/usr/bin/python
22

3-
import httplib2
3+
# Update the snippet metadata for a video. Sample usage:
4+
# python update_video.py --video_id=<VIDEO_ID> --tags="<TAG1, TAG2>" --title="New title" --description="New description"
5+
6+
import argparse
47
import os
5-
import sys
68

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
9+
import google.oauth2.credentials
10+
import google_auth_oauthlib.flow
11+
from googleapiclient.discovery import build
12+
from googleapiclient.errors import HttpError
13+
from google_auth_oauthlib.flow import InstalledAppFlow
1214

1315

1416
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
@@ -21,89 +23,87 @@
2123
# https://developers.google.com/youtube/v3/guides/authentication
2224
# For more information about the client_secrets.json file format, see:
2325
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
24-
CLIENT_SECRETS_FILE = "client_secrets.json"
26+
CLIENT_SECRETS_FILE = 'client_secret.json'
2527

2628
# This OAuth 2.0 access scope allows for full read/write access to the
2729
# 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))
30+
SCOPES = ['https://www.googleapis.com/auth/youtube']
31+
API_SERVICE_NAME = 'youtube'
32+
API_VERSION = 'v3'
4933

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)
34+
# Authorize the request and store authorization credentials.
35+
def get_authenticated_service():
36+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
37+
credentials = flow.run_console()
38+
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
5439

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):
40+
def update_video(youtube, args):
6541
# Call the API's videos.list method to retrieve the video resource.
6642
videos_list_response = youtube.videos().list(
67-
id=options.video_id,
43+
id=args.video_id,
6844
part='snippet'
6945
).execute()
7046

71-
# If the response does not contain an array of "items" then the video was
47+
# If the response does not contain an array of 'items' then the video was
7248
# not found.
73-
if not videos_list_response["items"]:
74-
print "Video '%s' was not found." % options.video_id
49+
if not videos_list_response['items']:
50+
print 'Video "%s" was not found.' % args.video_id
7551
sys.exit(1)
7652

7753
# Since the request specified a video ID, the response only contains one
7854
# video resource. This code extracts the snippet from that resource.
79-
videos_list_snippet = videos_list_response["items"][0]["snippet"]
55+
videos_list_snippet = videos_list_response['items'][0]['snippet']
56+
57+
# Set video title, description, default language if specified in args.
58+
if args.title:
59+
videos_list_snippet['title'] = args.title
60+
if args.description:
61+
videos_list_snippet['description'] = args.description
8062

8163
# Preserve any tags already associated with the video. If the video does
8264
# not have any tags, create a new array. Append the provided tag to the
8365
# 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)
66+
if 'tags' not in videos_list_snippet:
67+
videos_list_snippet['tags'] = []
68+
if args.tags:
69+
videos_list_snippet['tags'] = args.tags.split(',')
70+
elif args.add_tag:
71+
videos_list_snippet['tags'].append(args.add_tag)
72+
73+
print(videos_list_snippet);
8774

8875
# Update the video resource by calling the videos.update() method.
8976
videos_update_response = youtube.videos().update(
9077
part='snippet',
9178
body=dict(
9279
snippet=videos_list_snippet,
93-
id=options.video_id
80+
id=args.video_id
9481
)).execute()
9582

96-
if __name__ == "__main__":
97-
argparser.add_argument("--video-id", help="ID of video to update.",
83+
print('The updated video metadata is:\n' +
84+
'Title: ' + videos_update_response['snippet']['title'] + '\n')
85+
if videos_update_response['snippet']['description']:
86+
print ('Description: ' +
87+
videos_update_response['snippet']['description'] + '\n')
88+
if videos_update_response['snippet']['tags']:
89+
print ('Tags: ' + ','.join(videos_update_response['snippet']['tags']) + '\n')
90+
91+
if __name__ == '__main__':
92+
parser = argparse.ArgumentParser()
93+
parser.add_argument('--video_id', help='ID of video to update.',
9894
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)
95+
parser.add_argument('--tags',
96+
help='Comma-separated list of tags relevant to the video. This argument ' +
97+
'replaces the existing list of tags.')
98+
parser.add_argument('--add_tag', help='Additional tag to add to video. ' +
99+
'This argument does not affect current tags.')
100+
parser.add_argument('--title', help='Title of the video.')
101+
parser.add_argument('--description', help='Description of the video.')
102+
args = parser.parse_args()
103+
104+
youtube = get_authenticated_service()
104105
try:
105106
update_video(youtube, args)
106107
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)
108+
print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)
109+
print 'Tag "%s" was added to video id "%s".' % (args.add_tag, args.video_id)

0 commit comments

Comments
 (0)