|
|
|
@ -23,6 +23,46 @@ def update_all_movies():
|
|
|
|
|
logging.info('BAZARR All existing movie subtitles indexed from disk.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_movie_file_size_from_db(movie_path):
|
|
|
|
|
try:
|
|
|
|
|
bazarr_file_size = os.path.getsize(path_mappings.path_replace_movie(movie_path))
|
|
|
|
|
except OSError:
|
|
|
|
|
bazarr_file_size = 0
|
|
|
|
|
return bazarr_file_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Update movies in DB
|
|
|
|
|
def update_movie(updated_movie, send_event):
|
|
|
|
|
try:
|
|
|
|
|
database.execute(
|
|
|
|
|
update(TableMovies).values(updated_movie)
|
|
|
|
|
.where(TableMovies.tmdbId == updated_movie['tmdbId']))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot update movie {updated_movie['path']} because of {e}")
|
|
|
|
|
else:
|
|
|
|
|
store_subtitles_movie(updated_movie['path'],
|
|
|
|
|
path_mappings.path_replace_movie(updated_movie['path']))
|
|
|
|
|
|
|
|
|
|
if send_event:
|
|
|
|
|
event_stream(type='movie', action='update', payload=updated_movie['radarrId'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Insert new movies in DB
|
|
|
|
|
def add_movie(added_movie, send_event):
|
|
|
|
|
try:
|
|
|
|
|
database.execute(
|
|
|
|
|
insert(TableMovies)
|
|
|
|
|
.values(added_movie))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot insert movie {added_movie['path']} because of {e}")
|
|
|
|
|
else:
|
|
|
|
|
store_subtitles_movie(added_movie['path'],
|
|
|
|
|
path_mappings.path_replace_movie(added_movie['path']))
|
|
|
|
|
|
|
|
|
|
if send_event:
|
|
|
|
|
event_stream(type='movie', action='update', payload=int(added_movie['radarrId']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_movies(send_event=True):
|
|
|
|
|
check_radarr_rootfolder()
|
|
|
|
|
logging.debug('BAZARR Starting movie sync from Radarr.')
|
|
|
|
@ -49,15 +89,35 @@ def update_movies(send_event=True):
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
# Get current movies in DB
|
|
|
|
|
current_movies_db = [x.tmdbId for x in
|
|
|
|
|
database.execute(
|
|
|
|
|
select(TableMovies.tmdbId))
|
|
|
|
|
.all()]
|
|
|
|
|
|
|
|
|
|
current_movies_radarr = []
|
|
|
|
|
movies_to_update = []
|
|
|
|
|
current_movies_id_db = [x.tmdbId for x in
|
|
|
|
|
database.execute(
|
|
|
|
|
select(TableMovies.tmdbId))
|
|
|
|
|
.all()]
|
|
|
|
|
current_movies_db_kv = [x.items() for x in [y._asdict()['TableMovies'].__dict__ for y in
|
|
|
|
|
database.execute(
|
|
|
|
|
select(TableMovies))
|
|
|
|
|
.all()]]
|
|
|
|
|
|
|
|
|
|
current_movies_radarr = [str(movie['tmdbId']) for movie in movies if movie['hasFile'] and
|
|
|
|
|
'movieFile' in movie and
|
|
|
|
|
(movie['movieFile']['size'] > 20480 or
|
|
|
|
|
get_movie_file_size_from_db(movie['movieFile']['path']) > 20480)]
|
|
|
|
|
movies_to_add = []
|
|
|
|
|
altered_movies = []
|
|
|
|
|
|
|
|
|
|
# Remove old movies from DB
|
|
|
|
|
movies_to_delete = list(set(current_movies_id_db) - set(current_movies_radarr))
|
|
|
|
|
|
|
|
|
|
if len(movies_to_delete):
|
|
|
|
|
try:
|
|
|
|
|
removed_movies = database.execute(delete(TableMovies)
|
|
|
|
|
.where(TableMovies.tmdbId.in_(movies_to_delete))
|
|
|
|
|
.returning(TableMovies.radarrId))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot delete movies because of {e}")
|
|
|
|
|
else:
|
|
|
|
|
for removed_movie in removed_movies:
|
|
|
|
|
if send_event:
|
|
|
|
|
event_stream(type='movie', action='delete', payload=removed_movie.radarrId)
|
|
|
|
|
|
|
|
|
|
# Build new and updated movies
|
|
|
|
|
movies_count = len(movies)
|
|
|
|
@ -71,75 +131,26 @@ def update_movies(send_event=True):
|
|
|
|
|
|
|
|
|
|
if movie['hasFile'] is True:
|
|
|
|
|
if 'movieFile' in movie:
|
|
|
|
|
try:
|
|
|
|
|
bazarr_file_size = \
|
|
|
|
|
os.path.getsize(path_mappings.path_replace_movie(movie['movieFile']['path']))
|
|
|
|
|
except OSError:
|
|
|
|
|
bazarr_file_size = 0
|
|
|
|
|
if movie['movieFile']['size'] > 20480 or bazarr_file_size > 20480:
|
|
|
|
|
if (movie['movieFile']['size'] > 20480 or
|
|
|
|
|
get_movie_file_size_from_db(movie['movieFile']['path']) > 20480):
|
|
|
|
|
# Add movies in radarr to current movies list
|
|
|
|
|
current_movies_radarr.append(str(movie['tmdbId']))
|
|
|
|
|
|
|
|
|
|
if str(movie['tmdbId']) in current_movies_db:
|
|
|
|
|
movies_to_update.append(movieParser(movie, action='update',
|
|
|
|
|
tags_dict=tagsDict,
|
|
|
|
|
movie_default_profile=movie_default_profile,
|
|
|
|
|
audio_profiles=audio_profiles))
|
|
|
|
|
if str(movie['tmdbId']) in current_movies_id_db:
|
|
|
|
|
parsed_movie = movieParser(movie, action='update',
|
|
|
|
|
tags_dict=tagsDict,
|
|
|
|
|
movie_default_profile=movie_default_profile,
|
|
|
|
|
audio_profiles=audio_profiles)
|
|
|
|
|
if not any([parsed_movie.items() <= x for x in current_movies_db_kv]):
|
|
|
|
|
update_movie(parsed_movie, send_event)
|
|
|
|
|
else:
|
|
|
|
|
movies_to_add.append(movieParser(movie, action='insert',
|
|
|
|
|
tags_dict=tagsDict,
|
|
|
|
|
movie_default_profile=movie_default_profile,
|
|
|
|
|
audio_profiles=audio_profiles))
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
if send_event:
|
|
|
|
|
hide_progress(id='movies_progress')
|
|
|
|
|
|
|
|
|
|
# Remove old movies from DB
|
|
|
|
|
removed_movies = list(set(current_movies_db) - set(current_movies_radarr))
|
|
|
|
|
|
|
|
|
|
for removed_movie in removed_movies:
|
|
|
|
|
database.execute(
|
|
|
|
|
delete(TableMovies)
|
|
|
|
|
.where(TableMovies.tmdbId == removed_movie))
|
|
|
|
|
|
|
|
|
|
# Update movies in DB
|
|
|
|
|
for updated_movie in movies_to_update:
|
|
|
|
|
if database.execute(
|
|
|
|
|
select(TableMovies)
|
|
|
|
|
.filter_by(**updated_movie))\
|
|
|
|
|
.first():
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
database.execute(
|
|
|
|
|
update(TableMovies).values(updated_movie)
|
|
|
|
|
.where(TableMovies.tmdbId == updated_movie['tmdbId']))
|
|
|
|
|
|
|
|
|
|
altered_movies.append([updated_movie['tmdbId'],
|
|
|
|
|
updated_movie['path'],
|
|
|
|
|
updated_movie['radarrId'],
|
|
|
|
|
updated_movie['monitored']])
|
|
|
|
|
|
|
|
|
|
# Insert new movies in DB
|
|
|
|
|
for added_movie in movies_to_add:
|
|
|
|
|
try:
|
|
|
|
|
database.execute(
|
|
|
|
|
insert(TableMovies)
|
|
|
|
|
.values(added_movie))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot update movie {added_movie['path']} because of {e}")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
altered_movies.append([added_movie['tmdbId'],
|
|
|
|
|
added_movie['path'],
|
|
|
|
|
added_movie['radarrId'],
|
|
|
|
|
added_movie['monitored']])
|
|
|
|
|
if send_event:
|
|
|
|
|
event_stream(type='movie', action='update', payload=int(added_movie['radarrId']))
|
|
|
|
|
|
|
|
|
|
# Store subtitles for added or modified movies
|
|
|
|
|
for i, altered_movie in enumerate(altered_movies, 1):
|
|
|
|
|
store_subtitles_movie(altered_movie[1], path_mappings.path_replace_movie(altered_movie[1]))
|
|
|
|
|
|
|
|
|
|
logging.debug('BAZARR All movies synced from Radarr into database.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -155,13 +166,17 @@ def update_one_movie(movie_id, action, defer_search=False):
|
|
|
|
|
# Remove movie from DB
|
|
|
|
|
if action == 'deleted':
|
|
|
|
|
if existing_movie:
|
|
|
|
|
database.execute(
|
|
|
|
|
delete(TableMovies)
|
|
|
|
|
.where(TableMovies.radarrId == movie_id))
|
|
|
|
|
|
|
|
|
|
event_stream(type='movie', action='delete', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|
existing_movie.path)))
|
|
|
|
|
try:
|
|
|
|
|
database.execute(
|
|
|
|
|
delete(TableMovies)
|
|
|
|
|
.where(TableMovies.radarrId == movie_id))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot delete movie {path_mappings.path_replace_movie(existing_movie.path)} "
|
|
|
|
|
f"because of {e}")
|
|
|
|
|
else:
|
|
|
|
|
event_stream(type='movie', action='delete', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|
existing_movie.path)))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
movie_default_enabled = settings.general.getboolean('movie_default_enabled')
|
|
|
|
@ -200,25 +215,33 @@ def update_one_movie(movie_id, action, defer_search=False):
|
|
|
|
|
|
|
|
|
|
# Remove movie from DB
|
|
|
|
|
if not movie and existing_movie:
|
|
|
|
|
database.execute(
|
|
|
|
|
delete(TableMovies)
|
|
|
|
|
.where(TableMovies.radarrId == movie_id))
|
|
|
|
|
|
|
|
|
|
event_stream(type='movie', action='delete', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|
existing_movie.path)))
|
|
|
|
|
try:
|
|
|
|
|
database.execute(
|
|
|
|
|
delete(TableMovies)
|
|
|
|
|
.where(TableMovies.radarrId == movie_id))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot delete movie {path_mappings.path_replace_movie(existing_movie.path)} because "
|
|
|
|
|
f"of {e}")
|
|
|
|
|
else:
|
|
|
|
|
event_stream(type='movie', action='delete', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|
existing_movie.path)))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Update existing movie in DB
|
|
|
|
|
elif movie and existing_movie:
|
|
|
|
|
database.execute(
|
|
|
|
|
update(TableMovies)
|
|
|
|
|
.values(movie)
|
|
|
|
|
.where(TableMovies.radarrId == movie['radarrId']))
|
|
|
|
|
|
|
|
|
|
event_stream(type='movie', action='update', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR updated this movie into the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|
movie['path'])))
|
|
|
|
|
try:
|
|
|
|
|
database.execute(
|
|
|
|
|
update(TableMovies)
|
|
|
|
|
.values(movie)
|
|
|
|
|
.where(TableMovies.radarrId == movie['radarrId']))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot update movie {path_mappings.path_replace_movie(movie['path'])} because "
|
|
|
|
|
f"of {e}")
|
|
|
|
|
else:
|
|
|
|
|
event_stream(type='movie', action='update', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR updated this movie into the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|
movie['path'])))
|
|
|
|
|
|
|
|
|
|
# Insert new movie in DB
|
|
|
|
|
elif movie and not existing_movie:
|
|
|
|
@ -227,7 +250,8 @@ def update_one_movie(movie_id, action, defer_search=False):
|
|
|
|
|
insert(TableMovies)
|
|
|
|
|
.values(movie))
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
logging.error(f"BAZARR cannot insert movie {movie['path']} because of {e}")
|
|
|
|
|
logging.error(f"BAZARR cannot insert movie {path_mappings.path_replace_movie(movie['path'])} because "
|
|
|
|
|
f"of {e}")
|
|
|
|
|
else:
|
|
|
|
|
event_stream(type='movie', action='update', payload=int(movie_id))
|
|
|
|
|
logging.debug('BAZARR inserted this movie into the database:{}'.format(path_mappings.path_replace_movie(
|
|
|
|
|