From 8e6992875683a692b668b60a408949db62f844ad Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 20 Jan 2021 17:35:13 +1300 Subject: [PATCH] Captions: Python code cleanup and optimisation --- extractors/captions.py | 24 +++++++----------------- extractors/video.py | 25 ++++++++++++------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/extractors/captions.py b/extractors/captions.py index 85df2be..00938a5 100644 --- a/extractors/captions.py +++ b/extractors/captions.py @@ -10,24 +10,14 @@ def extract_captions(id, **kwargs): # Return captions for the language specified, # The captions list otherwise -def extract_captions_from_dict(captions, **kwargs): - lang = None - label = None - - if "lang" in kwargs: - lang = kwargs["lang"] - elif "label" in kwargs: - label = kwargs["label"] - else: +def extract_captions_from_dict(captions, *, lang=None, label=None): + if lang is None and label is None: return captions - for subtitle in captions["captions"]: - if lang == subtitle["languageCode"] or label == subtitle["label"]: - url = subtitle["second__subtitleUrl"] - - with requests.get(url) as r: - r.raise_for_status() - return r.content.decode("utf8") + 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() + return r # Currently unused in favour of extract_captions_from_api. def extract_captions_from_video(id): @@ -67,7 +57,7 @@ def extract_captions_from_api(id): "label": label if label != "" else language_code, "languageCode": language_code, "url": subtitle_api_url, - "second__subtitleUrl": subtitle_url + "second__remoteUrl": subtitle_url }) return result diff --git a/extractors/video.py b/extractors/video.py index d24d060..4e2656f 100644 --- a/extractors/video.py +++ b/extractors/video.py @@ -173,22 +173,21 @@ def extract_video(id): "second__width": format["width"], "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(): - - if language_code != "live_chat": - subtitle_url = subtitle["url"] - label = get_language_label_from_url(subtitle_url) - subtitle_api_url = get_subtitle_api_url(id, label, language_code) + if language_code == "live_chat": + continue - result["captions"].append({ - "label": label if label != "" else language_code, - "languageCode": language_code, - "url": subtitle_api_url, - "second__subtitleUrl": subtitle_url # Direct YouTube url - }) + 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({ + "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)