From 434aa97a8651225d2af45b287ed0db3a2b27b207 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 24 Jul 2023 23:44:27 +0700 Subject: [PATCH] static cache file path building, #498 --- tubearchivist/home/src/index/reindex.py | 5 ++- tubearchivist/home/src/index/video.py | 44 ++++++++----------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/tubearchivist/home/src/index/reindex.py b/tubearchivist/home/src/index/reindex.py index 99fd0f8..7c69b49 100644 --- a/tubearchivist/home/src/index/reindex.py +++ b/tubearchivist/home/src/index/reindex.py @@ -287,7 +287,10 @@ class Reindex(ReindexBase): es_meta = video.json_data.copy() # get new - video.build_json() + media_url = os.path.join( + self.config["application"]["videos"], es_meta["media_url"] + ) + video.build_json(media_path=media_url) if not video.youtube_meta: video.deactivate() return diff --git a/tubearchivist/home/src/index/video.py b/tubearchivist/home/src/index/video.py index 7b2da2d..87360ae 100644 --- a/tubearchivist/home/src/index/video.py +++ b/tubearchivist/home/src/index/video.py @@ -231,18 +231,24 @@ class YoutubeVideo(YouTubeItem, YoutubeSubtitle): def build_dl_cache_path(self): """find video path in dl cache""" cache_dir = self.app_conf["cache_dir"] - cache_path = f"{cache_dir}/download/" - all_cached = os.listdir(cache_path) - for file_cached in all_cached: - if self.youtube_id in file_cached: - vid_path = os.path.join(cache_path, file_cached) - return vid_path + video_id = self.json_data["youtube_id"] + cache_path = f"{cache_dir}/download/{video_id}.mp4" + if os.path.exists(cache_path): + return cache_path + + channel_path = os.path.join( + self.app_conf["videos"], + self.json_data["channel"]["channel_id"], + f"{video_id}.mp4", + ) + if os.path.exists(channel_path): + return channel_path raise FileNotFoundError def add_player(self, media_path=False): """add player information for new videos""" - vid_path = self._get_vid_path(media_path) + vid_path = media_path or self.build_dl_cache_path() duration_handler = DurationConverter() duration = duration_handler.get_sec(vid_path) @@ -259,7 +265,7 @@ class YoutubeVideo(YouTubeItem, YoutubeSubtitle): def add_streams(self, media_path=False): """add stream metadata""" - vid_path = self._get_vid_path(media_path) + vid_path = media_path or self.build_dl_cache_path() media = MediaStreamExtractor(vid_path) self.json_data.update( { @@ -268,28 +274,6 @@ class YoutubeVideo(YouTubeItem, YoutubeSubtitle): } ) - def _get_vid_path(self, media_path=False): - """get path of media file""" - if media_path: - return media_path - - try: - # when indexing from download task - vid_path = self.build_dl_cache_path() - except FileNotFoundError as err: - # when reindexing needs to handle title rename - channel = os.path.split(self.json_data["media_url"])[0] - channel_dir = os.path.join(self.app_conf["videos"], channel) - all_files = os.listdir(channel_dir) - for file in all_files: - if self.youtube_id in file and file.endswith(".mp4"): - vid_path = os.path.join(channel_dir, file) - break - else: - raise FileNotFoundError("could not find video file") from err - - return vid_path - def add_file_path(self): """build media_url for where file will be located""" self.json_data["media_url"] = os.path.join(