From e3d37576ed598f9fdc39fd39615562afaee2393f Mon Sep 17 00:00:00 2001 From: Kamil Date: Sun, 24 Nov 2024 16:17:30 +0000 Subject: [PATCH] Added REFRESH_LIBRARIES_AFTER_DOWNLOAD_TASK resolves #10 --- app/tasks.py | 5 +++++ config.py | 5 ++--- jellyfin/client.py | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/tasks.py b/app/tasks.py index 081cef7..1330f82 100644 --- a/app/tasks.py +++ b/app/tasks.py @@ -181,6 +181,11 @@ def download_missing_tracks(self): } finally: release_lock(lock_key) + if app.config['REFRESH_LIBRARIES_AFTER_DOWNLOAD_TASK']: + libraries = jellyfin.get_libraries(jellyfin_admin_token) + for lib in libraries: + if lib['CollectionType'] == 'music': + jellyfin.refresh_library(jellyfin_admin_token, lib['ItemId']) else: app.logger.info("Skipping task. Another instance is already running.") return {'status': 'Task skipped, another instance is running'} diff --git a/config.py b/config.py index 7e28adc..bebea6a 100644 --- a/config.py +++ b/config.py @@ -13,6 +13,7 @@ class Config: JELLYPLIST_DB_USER = os.getenv('JELLYPLIST_DB_USER') JELLYPLIST_DB_PASSWORD = os.getenv('JELLYPLIST_DB_PASSWORD') START_DOWNLOAD_AFTER_PLAYLIST_ADD = os.getenv('START_DOWNLOAD_AFTER_PLAYLIST_ADD',"true").lower() == 'true' # If a new Playlist is added, the Download Task will be scheduled immediately + REFRESH_LIBRARIES_AFTER_DOWNLOAD_TASK = os.getenv('REFRESH_LIBRARIES_AFTER_DOWNLOAD_TASK',"false").lower() == 'true' CACHE_TYPE = 'redis' CACHE_REDIS_PORT = 6379 CACHE_REDIS_HOST = 'redis' @@ -20,9 +21,6 @@ class Config: CACHE_DEFAULT_TIMEOUT = 3600 REDIS_URL = os.getenv('REDIS_URL','redis://redis:6379/0') SEARCH_JELLYFIN_BEFORE_DOWNLOAD = os.getenv('SEARCH_JELLYFIN_BEFORE_DOWNLOAD',"true").lower() == 'true' - - - SEARCH_JELLYFIN_BEFORE_DOWNLOAD = True # SpotDL specific configuration SPOTDL_CONFIG = { 'cookie_file': '/jellyplist/cookies.txt', @@ -36,6 +34,7 @@ class Config: 'JELLYFIN_SERVER_URL': cls.JELLYFIN_SERVER_URL, 'JELLYFIN_ADMIN_USER': cls.JELLYFIN_ADMIN_USER, 'JELLYFIN_ADMIN_PASSWORD': cls.JELLYFIN_ADMIN_PASSWORD, + 'SPOTIFY_CLIENT_ID': cls.SPOTIFY_CLIENT_ID, 'SPOTIFY_CLIENT_SECRET': cls.SPOTIFY_CLIENT_SECRET, 'JELLYPLIST_DB_HOST' : cls.JELLYPLIST_DB_HOST, diff --git a/jellyfin/client.py b/jellyfin/client.py index 7ed547b..3ff4c06 100644 --- a/jellyfin/client.py +++ b/jellyfin/client.py @@ -172,8 +172,36 @@ class JellyfinClient: else: raise Exception(f"Failed to get playlists: {response.content}") - - + def get_libraries(self, session_token: str): + url = f'{self.base_url}/Library/VirtualFolders' + params = { + + } + response = requests.get(url, headers=self._get_headers(session_token=session_token), params=params , timeout = 10) + if response.status_code == 200: + return response.json() + else: + raise Exception(f"Failed to get playlists: {response.content}") + + def refresh_library(self, session_token: str, library_id: str) -> bool: + url = f'{self.base_url}/Items/{library_id}/Refresh' + + params = { + "Recursive": "true", + "ImageRefreshMode": "Default", + "MetadataRefreshMode": "Default", + "ReplaceAllImages": "false", + "RegenerateTrickplay": "false", + "ReplaceAllMetadata": "false" + } + response = requests.post(url, headers=self._get_headers(session_token=session_token), params=params , timeout = 10) + if response.status_code == 204: + return True + else: + raise Exception(f"Failed to update library: {response.content}") + + + def search_music_tracks(self, session_token: str, search_query: str): """ Search for music tracks by title, song name, and optionally Spotify-ID.