Added Sync Only Monitored Movies feature

Implemented algorithm. that (when the switch is on) will only check movies that are monitored, or whose monitored state does not agree between Bazarr and Radarr.
Added statistics to indicate effectiveness of algorithm and logging to help with any potential further debugging.
Updated comments to add reasoning behind code.
This commit is contained in:
JayZed 2024-01-14 13:21:56 -05:00
parent a3c5f82d1a
commit b1f7d4c0e4
1 changed files with 46 additions and 5 deletions

View File

@ -16,6 +16,8 @@ from app.event_handler import event_stream, show_progress, hide_progress
from .utils import get_profile_list, get_tags, get_movies_from_radarr_api
from .parser import movieParser
# map between booleans and strings in DB
bool_map = {"True": True, "False": False}
def update_all_movies():
movies_full_scan_subtitles()
@ -45,6 +47,16 @@ def update_movie(updated_movie, send_event):
event_stream(type='movie', action='update', payload=updated_movie['radarrId'])
def get_movie_monitored_status(movie_id):
existing_movie_monitored = database.execute(
select(TableMovies.monitored)
.where(TableMovies.tmdbId == movie_id))\
.first()
if existing_movie_monitored == None:
return True
else:
return bool_map[existing_movie_monitored[0]]
# Insert new movies in DB
def add_movie(added_movie, send_event):
try:
@ -107,9 +119,9 @@ def update_movies(send_event=True):
(movie['movieFile']['size'] > 20480 or
get_movie_file_size_from_db(movie['movieFile']['path']) > 20480)]
# Remove old movies from DB
# Remove movies from DB that either no longer exist in Radarr or exist and Radarr says do not have a movie file
movies_to_delete = list(set(current_movies_id_db) - set(current_movies_radarr))
movies_deleted = []
if len(movies_to_delete):
try:
database.execute(delete(TableMovies).where(TableMovies.tmdbId.in_(movies_to_delete)))
@ -117,11 +129,19 @@ def update_movies(send_event=True):
logging.error(f"BAZARR cannot delete movies because of {e}")
else:
for removed_movie in movies_to_delete:
movies_deleted.append(removed_movie['title'])
if send_event:
event_stream(type='movie', action='delete', payload=removed_movie)
# Build new and updated movies
# Add new movies and update movies that Radarr says have media files
# Any new movies added to Radarr that don't have media files yet will not be added to DB
movies_count = len(movies)
sync_monitored = settings.radarr.sync_only_monitored_movies
if sync_monitored:
skipped_count = 0
files_missing = 0
movies_added = []
movies_updated = []
for i, movie in enumerate(movies):
if send_event:
show_progress(id='movies_progress',
@ -129,12 +149,20 @@ def update_movies(send_event=True):
name=movie['title'],
value=i,
count=movies_count)
# Only movies that Radarr says have files downloaded will be kept up to date in the DB
if movie['hasFile'] is True:
if 'movieFile' in movie:
if sync_monitored:
if get_movie_monitored_status(movie['tmdbId']) != movie['monitored']:
# monitored status is not the same as our DB
logging.debug(f"movie {movie['title']} monitor status changed offline")
elif not movie['monitored']:
skipped_count += 1
continue
if (movie['movieFile']['size'] > 20480 or
get_movie_file_size_from_db(movie['movieFile']['path']) > 20480):
# Add movies in radarr to current movies list
# Add/update movies from Radarr that have a movie file to current movies list
if str(movie['tmdbId']) in current_movies_id_db:
parsed_movie = movieParser(movie, action='update',
tags_dict=tagsDict,
@ -142,16 +170,29 @@ def update_movies(send_event=True):
audio_profiles=audio_profiles)
if not any([parsed_movie.items() <= x for x in current_movies_db_kv]):
update_movie(parsed_movie, send_event)
movies_updated.append(parsed_movie['title'])
else:
parsed_movie = movieParser(movie, action='insert',
tags_dict=tagsDict,
movie_default_profile=movie_default_profile,
audio_profiles=audio_profiles)
add_movie(parsed_movie, send_event)
movies_added.append(parsed_movie['title'])
else:
logging.debug(f"Movie skipped {movie['title']} because file missing")
files_missing += 1
if send_event:
hide_progress(id='movies_progress')
logging.debug(f"Skipped {files_missing} file missing movies out of {i}")
if sync_monitored:
logging.debug(f"Skipped {skipped_count} unmonitored movies out of {i}")
logging.debug(f"Processed {i - files_missing - skipped_count} movies out of {i} " +
f"with {len(movies_added)} added, {len(movies_updated)} updated and {movies_deleted} deleted")
else:
logging.debug(f"Processed {i - files_missing} movies out of {i} with {len(movies_added)} added and {len(movies_updated)} updated")
logging.debug('BAZARR All movies synced from Radarr into database.')