2021-04-05 00:06:45 +00:00
|
|
|
import re
|
2021-01-17 22:59:14 +00:00
|
|
|
import requests
|
|
|
|
from extractors.video import extract_video
|
|
|
|
from tools.converters import escape_html_textcontent, get_subtitle_api_url
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
def extract_captions(id, **kwargs):
|
2021-04-04 13:23:54 +00:00
|
|
|
if "label" in kwargs and "auto-generated" in kwargs["label"]:
|
|
|
|
captions = extract_captions_from_video(id)
|
|
|
|
else:
|
|
|
|
captions = extract_captions_from_api(id)
|
2021-01-17 22:59:14 +00:00
|
|
|
return extract_captions_from_dict(captions, **kwargs)
|
|
|
|
|
|
|
|
# Return captions for the language specified,
|
|
|
|
# The captions list otherwise
|
2021-01-20 04:35:13 +00:00
|
|
|
def extract_captions_from_dict(captions, *, lang=None, label=None):
|
|
|
|
if lang is None and label is None:
|
2021-01-17 22:59:14 +00:00
|
|
|
return captions
|
|
|
|
|
2021-01-20 04:35:13 +00:00
|
|
|
url = next(caption["second__remoteUrl"] for caption in captions["captions"] if caption["languageCode"] == lang or caption["label"] == label)
|
|
|
|
with requests.get(url) as r:
|
|
|
|
r.raise_for_status()
|
2021-04-05 00:06:45 +00:00
|
|
|
# remove extraneous " align:start position:0%" on timestamps lines on auto-generated captions
|
|
|
|
if (lang and "auto-generated" in lang) or (label and "auto-generated" in label):
|
|
|
|
return re.sub(r"^([0-9:.]+ --> [0-9:.]+).*$", r"\1", r.content.decode("utf8"), flags=re.MULTILINE)
|
2021-01-20 04:35:13 +00:00
|
|
|
return r
|
2021-01-17 22:59:14 +00:00
|
|
|
|
2021-04-04 13:23:54 +00:00
|
|
|
# List of captions directly from youtube, but no automatic
|
2021-01-17 22:59:14 +00:00
|
|
|
def extract_captions_from_api(id):
|
2021-04-04 13:23:54 +00:00
|
|
|
url = "https://video.google.com/timedtext?hl=en&type=list&v={}".format(id)
|
2021-01-17 22:59:14 +00:00
|
|
|
with requests.get(url) as r:
|
2021-01-20 04:35:24 +00:00
|
|
|
if r.status_code == 404:
|
|
|
|
return {
|
|
|
|
"error": "Video unavailable",
|
|
|
|
"identifier": "NOT_FOUND"
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:59:14 +00:00
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
transcript = ET.fromstring(r.content.decode("utf8"))
|
|
|
|
tracks = transcript.findall("track")
|
|
|
|
|
|
|
|
captions = []
|
|
|
|
result = {
|
|
|
|
"captions": captions
|
|
|
|
}
|
|
|
|
|
|
|
|
for track in tracks:
|
|
|
|
language_code = track.attrib["lang_code"]
|
|
|
|
label = track.get("name", default=language_code)
|
|
|
|
subtitle_api_url = get_subtitle_api_url(id, label, language_code)
|
|
|
|
|
|
|
|
params = urlencode({
|
|
|
|
"lang": language_code,
|
|
|
|
"v": id,
|
|
|
|
"fmt": "vtt",
|
|
|
|
"name": label
|
|
|
|
})
|
|
|
|
|
|
|
|
subtitle_url = "https://www.youtube.com/api/timedtext?" + params
|
|
|
|
|
|
|
|
captions.append({
|
|
|
|
"label": label if label != "" else language_code,
|
|
|
|
"languageCode": language_code,
|
|
|
|
"url": subtitle_api_url,
|
2021-01-20 04:35:13 +00:00
|
|
|
"second__remoteUrl": subtitle_url
|
2021-01-17 22:59:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
2021-04-04 13:23:54 +00:00
|
|
|
|
|
|
|
# We'll fall back to this function for auto-captions.
|
|
|
|
def extract_captions_from_video(id):
|
|
|
|
return {
|
|
|
|
"captions": extract_video(id)["captions"]
|
|
|
|
}
|