def wanted_search_missing_subtitles_movies(): conditions = [(TableMovies.missing_subtitles != '[]')] conditions += get_exclusion_clause('movie') movies = TableMovies.select(TableMovies.radarrId, TableMovies.tags, TableMovies.monitored, TableMovies.title) \ .where(reduce(operator.and_, conditions)) \ .dicts() movies = list(movies) count_movies = len(movies) for i, movie in enumerate(movies): show_progress(id='wanted_movies_progress', header='Searching subtitles...', name=movie['title'], value=i, count=count_movies) providers = get_providers() if providers: wanted_download_subtitles_movie(movie['radarrId']) else: logging.info("BAZARR All providers are throttled") break hide_progress(id='wanted_movies_progress') logging.info('BAZARR Finished searching for missing Movies Subtitles. Check History for more information.')
def series_full_scan_subtitles(): use_ffprobe_cache = settings.sonarr.getboolean('use_ffprobe_cache') episodes = TableEpisodes.select(TableEpisodes.path).dicts() count_episodes = len(episodes) for i, episode in enumerate(episodes): show_progress(id='episodes_disk_scan', header='Full disk scan...', name='Episodes subtitles', value=i, count=count_episodes) store_subtitles(episode['path'], path_mappings.path_replace(episode['path']), use_cache=use_ffprobe_cache) hide_progress(id='episodes_disk_scan') gc.collect()
def movies_full_scan_subtitles(): use_ffprobe_cache = settings.radarr.getboolean('use_ffprobe_cache') movies = TableMovies.select(TableMovies.path).dicts() count_movies = len(movies) for i, movie in enumerate(movies): show_progress(id='movies_disk_scan', header='Full disk scan...', name='Movies subtitles', value=i, count=count_movies) store_subtitles_movie(movie['path'], path_mappings.path_replace_movie(movie['path']), use_cache=use_ffprobe_cache) hide_progress(id='movies_disk_scan') gc.collect()
def wanted_search_missing_subtitles_series(): conditions = [(TableEpisodes.missing_subtitles != '[]')] conditions += get_exclusion_clause('series') episodes = TableEpisodes.select(TableEpisodes.sonarrSeriesId, TableEpisodes.sonarrEpisodeId, TableShows.tags, TableEpisodes.monitored, TableShows.title, TableEpisodes.season, TableEpisodes.episode, TableEpisodes.title.alias('episodeTitle'), TableShows.seriesType)\ .join(TableShows, on=(TableEpisodes.sonarrSeriesId == TableShows.sonarrSeriesId))\ .where(reduce(operator.and_, conditions))\ .dicts() episodes = list(episodes) count_episodes = len(episodes) for i, episode in enumerate(episodes): show_progress(id='wanted_episodes_progress', header='Searching subtitles...', name='{0} - S{1:02d}E{2:02d} - {3}'.format(episode['title'], episode['season'], episode['episode'], episode['episodeTitle']), value=i, count=count_episodes) providers = get_providers() if providers: wanted_download_subtitles(episode['sonarrEpisodeId']) else: logging.info("BAZARR All providers are throttled") break hide_progress(id='wanted_episodes_progress') logging.info('BAZARR Finished searching for missing Series Subtitles. Check History for more information.')
def update_series(send_event=True): check_sonarr_rootfolder() apikey_sonarr = settings.sonarr.apikey if apikey_sonarr is None: return serie_default_enabled = settings.general.getboolean('serie_default_enabled') if serie_default_enabled is True: serie_default_profile = settings.general.serie_default_profile if serie_default_profile == '': serie_default_profile = None else: serie_default_profile = None audio_profiles = get_profile_list() tagsDict = get_tags() # Get shows data from Sonarr series = get_series_from_sonarr_api(url=url_sonarr(), apikey_sonarr=apikey_sonarr) if not series: return else: # Get current shows in DB current_shows_db = TableShows.select(TableShows.sonarrSeriesId).dicts() current_shows_db_list = [x['sonarrSeriesId'] for x in current_shows_db] current_shows_sonarr = [] series_to_update = [] series_to_add = [] series_count = len(series) for i, show in enumerate(series): if send_event: show_progress(id='series_progress', header='Syncing series...', name=show['title'], value=i, count=series_count) # Add shows in Sonarr to current shows list current_shows_sonarr.append(show['id']) if show['id'] in current_shows_db_list: series_to_update.append(seriesParser(show, action='update', tags_dict=tagsDict, serie_default_profile=serie_default_profile, audio_profiles=audio_profiles)) else: series_to_add.append(seriesParser(show, action='insert', tags_dict=tagsDict, serie_default_profile=serie_default_profile, audio_profiles=audio_profiles)) if send_event: hide_progress(id='series_progress') # Remove old series from DB removed_series = list(set(current_shows_db_list) - set(current_shows_sonarr)) for series in removed_series: try: TableShows.delete().where(TableShows.sonarrSeriesId == series).execute() except Exception as e: logging.error(f"BAZARR cannot delete series with sonarrSeriesId {series} because of {e}") continue else: if send_event: event_stream(type='series', action='delete', payload=series) # Update existing series in DB series_in_db_list = [] series_in_db = TableShows.select(TableShows.title, TableShows.path, TableShows.tvdbId, TableShows.sonarrSeriesId, TableShows.overview, TableShows.poster, TableShows.fanart, TableShows.audio_language, TableShows.sortTitle, TableShows.year, TableShows.alternateTitles, TableShows.tags, TableShows.seriesType, TableShows.imdbId).dicts() for item in series_in_db: series_in_db_list.append(item) series_to_update_list = [i for i in series_to_update if i not in series_in_db_list] for updated_series in series_to_update_list: try: TableShows.update(updated_series).where(TableShows.sonarrSeriesId == updated_series['sonarrSeriesId']).execute() except IntegrityError as e: logging.error(f"BAZARR cannot update series {updated_series['path']} because of {e}") continue else: if send_event: event_stream(type='series', payload=updated_series['sonarrSeriesId']) # Insert new series in DB for added_series in series_to_add: try: result = TableShows.insert(added_series).on_conflict(action='IGNORE').execute() except IntegrityError as e: logging.error(f"BAZARR cannot insert series {added_series['path']} because of {e}") continue else: if result: list_missing_subtitles(no=added_series['sonarrSeriesId']) else: logging.debug('BAZARR unable to insert this series into the database:', path_mappings.path_replace(added_series['path'])) if send_event: event_stream(type='series', action='update', payload=added_series['sonarrSeriesId']) logging.debug('BAZARR All series synced from Sonarr into database.')
def sync_episodes(series_id=None, send_event=True): logging.debug('BAZARR Starting episodes sync from Sonarr.') apikey_sonarr = settings.sonarr.apikey # Get current episodes id in DB current_episodes_db = TableEpisodes.select(TableEpisodes.sonarrEpisodeId, TableEpisodes.path, TableEpisodes.sonarrSeriesId)\ .where((TableEpisodes.sonarrSeriesId == series_id) if series_id else None)\ .dicts() current_episodes_db_list = [ x['sonarrEpisodeId'] for x in current_episodes_db ] current_episodes_sonarr = [] episodes_to_update = [] episodes_to_add = [] altered_episodes = [] # Get sonarrId for each series from database seriesIdList = get_series_from_sonarr_api( series_id=series_id, url=url_sonarr(), apikey_sonarr=apikey_sonarr, ) series_count = len(seriesIdList) for i, seriesId in enumerate(seriesIdList): if send_event: show_progress(id='episodes_progress', header='Syncing episodes...', name=seriesId['title'], value=i, count=series_count) # Get episodes data for a series from Sonarr episodes = get_episodes_from_sonarr_api( url=url_sonarr(), apikey_sonarr=apikey_sonarr, series_id=seriesId['sonarrSeriesId']) if not episodes: continue else: # For Sonarr v3, we need to update episodes to integrate the episodeFile API endpoint results if not get_sonarr_info.is_legacy(): episodeFiles = get_episodesFiles_from_sonarr_api( url=url_sonarr(), apikey_sonarr=apikey_sonarr, series_id=seriesId['sonarrSeriesId']) for episode in episodes: if episode['hasFile']: item = [ x for x in episodeFiles if x['id'] == episode['episodeFileId'] ] if item: episode['episodeFile'] = item[0] for episode in episodes: if 'hasFile' in episode: if episode['hasFile'] is True: if 'episodeFile' in episode: if episode['episodeFile']['size'] > 20480: # Add episodes in sonarr to current episode list current_episodes_sonarr.append(episode['id']) # Parse episode data if episode['id'] in current_episodes_db_list: episodes_to_update.append( episodeParser(episode)) else: episodes_to_add.append( episodeParser(episode)) if send_event: hide_progress(id='episodes_progress') # Remove old episodes from DB removed_episodes = list( set(current_episodes_db_list) - set(current_episodes_sonarr)) for removed_episode in removed_episodes: episode_to_delete = TableEpisodes.select(TableEpisodes.sonarrSeriesId, TableEpisodes.sonarrEpisodeId)\ .where(TableEpisodes.sonarrEpisodeId == removed_episode)\ .dicts()\ .get() TableEpisodes.delete().where( TableEpisodes.sonarrEpisodeId == removed_episode).execute() if send_event: event_stream(type='episode', action='delete', payload=episode_to_delete['sonarrEpisodeId']) # Update existing episodes in DB episode_in_db_list = [] episodes_in_db = TableEpisodes.select( TableEpisodes.sonarrSeriesId, TableEpisodes.sonarrEpisodeId, TableEpisodes.title, TableEpisodes.path, TableEpisodes.season, TableEpisodes.episode, TableEpisodes.scene_name, TableEpisodes.monitored, TableEpisodes.format, TableEpisodes.resolution, TableEpisodes.video_codec, TableEpisodes.audio_codec, TableEpisodes.episode_file_id, TableEpisodes.audio_language, TableEpisodes.file_size).dicts() for item in episodes_in_db: episode_in_db_list.append(item) episodes_to_update_list = [ i for i in episodes_to_update if i not in episode_in_db_list ] for updated_episode in episodes_to_update_list: TableEpisodes.update(updated_episode).where( TableEpisodes.sonarrEpisodeId == updated_episode['sonarrEpisodeId']).execute() altered_episodes.append([ updated_episode['sonarrEpisodeId'], updated_episode['path'], updated_episode['sonarrSeriesId'] ]) # Insert new episodes in DB for added_episode in episodes_to_add: result = TableEpisodes.insert(added_episode).on_conflict( action='IGNORE').execute() if result > 0: altered_episodes.append([ added_episode['sonarrEpisodeId'], added_episode['path'], added_episode['monitored'] ]) if send_event: event_stream(type='episode', payload=added_episode['sonarrEpisodeId']) else: logging.debug( 'BAZARR unable to insert this episode into the database:{}'. format(path_mappings.path_replace(added_episode['path']))) # Store subtitles for added or modified episodes for i, altered_episode in enumerate(altered_episodes, 1): store_subtitles(altered_episode[1], path_mappings.path_replace(altered_episode[1])) logging.debug('BAZARR All episodes synced from Sonarr into database.')
def update_movies(send_event=True): check_radarr_rootfolder() logging.debug('BAZARR Starting movie sync from Radarr.') apikey_radarr = settings.radarr.apikey movie_default_enabled = settings.general.getboolean( 'movie_default_enabled') if movie_default_enabled is True: movie_default_profile = settings.general.movie_default_profile if movie_default_profile == '': movie_default_profile = None else: movie_default_profile = None if apikey_radarr is None: pass else: audio_profiles = get_profile_list() tagsDict = get_tags() # Get movies data from radarr movies = get_movies_from_radarr_api(url=url_radarr(), apikey_radarr=apikey_radarr) if not movies: return else: # Get current movies in DB current_movies_db = TableMovies.select( TableMovies.tmdbId, TableMovies.path, TableMovies.radarrId).dicts() current_movies_db_list = [x['tmdbId'] for x in current_movies_db] current_movies_radarr = [] movies_to_update = [] movies_to_add = [] altered_movies = [] # Build new and updated movies movies_count = len(movies) for i, movie in enumerate(movies): if send_event: show_progress(id='movies_progress', header='Syncing movies...', name=movie['title'], value=i, count=movies_count) if movie['hasFile'] is True: if 'movieFile' in movie: if movie['movieFile']['size'] > 20480: # Add movies in radarr to current movies list current_movies_radarr.append(str(movie['tmdbId'])) if str(movie['tmdbId']) in current_movies_db_list: movies_to_update.append( movieParser(movie, action='update', tags_dict=tagsDict, movie_default_profile= movie_default_profile, audio_profiles=audio_profiles)) else: movies_to_add.append( movieParser(movie, action='insert', tags_dict=tagsDict, movie_default_profile= movie_default_profile, audio_profiles=audio_profiles)) if send_event: hide_progress(id='movies_progress') # Remove old movies from DB removed_movies = list( set(current_movies_db_list) - set(current_movies_radarr)) for removed_movie in removed_movies: try: TableMovies.delete().where( TableMovies.tmdbId == removed_movie).execute() except Exception as e: logging.error( f"BAZARR cannot remove movie tmdbId {removed_movie} because of {e}" ) continue # Update movies in DB movies_in_db_list = [] movies_in_db = TableMovies.select( TableMovies.radarrId, TableMovies.title, TableMovies.path, TableMovies.tmdbId, TableMovies.overview, TableMovies.poster, TableMovies.fanart, TableMovies.audio_language, TableMovies.sceneName, TableMovies.monitored, TableMovies.sortTitle, TableMovies.year, TableMovies.alternativeTitles, TableMovies.format, TableMovies.resolution, TableMovies.video_codec, TableMovies.audio_codec, TableMovies.imdbId, TableMovies.movie_file_id, TableMovies.tags, TableMovies.file_size).dicts() for item in movies_in_db: movies_in_db_list.append(item) movies_to_update_list = [ i for i in movies_to_update if i not in movies_in_db_list ] for updated_movie in movies_to_update_list: try: TableMovies.update(updated_movie).where( TableMovies.tmdbId == updated_movie['tmdbId']).execute() except IntegrityError as e: logging.error( f"BAZARR cannot update movie {updated_movie['path']} because of {e}" ) continue else: 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: result = TableMovies.insert(added_movie).on_conflict( action='IGNORE').execute() except IntegrityError as e: logging.error( f"BAZARR cannot insert movie {added_movie['path']} because of {e}" ) continue else: if result > 0: 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'])) else: logging.debug( 'BAZARR unable to insert this movie into the database:', path_mappings.path_replace_movie( added_movie['path'])) # 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.')
def movies_download_subtitles(no): conditions = [(TableMovies.radarrId == no)] conditions += get_exclusion_clause('movie') movies = TableMovies.select(TableMovies.path, TableMovies.missing_subtitles, TableMovies.audio_language, TableMovies.radarrId, TableMovies.sceneName, TableMovies.title, TableMovies.tags, TableMovies.monitored)\ .where(reduce(operator.and_, conditions))\ .dicts() if not len(movies): logging.debug( "BAZARR no movie with that radarrId can be found in database:", str(no)) return else: movie = movies[0] if ast.literal_eval(movie['missing_subtitles']): count_movie = len(ast.literal_eval(movie['missing_subtitles'])) else: count_movie = 0 audio_language_list = get_audio_profile_languages( movie_id=movie['radarrId']) if len(audio_language_list) > 0: audio_language = audio_language_list[0]['name'] else: audio_language = 'None' languages = [] providers_list = None for i, language in enumerate(ast.literal_eval(movie['missing_subtitles'])): providers_list = get_providers() if language is not None: hi_ = "True" if language.endswith(':hi') else "False" forced_ = "True" if language.endswith(':forced') else "False" languages.append((language.split(":")[0], hi_, forced_)) if providers_list: # confirm if language is still missing or if cutoff have been reached confirmed_missing_subs = TableMovies.select(TableMovies.missing_subtitles) \ .where(TableMovies.radarrId == movie['radarrId']) \ .dicts() \ .get_or_none() if not confirmed_missing_subs: continue if language not in ast.literal_eval( confirmed_missing_subs['missing_subtitles']): continue show_progress(id='movie_search_progress_{}'.format(no), header='Searching missing subtitles...', name=movie['title'], value=i, count=count_movie) if providers_list: for result in generate_subtitles( path_mappings.path_replace_movie(movie['path']), languages, audio_language, str(movie['sceneName']), movie['title'], 'movie'): if result: message = result[0] path = result[1] forced = result[5] if result[8]: language_code = result[2] + ":hi" elif forced: language_code = result[2] + ":forced" else: language_code = result[2] provider = result[3] score = result[4] subs_id = result[6] subs_path = result[7] store_subtitles_movie( movie['path'], path_mappings.path_replace_movie(movie['path'])) history_log_movie(1, no, message, path, language_code, provider, score, subs_id, subs_path) send_notifications_movie(no, message) else: logging.info("BAZARR All providers are throttled") hide_progress(id='movie_search_progress_{}'.format(no))
def series_download_subtitles(no): conditions = [(TableEpisodes.sonarrSeriesId == no), (TableEpisodes.missing_subtitles != '[]')] conditions += get_exclusion_clause('series') episodes_details = TableEpisodes.select(TableEpisodes.path, TableEpisodes.missing_subtitles, TableEpisodes.monitored, TableEpisodes.sonarrEpisodeId, TableEpisodes.scene_name, TableShows.tags, TableShows.seriesType, TableEpisodes.audio_language, TableShows.title, TableEpisodes.season, TableEpisodes.episode, TableEpisodes.title.alias('episodeTitle')) \ .join(TableShows, on=(TableEpisodes.sonarrSeriesId == TableShows.sonarrSeriesId)) \ .where(reduce(operator.and_, conditions)) \ .dicts() if not episodes_details: logging.debug( "BAZARR no episode for that sonarrSeriesId have been found in database or they have all been " "ignored because of monitored status, series type or series tags: {}" .format(no)) return count_episodes_details = len(episodes_details) for i, episode in enumerate(episodes_details): providers_list = get_providers() if providers_list: show_progress(id='series_search_progress_{}'.format(no), header='Searching missing subtitles...', name='{0} - S{1:02d}E{2:02d} - {3}'.format( episode['title'], episode['season'], episode['episode'], episode['episodeTitle']), value=i, count=count_episodes_details) audio_language_list = get_audio_profile_languages( episode_id=episode['sonarrEpisodeId']) if len(audio_language_list) > 0: audio_language = audio_language_list[0]['name'] else: audio_language = 'None' languages = [] for language in ast.literal_eval(episode['missing_subtitles']): # confirm if language is still missing or if cutoff have been reached confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \ .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \ .dicts() \ .get_or_none() if not confirmed_missing_subs: continue if language not in ast.literal_eval( confirmed_missing_subs['missing_subtitles']): continue if language is not None: hi_ = "True" if language.endswith(':hi') else "False" forced_ = "True" if language.endswith( ':forced') else "False" languages.append((language.split(":")[0], hi_, forced_)) if not languages: continue for result in generate_subtitles( path_mappings.path_replace(episode['path']), languages, audio_language, str(episode['scene_name']), episode['title'], 'series'): if result: message = result[0] path = result[1] forced = result[5] if result[8]: language_code = result[2] + ":hi" elif forced: language_code = result[2] + ":forced" else: language_code = result[2] provider = result[3] score = result[4] subs_id = result[6] subs_path = result[7] store_subtitles( episode['path'], path_mappings.path_replace(episode['path'])) history_log(1, no, episode['sonarrEpisodeId'], message, path, language_code, provider, score, subs_id, subs_path) send_notifications(no, episode['sonarrEpisodeId'], message) else: logging.info("BAZARR All providers are throttled") break hide_progress(id='series_search_progress_{}'.format(no))