From e6b0a4668b74f4ee2d19bdd0e154291bfaa0ccf1 Mon Sep 17 00:00:00 2001 From: Gaisberg <93206976+Gaisberg@users.noreply.github.com> Date: Fri, 15 Dec 2023 21:22:41 +0200 Subject: [PATCH] Minor symlinking and state tweaks (#27) * Minor symlinking tweaks * deps: remove package-lock.json from .gitignore --------- Co-authored-by: Gaisberg Co-authored-by: Ayush Sehrawat --- backend/program/content/overseerr.py | 4 +--- backend/program/libraries/plex.py | 1 + backend/program/media.py | 14 +++----------- backend/program/symlink.py | 26 ++++++++------------------ 4 files changed, 13 insertions(+), 32 deletions(-) diff --git a/backend/program/content/overseerr.py b/backend/program/content/overseerr.py index c5df3e59..79969fd7 100644 --- a/backend/program/content/overseerr.py +++ b/backend/program/content/overseerr.py @@ -32,14 +32,12 @@ def _validate_settings(self): return response.ok except ConnectTimeout: return False - # response = json.loads(response.content) - # return response['response'] def update_items(self, media_items: MediaItemContainer): """Fetch media from overseerr and add them to media_items attribute if they are not already there""" logger.debug("Getting items...") - items = self._get_items_from_overseerr(1000) + items = self._get_items_from_overseerr(10000) new_items = [item for item in items if item not in media_items] container = self.updater.create_items(new_items) added_items = media_items.extend(container) diff --git a/backend/program/libraries/plex.py b/backend/program/libraries/plex.py index c33389df..2fe393da 100644 --- a/backend/program/libraries/plex.py +++ b/backend/program/libraries/plex.py @@ -72,6 +72,7 @@ def update_sections(self, media_items: List[MediaItem]): or (item.type == "show" and any(season for season in item.seasons if season.state is MediaItemState.SYMLINK)) + or any(episode for season in item.seasons for episode in season.episodes if episode.state is MediaItemState.SYMLINK) ): if not section.refreshing: section.update() diff --git a/backend/program/media.py b/backend/program/media.py index b8ff7ecf..13eace9c 100644 --- a/backend/program/media.py +++ b/backend/program/media.py @@ -48,7 +48,7 @@ def state(self): return MediaItemState.LIBRARY if self.symlinked: return MediaItemState.SYMLINK - if self.is_cached(): + if self.is_cached() or self.file: return MediaItemState.DOWNLOAD if len(self.streams) > 0: return MediaItemState.SCRAPE @@ -135,12 +135,6 @@ def state(self): return MediaItemState.LIBRARY if any(season.state in [MediaItemState.LIBRARY, MediaItemState.LIBRARY_PARTIAL] for season in self.seasons): return MediaItemState.LIBRARY_PARTIAL - if any(season.state == MediaItemState.SYMLINK for season in self.seasons): - return MediaItemState.SYMLINK - if any(season.state == MediaItemState.DOWNLOAD for season in self.seasons): - return MediaItemState.DOWNLOAD - if any(season.state == MediaItemState.SCRAPE for season in self.seasons): - return MediaItemState.SCRAPE if any(season.state == MediaItemState.CONTENT for season in self.seasons): return MediaItemState.CONTENT return MediaItemState.UNKNOWN @@ -172,11 +166,9 @@ def state(self): return MediaItemState.LIBRARY if any(episode.state == MediaItemState.LIBRARY for episode in self.episodes): return MediaItemState.LIBRARY_PARTIAL - if any(episode.state == MediaItemState.SYMLINK for episode in self.episodes): - return MediaItemState.SYMLINK - if self.is_cached() or any(episode.state == MediaItemState.DOWNLOAD for episode in self.episodes): + if self.is_cached(): return MediaItemState.DOWNLOAD - if self.is_scraped() or any(episode.state == MediaItemState.SCRAPE for episode in self.episodes): + if self.is_scraped(): return MediaItemState.SCRAPE if any(episode.state == MediaItemState.CONTENT for episode in self.episodes): return MediaItemState.CONTENT diff --git a/backend/program/symlink.py b/backend/program/symlink.py index a413f502..cd717bb2 100644 --- a/backend/program/symlink.py +++ b/backend/program/symlink.py @@ -5,6 +5,7 @@ from utils.settings import settings_manager as settings from utils.logger import logger from program.media import MediaItemState +from utils.thread import ThreadRunner class Symlinker: @@ -15,6 +16,7 @@ def __init__( ): # Symlinking is required while True: + self.cache = {} self.settings = settings.get("symlink") self.mount_path = os.path.abspath(self.settings["mount"]) self.host_path = os.path.abspath(self.settings["host_mount"]) @@ -26,6 +28,8 @@ def __init__( os.mkdir(os.path.join(self.symlink_path, "movies")) if not os.path.exists(os.path.join(self.symlink_path, "shows")): os.mkdir(os.path.join(self.symlink_path, "shows")) + self.cache_thread = ThreadRunner(self.update_cache, 10) + self.cache_thread.start() break logger.error("Rclone mount not found, retrying in 2...") time.sleep(2) @@ -119,23 +123,9 @@ def run(self, media_items): logger.debug("Done!") def _find_file(self, filename): + return self.cache.get(filename, None) + + def update_cache(self): for root, _, files in os.walk(os.path.join(self.host_path, "torrents")): for file in files: - if file == filename: - return os.path.join(root, file) - -# class FileWatcher(): -# def __init__(self, path): -# self.path = os.path.join(os.path.abspath(path), "torrents") -# self.cache = {} - -# def run(self): -# for folder_name, _, filenames in os.walk(self.path): -# folder = folder_name.split("/")[-1] -# for filename in filenames: -# if folder not in self.cache: -# self.cache[folder] = [] -# if filename not in self.cache[folder]: -# self.cache[folder].append(filename) -# # Scan every after 5 seconds each cycle... -# time.sleep(5) \ No newline at end of file + self.cache[file] = os.path.join(root, file) \ No newline at end of file