|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
3 |
| -import httplib2 |
| 3 | +# Retrieve the authenticated user's uploaded videos. |
| 4 | +# Sample usage: |
| 5 | +# python my_uploads.py |
| 6 | + |
| 7 | +import argparse |
4 | 8 | import os
|
5 |
| -import sys |
| 9 | +import re |
6 | 10 |
|
7 |
| -from apiclient.discovery import build |
8 |
| -from oauth2client.client import flow_from_clientsecrets |
9 |
| -from oauth2client.file import Storage |
10 |
| -from oauth2client.tools import argparser, run_flow |
| 11 | +import google.oauth2.credentials |
| 12 | +import google_auth_oauthlib.flow |
| 13 | +from googleapiclient.discovery import build |
| 14 | +from googleapiclient.errors import HttpError |
| 15 | +from google_auth_oauthlib.flow import InstalledAppFlow |
11 | 16 |
|
12 | 17 |
|
13 | 18 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
|
|
20 | 25 | # https://developers.google.com/youtube/v3/guides/authentication
|
21 | 26 | # For more information about the client_secrets.json file format, see:
|
22 | 27 | # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
|
23 |
| -CLIENT_SECRETS_FILE = "client_secrets.json" |
24 |
| - |
25 |
| -# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
26 |
| -# missing. |
27 |
| -MISSING_CLIENT_SECRETS_MESSAGE = """ |
28 |
| -WARNING: Please configure OAuth 2.0 |
29 |
| -
|
30 |
| -To make this sample run you will need to populate the client_secrets.json file |
31 |
| -found at: |
32 |
| -
|
33 |
| - %s |
34 |
| -
|
35 |
| -with information from the {{ Cloud Console }} |
36 |
| -{{ https://cloud.google.com/console }} |
37 |
| -
|
38 |
| -For more information about the client_secrets.json file format, please visit: |
39 |
| -https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
40 |
| -""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
41 |
| - CLIENT_SECRETS_FILE)) |
| 28 | +CLIENT_SECRETS_FILE = 'client_secret.json' |
42 | 29 |
|
43 | 30 | # This OAuth 2.0 access scope allows for read-only access to the authenticated
|
44 | 31 | # user's account, but not other types of account access.
|
45 |
| -YOUTUBE_READONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly" |
46 |
| -YOUTUBE_API_SERVICE_NAME = "youtube" |
47 |
| -YOUTUBE_API_VERSION = "v3" |
48 |
| - |
49 |
| -flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, |
50 |
| - message=MISSING_CLIENT_SECRETS_MESSAGE, |
51 |
| - scope=YOUTUBE_READONLY_SCOPE) |
52 |
| - |
53 |
| -storage = Storage("%s-oauth2.json" % sys.argv[0]) |
54 |
| -credentials = storage.get() |
55 |
| - |
56 |
| -if credentials is None or credentials.invalid: |
57 |
| - flags = argparser.parse_args() |
58 |
| - credentials = run_flow(flow, storage, flags) |
59 |
| - |
60 |
| -youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
61 |
| - http=credentials.authorize(httplib2.Http())) |
62 |
| - |
63 |
| -# Retrieve the contentDetails part of the channel resource for the |
64 |
| -# authenticated user's channel. |
65 |
| -channels_response = youtube.channels().list( |
66 |
| - mine=True, |
67 |
| - part="contentDetails" |
68 |
| -).execute() |
69 |
| - |
70 |
| -for channel in channels_response["items"]: |
71 |
| - # From the API response, extract the playlist ID that identifies the list |
72 |
| - # of videos uploaded to the authenticated user's channel. |
73 |
| - uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"] |
74 |
| - |
75 |
| - print "Videos in list %s" % uploads_list_id |
76 |
| - |
| 32 | +SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'] |
| 33 | +API_SERVICE_NAME = 'youtube' |
| 34 | +API_VERSION = 'v3' |
| 35 | + |
| 36 | +# Authorize the request and store authorization credentials. |
| 37 | +def get_authenticated_service(): |
| 38 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
| 39 | + credentials = flow.run_console() |
| 40 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
| 41 | + |
| 42 | +def get_my_uploads_list(): |
| 43 | + # Retrieve the contentDetails part of the channel resource for the |
| 44 | + # authenticated user's channel. |
| 45 | + channels_response = youtube.channels().list( |
| 46 | + mine=True, |
| 47 | + part='contentDetails' |
| 48 | + ).execute() |
| 49 | + |
| 50 | + for channel in channels_response['items']: |
| 51 | + # From the API response, extract the playlist ID that identifies the list |
| 52 | + # of videos uploaded to the authenticated user's channel. |
| 53 | + return channel['contentDetails']['relatedPlaylists']['uploads'] |
| 54 | + |
| 55 | + return None |
| 56 | + |
| 57 | +def list_my_uploaded_videos(uploads_playlist_id): |
77 | 58 | # Retrieve the list of videos uploaded to the authenticated user's channel.
|
78 | 59 | playlistitems_list_request = youtube.playlistItems().list(
|
79 |
| - playlistId=uploads_list_id, |
80 |
| - part="snippet", |
81 |
| - maxResults=50 |
| 60 | + playlistId=uploads_playlist_id, |
| 61 | + part='snippet', |
| 62 | + maxResults=5 |
82 | 63 | )
|
83 | 64 |
|
| 65 | + print 'Videos in list %s' % uploads_playlist_id |
84 | 66 | while playlistitems_list_request:
|
85 | 67 | playlistitems_list_response = playlistitems_list_request.execute()
|
86 | 68 |
|
87 | 69 | # Print information about each video.
|
88 |
| - for playlist_item in playlistitems_list_response["items"]: |
89 |
| - title = playlist_item["snippet"]["title"] |
90 |
| - video_id = playlist_item["snippet"]["resourceId"]["videoId"] |
91 |
| - print "%s (%s)" % (title, video_id) |
| 70 | + for playlist_item in playlistitems_list_response['items']: |
| 71 | + title = playlist_item['snippet']['title'] |
| 72 | + video_id = playlist_item['snippet']['resourceId']['videoId'] |
| 73 | + print '%s (%s)' % (title, video_id) |
92 | 74 |
|
93 | 75 | playlistitems_list_request = youtube.playlistItems().list_next(
|
94 | 76 | playlistitems_list_request, playlistitems_list_response)
|
95 | 77 |
|
96 |
| - print |
| 78 | +if __name__ == '__main__': |
| 79 | + youtube = get_authenticated_service() |
| 80 | + try: |
| 81 | + uploads_playlist_id = get_my_uploads_list() |
| 82 | + if uploads_playlist_id: |
| 83 | + list_my_uploaded_videos(uploads_playlist_id) |
| 84 | + else: |
| 85 | + print('There is no uploaded videos playlist for this user.') |
| 86 | + except HttpError, e: |
| 87 | + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content) |
0 commit comments