Improved Sonarr and Radarr syncing to prevent database integrity exception being raised by relying on proper primary keys instead of other values.

pull/2807/head v1.5.1-beta.0
morpheus65535 1 month ago
parent 2247c55bfa
commit 8346ea9dc8

@ -53,7 +53,7 @@ def update_movie(updated_movie, send_event):
updated_movie['updated_at_timestamp'] = datetime.now() updated_movie['updated_at_timestamp'] = datetime.now()
database.execute( database.execute(
update(TableMovies).values(updated_movie) update(TableMovies).values(updated_movie)
.where(TableMovies.tmdbId == updated_movie['tmdbId'])) .where(TableMovies.radarrId == updated_movie['radarrId']))
except IntegrityError as e: except IntegrityError as e:
logging.error(f"BAZARR cannot update movie {updated_movie['path']} because of {e}") logging.error(f"BAZARR cannot update movie {updated_movie['path']} because of {e}")
else: else:
@ -66,7 +66,7 @@ def update_movie(updated_movie, send_event):
def get_movie_monitored_status(movie_id): def get_movie_monitored_status(movie_id):
existing_movie_monitored = database.execute( existing_movie_monitored = database.execute(
select(TableMovies.monitored) select(TableMovies.monitored)
.where(TableMovies.tmdbId == str(movie_id)))\ .where(TableMovies.radarrId == str(movie_id)))\
.first() .first()
if existing_movie_monitored is None: if existing_movie_monitored is None:
return True return True
@ -124,16 +124,16 @@ def update_movies(send_event=True):
return return
else: else:
# Get current movies in DB # Get current movies in DB
current_movies_id_db = [x.tmdbId for x in current_movies_id_db = [x.radarrId for x in
database.execute( database.execute(
select(TableMovies.tmdbId)) select(TableMovies.radarrId))
.all()] .all()]
current_movies_db_kv = [x.items() for x in [y._asdict()['TableMovies'].__dict__ for y in current_movies_db_kv = [x.items() for x in [y._asdict()['TableMovies'].__dict__ for y in
database.execute( database.execute(
select(TableMovies)) select(TableMovies))
.all()]] .all()]]
current_movies_radarr = [str(movie['tmdbId']) for movie in movies if movie['hasFile'] and current_movies_radarr = [str(movie['id']) for movie in movies if movie['hasFile'] and
'movieFile' in movie and 'movieFile' in movie and
(movie['movieFile']['size'] > MINIMUM_VIDEO_SIZE or (movie['movieFile']['size'] > MINIMUM_VIDEO_SIZE or
get_movie_file_size_from_db(movie['movieFile']['path']) > MINIMUM_VIDEO_SIZE)] get_movie_file_size_from_db(movie['movieFile']['path']) > MINIMUM_VIDEO_SIZE)]
@ -143,7 +143,7 @@ def update_movies(send_event=True):
movies_deleted = [] movies_deleted = []
if len(movies_to_delete): if len(movies_to_delete):
try: try:
database.execute(delete(TableMovies).where(TableMovies.tmdbId.in_(movies_to_delete))) database.execute(delete(TableMovies).where(TableMovies.radarrId.in_(movies_to_delete)))
except IntegrityError as e: except IntegrityError as e:
logging.error(f"BAZARR cannot delete movies because of {e}") logging.error(f"BAZARR cannot delete movies because of {e}")
else: else:
@ -172,7 +172,7 @@ def update_movies(send_event=True):
if movie['hasFile'] is True: if movie['hasFile'] is True:
if 'movieFile' in movie: if 'movieFile' in movie:
if sync_monitored: if sync_monitored:
if get_movie_monitored_status(movie['tmdbId']) != movie['monitored']: if get_movie_monitored_status(movie['id']) != movie['monitored']:
# monitored status is not the same as our DB # monitored status is not the same as our DB
trace(f"{i}: (Monitor Status Mismatch) {movie['title']}") trace(f"{i}: (Monitor Status Mismatch) {movie['title']}")
elif not movie['monitored']: elif not movie['monitored']:
@ -184,7 +184,7 @@ def update_movies(send_event=True):
get_movie_file_size_from_db(movie['movieFile']['path']) > MINIMUM_VIDEO_SIZE): get_movie_file_size_from_db(movie['movieFile']['path']) > MINIMUM_VIDEO_SIZE):
# Add/update movies from Radarr that have a movie file to current movies list # Add/update movies from Radarr that have a movie file to current movies list
trace(f"{i}: (Processing) {movie['title']}") trace(f"{i}: (Processing) {movie['title']}")
if str(movie['tmdbId']) in current_movies_id_db: if str(movie['id']) in current_movies_id_db:
parsed_movie = movieParser(movie, action='update', parsed_movie = movieParser(movie, action='update',
tags_dict=tagsDict, tags_dict=tagsDict,
language_profiles=language_profiles, language_profiles=language_profiles,

@ -34,7 +34,7 @@ def get_language_profiles():
def get_series_monitored_table(): def get_series_monitored_table():
series_monitored = database.execute( series_monitored = database.execute(
select(TableShows.tvdbId, TableShows.monitored))\ select(TableShows.sonarrSeriesId, TableShows.monitored))\
.all() .all()
series_dict = dict((x, y) for x, y in series_monitored) series_dict = dict((x, y) for x, y in series_monitored)
return series_dict return series_dict
@ -95,7 +95,7 @@ def update_series(send_event=True):
if sync_monitored: if sync_monitored:
try: try:
monitored_status_db = bool_map[series_monitored[show['tvdbId']]] monitored_status_db = bool_map[series_monitored[show['id']]]
except KeyError: except KeyError:
monitored_status_db = None monitored_status_db = None
if monitored_status_db is None: if monitored_status_db is None:

Loading…
Cancel
Save