Coverage for youtube.py : 45%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
4# This file is part of Vallenato.fr.
5#
6# Vallenato.fr is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Affero General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Vallenato.fr is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Affero General Public License for more details.
15#
16# You should have received a copy of the GNU Affero General Public License
17# along with Vallenato.fr. If not, see <http://www.gnu.org/licenses/>.
19# Adapted from:
20# https://github.com/youtube/api-samples/blob/master/python/my_uploads.py
21# https://github.com/youtube/api-samples/blob/master/python/upload_video.py
23import httplib2
24import os
25import logging
26import sys
28from googleapiclient.discovery import build
29from googleapiclient.errors import HttpError
30from oauth2client.file import Storage
31from oauth2client.client import flow_from_clientsecrets
32from oauth2client.tools import run_flow
37# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
38# the OAuth 2.0 information for this application, including its client_id and
39# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
40# the {{ Google Cloud Console }} at
41# {{ https://cloud.google.com/console }}.
42# Please ensure that you have enabled the YouTube Data API for your project.
43# For more information about using OAuth2 to access the YouTube Data API, see:
44# https://developers.google.com/youtube/v3/guides/authentication
45# For more information about the client_secrets.json file format, see:
46# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
47CLIENT_SECRETS_FILE = 'client_secret.json'
49# This OAuth 2.0 access scope allows an application to view videos and playlists
50SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']
51API_SERVICE_NAME = 'youtube'
52API_VERSION = 'v3'
54# This variable defines a message to display if the CLIENT_SECRETS_FILE is
55# missing.
56MISSING_CLIENT_SECRETS_MESSAGE = """
57WARNING: Please configure OAuth 2.0
59To make this sample run you will need to populate the client_secrets.json file
60found at:
61 %s
62with information from the APIs Console
63https://console.developers.google.com
65For more information about the client_secrets.json file format, please visit:
66https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
67""" % os.path.abspath(os.path.join(os.path.dirname(__file__), CLIENT_SECRETS_FILE))
69# Authorize the request and store authorization credentials.
70def yt_get_authenticated_service(args):
71 flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=SCOPES, message=MISSING_CLIENT_SECRETS_MESSAGE)
72 storage = Storage("vallenato.fr-oauth2.json")
73 credentials = storage.get()
74 if credentials is None or credentials.invalid:
75 credentials = run_flow(flow, storage, args)
76 return build(API_SERVICE_NAME, API_VERSION, credentials = credentials, cache_discovery=False)
78def yt_get_my_uploads_list(youtube):
79 return "UU_8R235jg1ld6MCMOzz2khQ"
80 # # Retrieve the contentDetails part of the channel resource for the
81 # # authenticated user's channel.
82 # channels_response = youtube.channels().list(
83 # mine=True,
84 # part='contentDetails'
85 # ).execute()
86 #
87 # for channel in channels_response['items']:
88 # # From the API response, extract the playlist ID that identifies the list
89 # # of videos uploaded to the authenticated user's channel.
90 # return channel['contentDetails']['relatedPlaylists']['uploads']
91 # return None
93def yt_list_my_uploaded_videos(uploads_playlist_id, youtube):
94 uploaded_videos = []
95 # Retrieve the list of videos uploaded to the authenticated user's channel.
96 playlistitems_list_request = youtube.playlistItems().list(
97 playlistId=uploads_playlist_id,
98 part='contentDetails'
99 )
101 logging.debug('Videos in list %s' % uploads_playlist_id)
102 while playlistitems_list_request:
103 playlistitems_list_response = playlistitems_list_request.execute()
105 videoIds = ",".join(playlist_item['contentDetails']['videoId'] for playlist_item in playlistitems_list_response['items'])
107 # To get the videos' tags, we need to do an extra query
108 videos_list_request = youtube.videos().list(
109 id=videoIds,
110 part='snippet,status'
111 )
113 while videos_list_request:
114 videos_list_response = videos_list_request.execute()
116 for video in videos_list_response['items']:
117 # Only keep Public videos
118 if video['status']['privacyStatus'] != "public":
119 continue
120 # Skip videos tagged as "no-website" or "Tutorial"
121 if 'tags' in video['snippet'] and (
122 "no-website" in video['snippet']['tags'] or
123 "Tutorial" in video['snippet']['tags']):
124 continue
125 vid = {}
126 vid["id"] = video['id']
127 vid["title"] = video['snippet']['title']
128 vid["description"] = video['snippet']['description']
129 try:
130 vid["tags"] = video['snippet']['tags']
131 except KeyError:
132 vid["tags"] = []
133 vid["publishedAt"] = video['snippet']['publishedAt']
134 # Sizes: default, medium, high, standard, maxres
135 vid["thumbnail"] = video['snippet']['thumbnails']['medium']
137 uploaded_videos.append(vid)
138 logging.debug(vid)
139 videos_list_request = youtube.videos().list_next(videos_list_request, videos_list_response)
141 playlistitems_list_request = youtube.playlistItems().list_next(playlistitems_list_request, playlistitems_list_response)
142 return uploaded_videos