mirror of
https://git.sr.ht/~cadence/NewLeaf
synced 2024-11-21 23:27:29 +00:00
Captions: Python code cleanup and optimisation
This commit is contained in:
parent
6709aa30c2
commit
8e69928756
@ -10,24 +10,14 @@ def extract_captions(id, **kwargs):
|
|||||||
|
|
||||||
# Return captions for the language specified,
|
# Return captions for the language specified,
|
||||||
# The captions list otherwise
|
# The captions list otherwise
|
||||||
def extract_captions_from_dict(captions, **kwargs):
|
def extract_captions_from_dict(captions, *, lang=None, label=None):
|
||||||
lang = None
|
if lang is None and label is None:
|
||||||
label = None
|
|
||||||
|
|
||||||
if "lang" in kwargs:
|
|
||||||
lang = kwargs["lang"]
|
|
||||||
elif "label" in kwargs:
|
|
||||||
label = kwargs["label"]
|
|
||||||
else:
|
|
||||||
return captions
|
return captions
|
||||||
|
|
||||||
for subtitle in captions["captions"]:
|
url = next(caption["second__remoteUrl"] for caption in captions["captions"] if caption["languageCode"] == lang or caption["label"] == label)
|
||||||
if lang == subtitle["languageCode"] or label == subtitle["label"]:
|
with requests.get(url) as r:
|
||||||
url = subtitle["second__subtitleUrl"]
|
r.raise_for_status()
|
||||||
|
return r
|
||||||
with requests.get(url) as r:
|
|
||||||
r.raise_for_status()
|
|
||||||
return r.content.decode("utf8")
|
|
||||||
|
|
||||||
# Currently unused in favour of extract_captions_from_api.
|
# Currently unused in favour of extract_captions_from_api.
|
||||||
def extract_captions_from_video(id):
|
def extract_captions_from_video(id):
|
||||||
@ -67,7 +57,7 @@ def extract_captions_from_api(id):
|
|||||||
"label": label if label != "" else language_code,
|
"label": label if label != "" else language_code,
|
||||||
"languageCode": language_code,
|
"languageCode": language_code,
|
||||||
"url": subtitle_api_url,
|
"url": subtitle_api_url,
|
||||||
"second__subtitleUrl": subtitle_url
|
"second__remoteUrl": subtitle_url
|
||||||
})
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -173,22 +173,21 @@ def extract_video(id):
|
|||||||
"second__width": format["width"],
|
"second__width": format["width"],
|
||||||
"second__height": format["height"]
|
"second__height": format["height"]
|
||||||
})
|
})
|
||||||
|
|
||||||
if "requested_subtitles" in info and info["requested_subtitles"]:
|
|
||||||
|
|
||||||
|
if info.get("requested_subtitles"):
|
||||||
for language_code, subtitle in info["requested_subtitles"].items():
|
for language_code, subtitle in info["requested_subtitles"].items():
|
||||||
|
if language_code == "live_chat":
|
||||||
if language_code != "live_chat":
|
continue
|
||||||
subtitle_url = subtitle["url"]
|
|
||||||
label = get_language_label_from_url(subtitle_url)
|
|
||||||
subtitle_api_url = get_subtitle_api_url(id, label, language_code)
|
|
||||||
|
|
||||||
result["captions"].append({
|
subtitle_url = subtitle["url"]
|
||||||
"label": label if label != "" else language_code,
|
label = get_language_label_from_url(subtitle_url)
|
||||||
"languageCode": language_code,
|
subtitle_api_url = get_subtitle_api_url(id, label, language_code)
|
||||||
"url": subtitle_api_url,
|
result["captions"].append({
|
||||||
"second__subtitleUrl": subtitle_url # Direct YouTube url
|
"label": label if label != "" else language_code,
|
||||||
})
|
"languageCode": language_code,
|
||||||
|
"url": subtitle_api_url,
|
||||||
|
"second__subtitleUrl": subtitle_url # Direct YouTube url
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
result = get_more_stuff_from_file(info["id"], result)
|
result = get_more_stuff_from_file(info["id"], result)
|
||||||
|
Loading…
Reference in New Issue
Block a user