|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
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 |
4 | 7 | import os
|
5 |
| -import sys |
6 | 8 |
|
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 |
12 | 14 |
|
13 | 15 |
|
14 | 16 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
|
|
21 | 23 | # https://developers.google.com/youtube/v3/guides/authentication
|
22 | 24 | # For more information about the client_secrets.json file format, see:
|
23 | 25 | # 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' |
25 | 27 |
|
26 | 28 | # This OAuth 2.0 access scope allows for full read/write access to the
|
27 | 29 | # 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' |
49 | 33 |
|
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) |
54 | 39 |
|
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): |
65 | 41 | # Call the API's videos.list method to retrieve the video resource.
|
66 | 42 | videos_list_response = youtube.videos().list(
|
67 |
| - id=options.video_id, |
| 43 | + id=args.video_id, |
68 | 44 | part='snippet'
|
69 | 45 | ).execute()
|
70 | 46 |
|
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 |
72 | 48 | # 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 |
75 | 51 | sys.exit(1)
|
76 | 52 |
|
77 | 53 | # Since the request specified a video ID, the response only contains one
|
78 | 54 | # 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 |
80 | 62 |
|
81 | 63 | # Preserve any tags already associated with the video. If the video does
|
82 | 64 | # not have any tags, create a new array. Append the provided tag to the
|
83 | 65 | # 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); |
87 | 74 |
|
88 | 75 | # Update the video resource by calling the videos.update() method.
|
89 | 76 | videos_update_response = youtube.videos().update(
|
90 | 77 | part='snippet',
|
91 | 78 | body=dict(
|
92 | 79 | snippet=videos_list_snippet,
|
93 |
| - id=options.video_id |
| 80 | + id=args.video_id |
94 | 81 | )).execute()
|
95 | 82 |
|
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.', |
98 | 94 | 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() |
104 | 105 | try:
|
105 | 106 | update_video(youtube, args)
|
106 | 107 | 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