index subtitle url to video

This commit is contained in:
simon 2022-02-04 17:14:00 +07:00
parent ad2647c4ba
commit 7aaf140ccb
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -14,24 +14,24 @@ from home.src.ta.helper import DurationConverter, clean_string
from ryd_client import ryd_client from ryd_client import ryd_client
class YoutubeSubtitle(YouTubeItem): class YoutubeSubtitle:
"""handle video subtitle functionality""" """handle video subtitle functionality"""
def __init__(self, youtube_meta, config): def __init__(self, config, youtube_meta, media_url, youtube_id):
self.youtube_meta = youtube_meta
self.youtube_id = youtube_meta["id"]
self.config = config self.config = config
self.languages = self.get_lang_list() self.youtube_meta = youtube_meta
self.source = self.config["downloads"]["subtitle_source"] self.media_url = media_url
self.youtube_id = youtube_id
self.languages = False
def get_lang_list(self): def sub_conf_parse(self):
"""return desired languages list""" """add additional conf values to self"""
languages_raw = self.config["downloads"]["subtitle"] languages_raw = self.config["downloads"]["subtitle"]
languages = [i.strip() for i in languages_raw.split(",")] self.languages = [i.strip() for i in languages_raw.split(",")]
return languages
def get_subtitles(self): def get_subtitles(self):
"""check what to do""" """check what to do"""
self.sub_conf_parse()
if not self.languages: if not self.languages:
# no subtitles # no subtitles
return False return False
@ -40,10 +40,12 @@ class YoutubeSubtitle(YouTubeItem):
if relevant_subtitles: if relevant_subtitles:
return relevant_subtitles return relevant_subtitles
if self.source == "auto": if self.config["downloads"]["subtitle_source"] == "auto":
relevant_auto = self.get_auto_caption() relevant_auto = self.get_auto_caption()
return relevant_auto return relevant_auto
return False
def get_auto_caption(self): def get_auto_caption(self):
"""get auto_caption subtitles""" """get auto_caption subtitles"""
print(f"{self.youtube_id}: get auto generated subtitles") print(f"{self.youtube_id}: get auto generated subtitles")
@ -54,10 +56,13 @@ class YoutubeSubtitle(YouTubeItem):
relevant_subtitles = [] relevant_subtitles = []
for language in self.languages: for lang in self.languages:
all_formats = all_subtitles.get(language) media_url = self.media_url.replace(".mp4", f"-{lang}.vtt")
all_formats = all_subtitles.get(lang)
subtitle = [i for i in all_formats if i["ext"] == "vtt"][0] subtitle = [i for i in all_formats if i["ext"] == "vtt"][0]
subtitle.update({"lang": language, "source": "auto"}) subtitle.update(
{"lang": lang, "source": "auto", "media_url": media_url}
)
relevant_subtitles.append(subtitle) relevant_subtitles.append(subtitle)
break break
@ -73,17 +78,20 @@ class YoutubeSubtitle(YouTubeItem):
relevant_subtitles = [] relevant_subtitles = []
for language in self.languages: for lang in self.languages:
all_formats = all_subtitles.get(language) media_url = self.media_url.replace(".mp4", f"-{lang}.vtt")
all_formats = all_subtitles.get(lang)
subtitle = [i for i in all_formats if i["ext"] == "vtt"][0] subtitle = [i for i in all_formats if i["ext"] == "vtt"][0]
subtitle.update({"lang": language, "source": "user"}) subtitle.update(
{"lang": lang, "source": "user", "media_url": media_url}
)
relevant_subtitles.append(subtitle) relevant_subtitles.append(subtitle)
break break
return relevant_subtitles return relevant_subtitles
class YoutubeVideo(YouTubeItem): class YoutubeVideo(YouTubeItem, YoutubeSubtitle):
"""represents a single youtube video""" """represents a single youtube video"""
es_path = False es_path = False
@ -239,7 +247,12 @@ class YoutubeVideo(YouTubeItem):
def _check_subtitles(self): def _check_subtitles(self):
"""optionally add subtitles""" """optionally add subtitles"""
handler = YoutubeSubtitle(self.youtube_meta, self.config) handler = YoutubeSubtitle(
self.config,
self.youtube_meta,
media_url=self.json_data["media_url"],
youtube_id=self.youtube_id,
)
subtitles = handler.get_subtitles() subtitles = handler.get_subtitles()
if subtitles: if subtitles:
self.json_data["subtitles"] = subtitles self.json_data["subtitles"] = subtitles