From b30f5f4c813a6eaf03f4101c85309fab8670619b Mon Sep 17 00:00:00 2001 From: destpstrzy Date: Fri, 24 Jan 2025 15:34:32 +0100 Subject: [PATCH] - added scan Plex library for new files after downloading subtitles --- bazarr/app/config.py | 3 +++ bazarr/plex/operations.py | 23 +++++++++++++++++++++- bazarr/subtitles/processing.py | 19 ++++++++++++------ frontend/src/pages/Settings/Plex/index.tsx | 22 ++++++++++++++++++--- frontend/src/types/settings.d.ts | 3 +++ 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/bazarr/app/config.py b/bazarr/app/config.py index 2e65d5913..37b3b0b36 100644 --- a/bazarr/app/config.py +++ b/bazarr/app/config.py @@ -221,7 +221,10 @@ validators = [ Validator('plex.ssl', must_exist=True, default=False, is_type_of=bool), Validator('plex.apikey', must_exist=True, default='', is_type_of=str), Validator('plex.movie_library', must_exist=True, default='', is_type_of=str), + Validator('plex.series_library', must_exist=True, default='', is_type_of=str), Validator('plex.set_added', must_exist=True, default=False, is_type_of=bool), + Validator('plex.update_movie_library', must_exist=True, default=False, is_type_of=bool), + Validator('plex.update_series_library', must_exist=True, default=False, is_type_of=bool), # proxy section Validator('proxy.type', must_exist=True, default=None, is_type_of=(NoneType, str), diff --git a/bazarr/plex/operations.py b/bazarr/plex/operations.py index 2ae326ff2..12cd0f298 100644 --- a/bazarr/plex/operations.py +++ b/bazarr/plex/operations.py @@ -24,4 +24,25 @@ def plex_set_added_date_now(movie_metadata): updates = {"addedAt.value": current_date} video.edit(**updates) except Exception as e: - logger.error(f"A Plex error occurred: {e}") \ No newline at end of file + logger.error(f"A Plex error occurred: {e}") + + +def plex_update_library(is_movie): + try: + # Determine protocol based on SSL settings + protocol_plex = "https://" if settings.plex.ssl else "http://" + baseurl = f"{protocol_plex}{settings.plex.ip}:{settings.plex.port}" + token = settings.plex.apikey + + # Connect to the Plex server + plex = PlexServer(baseurl, token) + + # Select the library to update + library_name = settings.plex.movie_library if is_movie else settings.plex.series_library + library = plex.library.section(library_name) + + # Trigger update + library.update() + + except Exception as e: + logger.error(f"A Plex error occurred: {e}") diff --git a/bazarr/subtitles/processing.py b/bazarr/subtitles/processing.py index 8617cbed6..be35f3462 100644 --- a/bazarr/subtitles/processing.py +++ b/bazarr/subtitles/processing.py @@ -11,7 +11,7 @@ from app.database import TableEpisodes, TableMovies, database, select from utilities.analytics import event_tracker from radarr.notify import notify_radarr from sonarr.notify import notify_sonarr -from plex.operations import plex_set_added_date_now +from plex.operations import plex_set_added_date_now, plex_update_library from app.event_handler import event_stream from .utils import _get_download_code3 @@ -78,7 +78,7 @@ def process_subtitle(subtitle, media_type, audio_language, path, max_score, is_u if media_type == 'series': episode_metadata = database.execute( select(TableEpisodes.sonarrSeriesId, TableEpisodes.sonarrEpisodeId) - .where(TableEpisodes.path == path_mappings.path_replace_reverse(path)))\ + .where(TableEpisodes.path == path_mappings.path_replace_reverse(path))) \ .first() if not episode_metadata: return @@ -97,7 +97,7 @@ def process_subtitle(subtitle, media_type, audio_language, path, max_score, is_u else: movie_metadata = database.execute( select(TableMovies.radarrId, TableMovies.imdbId) - .where(TableMovies.path == path_mappings.path_replace_reverse_movie(path)))\ + .where(TableMovies.path == path_mappings.path_replace_reverse_movie(path))) \ .first() if not movie_metadata: return @@ -116,7 +116,8 @@ def process_subtitle(subtitle, media_type, audio_language, path, max_score, is_u if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3, audio_language, audio_language_code2, audio_language_code3, - percent_score, subtitle_id, downloaded_provider, uploader, release_info, series_id, episode_id) + percent_score, subtitle_id, downloaded_provider, uploader, release_info, series_id, + episode_id) if media_type == 'series': use_pp_threshold = settings.general.use_postprocessing_threshold @@ -140,14 +141,20 @@ def process_subtitle(subtitle, media_type, audio_language, path, max_score, is_u event_stream(type='series', action='update', payload=episode_metadata.sonarrSeriesId) event_stream(type='episode-wanted', action='delete', payload=episode_metadata.sonarrEpisodeId) + if settings.general.use_plex is True: + if settings.plex.update_movie_library is True: + plex_update_library(is_movie_library=False) else: reversed_path = path_mappings.path_replace_reverse_movie(path) reversed_subtitles_path = path_mappings.path_replace_reverse_movie(downloaded_path) notify_radarr(movie_metadata.radarrId) event_stream(type='movie-wanted', action='delete', payload=movie_metadata.radarrId) - if settings.plex.set_added is True: - plex_set_added_date_now(movie_metadata) + if settings.general.use_plex is True: + if settings.plex.set_added is True: + plex_set_added_date_now(movie_metadata) + if settings.plex.update_movie_library is True: + plex_update_library(is_movie_library=True) event_tracker.track_subtitles(provider=downloaded_provider, action=action, language=downloaded_language) diff --git a/frontend/src/pages/Settings/Plex/index.tsx b/frontend/src/pages/Settings/Plex/index.tsx index 135ef6116..c1d254b6b 100644 --- a/frontend/src/pages/Settings/Plex/index.tsx +++ b/frontend/src/pages/Settings/Plex/index.tsx @@ -13,7 +13,7 @@ import { plexEnabledKey } from "@/pages/Settings/keys"; const SettingsPlexView: FunctionComponent = () => { return ( -
+
@@ -28,15 +28,31 @@ const SettingsPlexView: FunctionComponent = () => {
-
+
+ + Can be helpful for remote media files +
+
+ + + Can be helpful for remote media files
diff --git a/frontend/src/types/settings.d.ts b/frontend/src/types/settings.d.ts index 6b11c5752..5d360ef45 100644 --- a/frontend/src/types/settings.d.ts +++ b/frontend/src/types/settings.d.ts @@ -180,6 +180,9 @@ declare namespace Settings { ssl?: boolean; set_added?: boolean; movie_library?: string; + series_library?: string; + update_movie_library?: boolean; + update_series_library?: boolean; } interface Anticaptcha {