-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyoutube.py
437 lines (373 loc) · 15 KB
/
youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"""YouTube API interface"""
import http
import logging
import re
import time
from collections import Counter
from io import BytesIO
from urllib.parse import urljoin
from django.conf import settings
from django.db.models import Q
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseUpload
from mitol.mail.api import get_message_sender
from smart_open.s3 import Reader
from content_sync.constants import VERSION_DRAFT, VERSION_LIVE
from main.s3_utils import get_boto3_client
from main.utils import truncate_words
from videos.constants import (
DESTINATION_YOUTUBE,
YT_MAX_LENGTH_DESCRIPTION,
YT_MAX_LENGTH_TITLE,
)
from videos.messages import YouTubeUploadFailureMessage, YouTubeUploadSuccessMessage
from videos.models import VideoFile
from websites.api import is_ocw_site
from websites.constants import RESOURCE_TYPE_VIDEO
from websites.models import Website, WebsiteContent
from websites.utils import get_dict_field, get_dict_query_field
log = logging.getLogger(__name__)
# Quota errors should contain the following
API_QUOTA_ERROR_MSG = "quota"
CAPTION_UPLOAD_NAME = "CC (English)"
class YouTubeUploadException(Exception): # noqa: N818
"""Custom exception for YouTube uploads"""
def is_youtube_enabled() -> bool:
"""Returns True if youtube is enabled""" # noqa: D401
return (
settings.YT_ACCESS_TOKEN
and settings.YT_REFRESH_TOKEN
and settings.YT_CLIENT_ID
and settings.YT_CLIENT_SECRET
and settings.YT_PROJECT_ID
)
def mail_youtube_upload_failure(video_file: VideoFile):
"""Notify collaborators that a youtube upload failed"""
with get_message_sender(YouTubeUploadFailureMessage) as sender:
website = video_file.video.website
for collaborator in website.collaborators:
sender.build_and_send_message(
collaborator,
{
"site": {
"title": website.title,
"url": urljoin(
settings.SITE_BASE_URL,
f"sites/{website.name}",
),
},
"video": {"filename": video_file.video.source_key.split("/")[-1]},
},
)
def mail_youtube_upload_success(video_file: VideoFile):
"""Notify collaborators that a youtube upload succeeded"""
with get_message_sender(YouTubeUploadSuccessMessage) as sender:
website = video_file.video.website
for collaborator in website.collaborators:
sender.build_and_send_message(
collaborator,
{
"site": {
"title": website.title,
"url": urljoin(
settings.SITE_BASE_URL,
f"sites/{website.name}",
),
},
"video": {
"filename": video_file.video.source_key.split("/")[-1],
"url": f"https://www.youtube.com/watch?v={video_file.destination_id}",
},
},
)
def resumable_upload(request, max_retries=10):
"""
Upload a video to YouTube and resume on failure up to 10 times, adapted from YouTube API example.
To use resumable media you must use a MediaFileUpload object and flag it as a resumable upload.
You then repeatedly call next_chunk() on the googleapiclient.http.HttpRequest object until the
upload is complete.
Args:
request(googleapiclient.http.HttpRequest): The Youtube API execute request to process
max_retries(int): Maximum # of times to retry an upload (default 10)
Returns:
dict: The YouTube API response
""" # noqa: E501
response = None
error = None
retry = 0
retry_exceptions = (OSError, http.client.HTTPException)
retry_statuses = [500, 502, 503, 504]
while response is None:
try:
_, response = request.next_chunk()
if response is not None and "id" not in response:
msg = f"YouTube upload failed: {response}"
raise YouTubeUploadException(msg)
except HttpError as e:
if e.resp.status in retry_statuses:
error = e
else:
raise
except retry_exceptions as e:
error = e
if error is not None:
retry += 1
if retry > max_retries:
log.error("Final upload failure")
msg = "Retried YouTube upload 10x, giving up"
raise YouTubeUploadException(msg) from error
sleep_time = 2**retry
time.sleep(sleep_time)
return response
def strip_bad_chars(txt):
"""
Remove any characters forbidden by Youtube (<, >) from text
Args:
txt(str): Text to remove characters from.
Returns:
str: Text without bad characters
"""
return re.sub("<|>", "", txt)
class YouTubeApi:
"""
Class interface to YouTube API calls
"""
client = None
s3 = None
def __init__(self):
"""
Generate an authorized YouTube API client and S3 client
"""
credentials = Credentials(
settings.YT_ACCESS_TOKEN,
token_uri="https://accounts.google.com/o/oauth2/token", # noqa: S106
client_id=settings.YT_CLIENT_ID,
client_secret=settings.YT_CLIENT_SECRET,
refresh_token=settings.YT_REFRESH_TOKEN,
)
self.client = build("youtube", "v3", credentials=credentials)
self.s3 = get_boto3_client("s3")
def video_status(self, video_id):
"""
Checks the status of a video. 'processed' = ready for viewing.
Args:
video_id(str): YouTube video id
Returns:
str: status of the YouTube video
""" # noqa: D401
results = self.client.videos().list(part="status", id=video_id).execute()
return results["items"][0]["status"]["uploadStatus"]
def upload_video(
self,
videofile: VideoFile,
privacy="unlisted",
notify_subscribers=False, # noqa: FBT002
):
"""
Transfer the video's original video file from S3 to YouTube.
The YT account must be validated for videos > 15 minutes long:
https://www.youtube.com/verify
Args:
video(Video): The Video object whose original source file will be uploaded'
privacy(str): The privacy level to set the YouTube video to.
notify_subscribers(bool): whether subscribers should be notified
Returns:
dict: YouTube API response
"""
original_name = videofile.video.source_key.split("/")[-1]
request_body = {
"snippet": {
"title": truncate_words(
strip_bad_chars(original_name), YT_MAX_LENGTH_TITLE
),
"description": "",
"categoryId": settings.YT_CATEGORY_ID,
},
"status": {"privacyStatus": privacy},
}
with Reader(settings.AWS_STORAGE_BUCKET_NAME, videofile.s3_key) as s3_stream:
request = self.client.videos().insert(
part=",".join(request_body.keys()),
body=request_body,
notifySubscribers=notify_subscribers,
media_body=MediaIoBaseUpload(
s3_stream, mimetype="video/*", chunksize=-1, resumable=True
),
)
return resumable_upload(request)
def update_privacy(self, youtube_id: str, privacy: str):
"""Update the privacy level of a video"""
self.client.videos().update(
part="status",
body={
"id": youtube_id,
"status": {"privacyStatus": privacy, "embeddable": True},
},
).execute()
def update_captions(self, resource: WebsiteContent, youtube_id: str):
"""Update captions for video"""
videofile = VideoFile.objects.filter(
destination=DESTINATION_YOUTUBE,
destination_id=youtube_id,
video__website=resource.website,
).last()
if not videofile or not videofile.video.webvtt_transcript_file:
return
content = videofile.video.webvtt_transcript_file.open(mode="rb").read()
media_body = MediaIoBaseUpload(
BytesIO(content), mimetype="text/vtt", chunksize=-1, resumable=True
)
existing_captions = (
self.client.captions().list(part="snippet", videoId=youtube_id).execute()
)
existing_captions = existing_captions.get("items", [])
existing_captions = list(
filter(
lambda caption_file: caption_file.get("snippet", {}).get("name")
== CAPTION_UPLOAD_NAME,
existing_captions,
)
)
if existing_captions:
self.client.captions().update(
part="snippet",
body={"id": existing_captions[0].get("id")},
media_body=media_body,
).execute()
else:
self.client.captions().insert(
part="snippet",
sync=False,
body={
"snippet": {
"language": "en",
"name": CAPTION_UPLOAD_NAME,
"videoId": youtube_id,
}
},
media_body=media_body,
).execute()
def update_video(self, resource: WebsiteContent, privacy=None):
"""
Update a video's metadata based on a WebsiteContent object that is assumed to have certain fields.
""" # noqa: E501
metadata = resource.metadata
description = get_dict_field(metadata, settings.YT_FIELD_DESCRIPTION)
speakers = get_dict_field(metadata, settings.YT_FIELD_SPEAKERS)
if speakers:
description = f"{description}\n\nSpeakers: {speakers}"
youtube_id = get_dict_field(metadata, settings.YT_FIELD_ID)
self.client.videos().update(
part="snippet",
body={
"id": youtube_id,
"snippet": {
"title": truncate_words(
strip_bad_chars(resource.title), YT_MAX_LENGTH_TITLE
),
"description": truncate_words(
strip_bad_chars(description), YT_MAX_LENGTH_DESCRIPTION
),
"tags": get_dict_field(metadata, settings.YT_FIELD_TAGS),
"categoryId": settings.YT_CATEGORY_ID,
},
},
).execute()
self.update_captions(resource, youtube_id)
if privacy:
self.update_privacy(youtube_id, privacy=privacy)
@classmethod
def get_all_video_captions(
cls,
website_ids: list[str] | None = None,
video_ids: list[int] | None = None,
*,
only_dups: bool = False,
) -> list[dict]:
"""
Get caption tracks for all YouTube videos in database, with optional filtering
and duplicate detection.
Args:
website_ids (list[int], optional): Filter videos by website short_ids
video_ids (list[int], optional): Filter videos by video IDs
only_dups (bool, optional): Only return videos with duplicate captions
in any language. Defaults to False.
Returns:
list[dict]: List of dicts containing video info and caption tracks
"""
youtube = cls()
video_captions = []
video_files = VideoFile.objects.filter(
destination=DESTINATION_YOUTUBE, destination_id__isnull=False
).select_related("video", "video__website")
if website_ids:
video_files = video_files.filter(video__website__short_id__in=website_ids)
if video_ids:
video_files = video_files.filter(video_id__in=video_ids)
for video_file in video_files:
try:
captions_response = (
youtube.client.captions()
.list(part="snippet", videoId=video_file.destination_id)
.execute()
)
video_info = {
"video_id": video_file.destination_id,
"filename": video_file.video.source_key.split("/")[-1],
"website": video_file.video.website.name,
"captions": [],
}
language_counter = Counter()
for caption in captions_response.get("items", []):
caption_info = {
"id": caption["id"],
"language": caption["snippet"]["language"],
"name": caption["snippet"].get("name", ""),
"last_updated": caption["snippet"].get("lastUpdated", ""),
}
language_counter[caption_info["language"]] += 1
video_info["captions"].append(caption_info)
video_info["language_counts"] = dict(language_counter)
has_duplicates = any(count > 1 for count in language_counter.values())
if not only_dups or has_duplicates:
video_captions.append(video_info)
except HttpError:
log.exception(
"Failed to get captions for video %s", video_file.destination_id
)
continue
return video_captions
def update_youtube_metadata(website: Website, version=VERSION_DRAFT):
"""Update YouTube video metadata via the API"""
if not is_youtube_enabled() or not is_ocw_site(website):
return
query_id_field = get_dict_query_field("metadata", settings.YT_FIELD_ID)
video_resources = website.websitecontent_set.filter(
Q(metadata__resourcetype=RESOURCE_TYPE_VIDEO)
).exclude(Q(**{query_id_field: None}) | Q(**{query_id_field: ""}))
if video_resources.count() == 0:
return
youtube = YouTubeApi()
for video_resource in video_resources:
is_draft = get_dict_field(video_resource.metadata, "draft") is True
youtube_id = get_dict_field(video_resource.metadata, settings.YT_FIELD_ID)
# do not run this for any old imported videos
if VideoFile.objects.filter(
video__website=website, destination_id=youtube_id
).exists():
try:
youtube.update_video(
video_resource,
privacy=(
"public"
if version == VERSION_LIVE and not is_draft
else "unlisted"
),
)
except: # pylint:disable=bare-except # noqa: E722
log.exception(
"Unexpected error updating metadata for video resource %d",
video_resource.id,
)