diff --git a/bazarr/api.py b/bazarr/api.py index 5189a9bf7..cfed43a75 100644 --- a/bazarr/api.py +++ b/bazarr/api.py @@ -18,7 +18,7 @@ from config import settings, base_url, save_settings from init import * import logging -from database import database, filter_exclusions +from database import database, get_exclusion_clause from helper import path_mappings from get_languages import language_from_alpha3, language_from_alpha2, alpha2_from_alpha3, alpha2_from_language, \ alpha3_from_language, alpha3_from_alpha2 @@ -86,8 +86,7 @@ class BadgesSeries(Resource): missing_episodes = database.execute("SELECT table_shows.tags, table_episodes.monitored, table_shows.seriesType " "FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId =" " table_episodes.sonarrSeriesId WHERE missing_subtitles is not null AND " - "missing_subtitles != '[]'") - missing_episodes = filter_exclusions(missing_episodes, 'series') + "missing_subtitles != '[]'" + get_exclusion_clause('series')) missing_episodes = len(missing_episodes) result = { @@ -100,8 +99,7 @@ class BadgesMovies(Resource): @authenticate def get(self): missing_movies = database.execute("SELECT tags, monitored FROM table_movies WHERE missing_subtitles is not " - "null AND missing_subtitles != '[]'") - missing_movies = filter_exclusions(missing_movies, 'movie') + "null AND missing_subtitles != '[]'" + get_exclusion_clause('movie')) missing_movies = len(missing_movies) result = { @@ -330,8 +328,8 @@ class Series(Resource): "table_shows.seriesType FROM table_episodes INNER JOIN table_shows " "on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId " "WHERE table_episodes.sonarrSeriesId=? AND missing_subtitles is not " - "null AND missing_subtitles != '[]'", (item['sonarrSeriesId'],)) - episodeMissingCount = filter_exclusions(episodeMissingCount, 'series') + "null AND missing_subtitles != '[]'" + + get_exclusion_clause('series'), (item['sonarrSeriesId'],)) episodeMissingCount = len(episodeMissingCount) item.update({"episodeMissingCount": episodeMissingCount}) @@ -339,8 +337,8 @@ class Series(Resource): episodeFileCount = database.execute("SELECT table_shows.tags, table_episodes.monitored, " "table_shows.seriesType FROM table_episodes INNER JOIN table_shows on " "table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE " - "table_episodes.sonarrSeriesId=?", (item['sonarrSeriesId'],)) - episodeFileCount = filter_exclusions(episodeFileCount, 'series') + "table_episodes.sonarrSeriesId=?" + get_exclusion_clause('series'), + (item['sonarrSeriesId'],)) episodeFileCount = len(episodeFileCount) item.update({"episodeFileCount": episodeFileCount}) @@ -460,7 +458,7 @@ class SeriesEditSave(Resource): list_missing_subtitles(no=seriesId, send_event=False) event_stream(type='series_editor', action='update') - event_stream(type='badges') + event_stream(type='badges_series') return '', 204 @@ -1024,7 +1022,7 @@ class MoviesEditSave(Resource): list_missing_subtitles_movies(no=radarrId, send_event=False) event_stream(type='movies_editor', action='update') - event_stream(type='badges') + event_stream(type='badges_movies') return '', 204 @@ -1329,9 +1327,9 @@ class HistorySeries(Resource): "table_shows.seriesType FROM table_history INNER JOIN table_episodes on " "table_episodes.sonarrEpisodeId = table_history.sonarrEpisodeId INNER JOIN table_shows on " "table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE action IN (" + - ','.join(map(str, query_actions)) + ") AND timestamp > ? AND score is not null GROUP BY " - "table_history.video_path, table_history.language", (minimum_timestamp,)) - upgradable_episodes = filter_exclusions(upgradable_episodes, 'series') + ','.join(map(str, query_actions)) + ") AND timestamp > ? AND score is not null" + + get_exclusion_clause('series') + " GROUP BY table_history.video_path, table_history.language", + (minimum_timestamp,)) for upgradable_episode in upgradable_episodes: if upgradable_episode['timestamp'] > minimum_timestamp: @@ -1432,9 +1430,8 @@ class HistoryMovies(Resource): upgradable_movies = database.execute( "SELECT video_path, MAX(timestamp) as timestamp, score, tags, monitored FROM table_history_movie " "INNER JOIN table_movies on table_movies.radarrId=table_history_movie.radarrId WHERE action IN (" + - ','.join(map(str, query_actions)) + ") AND timestamp > ? AND score is not NULL GROUP BY video_path, " - "language", (minimum_timestamp,)) - upgradable_movies = filter_exclusions(upgradable_movies, 'movie') + ','.join(map(str, query_actions)) + ") AND timestamp > ? AND score is not NULL" + + get_exclusion_clause('movie') + " GROUP BY video_path, language", (minimum_timestamp,)) for upgradable_movie in upgradable_movies: if upgradable_movie['timestamp'] > minimum_timestamp: @@ -1571,8 +1568,8 @@ class WantedSeries(Resource): data_count = database.execute("SELECT table_episodes.monitored, table_shows.tags, table_shows.seriesType FROM " "table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = " - "table_episodes.sonarrSeriesId WHERE table_episodes.missing_subtitles != '[]'") - data_count = filter_exclusions(data_count, 'series') + "table_episodes.sonarrSeriesId WHERE table_episodes.missing_subtitles != '[]'" + + get_exclusion_clause('series')) row_count = len(data_count) data = database.execute("SELECT table_shows.title as seriesTitle, table_episodes.monitored, " "table_episodes.season || 'x' || table_episodes.episode as episode_number, " @@ -1581,8 +1578,8 @@ class WantedSeries(Resource): "table_episodes.sonarrEpisodeId, table_episodes.scene_name, table_shows.tags, " "table_episodes.failedAttempts, table_shows.seriesType FROM table_episodes INNER JOIN " "table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE " - "table_episodes.missing_subtitles != '[]' ORDER BY table_episodes._rowid_ DESC") - data = filter_exclusions(data, 'series')[int(start):int(start)+int(length)] + "table_episodes.missing_subtitles != '[]'" + get_exclusion_clause('series') + + " ORDER BY table_episodes._rowid_ DESC LIMIT " + length + " OFFSET " + start) for item in data: # Parse missing subtitles @@ -1614,13 +1611,13 @@ class WantedMovies(Resource): length = request.args.get('length') or -1 draw = request.args.get('draw') - data_count = database.execute("SELECT tags, monitored FROM table_movies WHERE missing_subtitles != '[]'") - data_count = filter_exclusions(data_count, 'movie') + data_count = database.execute("SELECT tags, monitored FROM table_movies WHERE missing_subtitles != '[]'" + + get_exclusion_clause('movie')) row_count = len(data_count) data = database.execute("SELECT title, missing_subtitles, radarrId, path, hearing_impaired, sceneName, " - "failedAttempts, tags, monitored FROM table_movies WHERE missing_subtitles != '[]' " - "ORDER BY _rowid_ DESC") - data = filter_exclusions(data, 'movie')[int(start):int(start)+int(length)] + "failedAttempts, tags, monitored FROM table_movies WHERE missing_subtitles != '[]'" + + get_exclusion_clause('movie') + " ORDER BY _rowid_ DESC LIMIT " + length + " OFFSET " + + start) for item in data: # Parse missing subtitles diff --git a/bazarr/config.py b/bazarr/config.py index 72fe3cb3e..e45165a4c 100644 --- a/bazarr/config.py +++ b/bazarr/config.py @@ -228,6 +228,11 @@ def save_settings(settings_items): 'settings-proxy-password']: configure_proxy = True + if key in ['settings-sonarr-excluded_tags', 'settings-sonarr-only_monitored', + 'settings-sonarr-excluded_series_types', 'settings.radarr.excluded_tags', + 'settings-radarr-only_monitored']: + exclusion_updated = True + if settings_keys[0] == 'settings': settings[settings_keys[1]][settings_keys[2]] = str(value) @@ -253,6 +258,11 @@ def save_settings(settings_items): if configure_proxy: configure_proxy_func() + if exclusion_updated: + from event_handler import event_stream + event_stream(type='badges_series') + event_stream(type='badges_movies') + def url_sonarr(): if settings.sonarr.getboolean('ssl'): diff --git a/bazarr/database.py b/bazarr/database.py index 5046f9974..005d96345 100644 --- a/bazarr/database.py +++ b/bazarr/database.py @@ -149,28 +149,29 @@ def db_upgrade(): "provider text, subs_id text, language text)") -def filter_exclusions(dicts_list, type): +def get_exclusion_clause(type): + where_clause = '' if type == 'series': tagsList = ast.literal_eval(settings.sonarr.excluded_tags) - monitoredOnly = settings.sonarr.getboolean('only_monitored') + for tag in tagsList: + where_clause += ' AND table_shows.tags NOT LIKE "%\'' + tag + '\'%"' else: tagsList = ast.literal_eval(settings.radarr.excluded_tags) - monitoredOnly = settings.radarr.getboolean('only_monitored') + for tag in tagsList: + where_clause += ' AND table_movies.tags NOT LIKE "%\'' + tag + '\'%"' - # Filter tags - dictsList_tags_filtered = [item for item in dicts_list if set(tagsList).isdisjoint(ast.literal_eval(item['tags']))] - - # Filter monitored - if monitoredOnly: - dictsList_monitored_filtered = [item for item in dictsList_tags_filtered if item['monitored'] == 'True'] + if type == 'series': + monitoredOnly = settings.sonarr.getboolean('only_monitored') + if monitoredOnly: + where_clause += ' AND table_episodes.monitored = "True"' else: - dictsList_monitored_filtered = dictsList_tags_filtered + monitoredOnly = settings.radarr.getboolean('only_monitored') + if monitoredOnly: + where_clause += ' AND table_movies.monitored = "True"' - # Filter series type if type == 'series': - dictsList_types_filtered = [item for item in dictsList_monitored_filtered if item['seriesType'] not in - ast.literal_eval(settings.sonarr.excluded_series_types)] - else: - dictsList_types_filtered = dictsList_monitored_filtered + typesList = ast.literal_eval(settings.sonarr.excluded_series_types) + for type in typesList: + where_clause += ' AND table_shows.seriesType != "' + type + '"' - return dictsList_types_filtered + return where_clause diff --git a/bazarr/get_episodes.py b/bazarr/get_episodes.py index 1e303a8f5..8e8bee97c 100644 --- a/bazarr/get_episodes.py +++ b/bazarr/get_episodes.py @@ -1,7 +1,7 @@ # coding=utf-8 import requests import logging -from database import database, dict_converter +from database import database, dict_converter, get_exclusion_clause from config import settings, url_sonarr from helper import path_mappings @@ -180,11 +180,13 @@ def sync_episodes(): if len(altered_episodes) <= 5: logging.debug("BAZARR No more than 5 episodes were added during this sync then we'll search for subtitles.") for altered_episode in altered_episodes: - if settings.sonarr.getboolean('only_monitored'): - if altered_episode[2] == 'True': - episode_download_subtitles(altered_episode[0]) - else: - episode_download_subtitles(altered_episode[0]) + data = database.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.monitored, table_shows.tags," + " table_shows.seriesType FROM table_episodes LEFT JOIN table_shows on " + "table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId WHERE " + "sonarrEpisodeId = ?" + get_exclusion_clause('series'), (altered_episode[0],), + only_one=True) + + episode_download_subtitles(data['sonarrEpisodeId']) else: logging.debug("BAZARR More than 5 episodes were added during this sync then we wont search for subtitles right now.") diff --git a/bazarr/get_movies.py b/bazarr/get_movies.py index ff3505795..3fcc8a849 100644 --- a/bazarr/get_movies.py +++ b/bazarr/get_movies.py @@ -10,7 +10,7 @@ from utils import get_radarr_version from list_subtitles import store_subtitles_movie, list_missing_subtitles_movies, movies_full_scan_subtitles from get_subtitle import movies_download_subtitles -from database import database, dict_converter +from database import database, dict_converter, get_exclusion_clause def update_all_movies(): @@ -265,11 +265,9 @@ def update_movies(): if len(altered_movies) <= 5: logging.debug("BAZARR No more than 5 movies were added during this sync then we'll search for subtitles.") for altered_movie in altered_movies: - if settings.radarr.getboolean('only_monitored'): - if altered_movie[3] == 'True': - movies_download_subtitles(altered_movie[2]) - else: - movies_download_subtitles(altered_movie[2]) + data = database.execute("SELECT * FROM table_movies WHERE radarrId = ?" + + get_exclusion_clause('movie'), (altered_movie[2],), only_one=True) + movies_download_subtitles(data['radarrId']) else: logging.debug("BAZARR More than 5 movies were added during this sync then we wont search for subtitles.") diff --git a/bazarr/get_subtitle.py b/bazarr/get_subtitle.py index 4ac1fc120..7ef841614 100644 --- a/bazarr/get_subtitle.py +++ b/bazarr/get_subtitle.py @@ -29,11 +29,10 @@ from notifier import send_notifications, send_notifications_movie from get_providers import get_providers, get_providers_auth, provider_throttle, provider_pool from knowit import api from subsyncer import subsync -from database import database, dict_mapper, filter_exclusions +from database import database, dict_mapper, get_exclusion_clause from analytics import track_event from locale import getpreferredencoding -import chardet def get_video(path, title, sceneName, providers=None, media_type="movie"): @@ -641,10 +640,10 @@ def manual_upload_subtitle(path, language, forced, title, scene_name, media_type def series_download_subtitles(no): episodes_details = database.execute("SELECT table_episodes.path, table_episodes.missing_subtitles, monitored, " "table_episodes.sonarrEpisodeId, table_episodes.scene_name, table_shows.tags, " - "table_shows.seriesType, table_episodes.audio_language FROM table_episodes INNER JOIN table_shows on " - "table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE " - "table_episodes.sonarrSeriesId=? and missing_subtitles!='[]'", (no,)) - episodes_details = filter_exclusions(episodes_details, 'series') + "table_shows.seriesType, table_episodes.audio_language FROM table_episodes " + "INNER JOIN table_shows on table_shows.sonarrSeriesId = " + "table_episodes.sonarrSeriesId WHERE table_episodes.sonarrSeriesId=? and " + "missing_subtitles!='[]'" + get_exclusion_clause('series'), (no,)) if not episodes_details: logging.debug("BAZARR no episode for that sonarrSeriesId can be found in database:", str(no)) return @@ -699,8 +698,8 @@ def episode_download_subtitles(no): "table_shows.hearing_impaired, table_shows.title, table_shows.sonarrSeriesId, " "table_shows.forced, table_episodes.audio_language, table_shows.seriesType FROM " "table_episodes LEFT JOIN table_shows on table_episodes.sonarrSeriesId = " - "table_shows.sonarrSeriesId WHERE sonarrEpisodeId=?", (no,)) - episodes_details = filter_exclusions(episodes_details, 'series') + "table_shows.sonarrSeriesId WHERE sonarrEpisodeId=?" + + get_exclusion_clause('series'), (no,)) if not episodes_details: logging.debug("BAZARR no episode with that sonarrEpisodeId can be found in database:", str(no)) return @@ -743,8 +742,7 @@ def episode_download_subtitles(no): def movies_download_subtitles(no): movies = database.execute( "SELECT path, missing_subtitles, audio_language, radarrId, sceneName, hearing_impaired, title, forced, tags, " - "monitored FROM table_movies WHERE radarrId=?", (no,)) - movies = filter_exclusions(movies, 'movie') + "monitored FROM table_movies WHERE radarrId=?" + get_exclusion_clause('movie'), (no,)) if not len(movies): logging.debug("BAZARR no movie with that radarrId can be found in database:", str(no)) return @@ -911,8 +909,7 @@ def wanted_search_missing_subtitles_series(): episodes = database.execute("SELECT table_episodes.path, table_shows.tags, table_episodes.monitored, " "table_shows.seriesType FROM table_episodes INNER JOIN table_shows on " "table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE missing_subtitles != " - "'[]'") - episodes = filter_exclusions(episodes, 'series') + "'[]'" + get_exclusion_clause('series')) # path_replace dict_mapper.path_replace(episodes) @@ -929,8 +926,8 @@ def wanted_search_missing_subtitles_series(): def wanted_search_missing_subtitles_movies(): - movies = database.execute("SELECT path, tags, monitored FROM table_movies WHERE missing_subtitles != '[]'") - movies = filter_exclusions(movies, 'movie') + movies = database.execute("SELECT path, tags, monitored FROM table_movies WHERE missing_subtitles != '[]'" + + get_exclusion_clause('movie')) # path_replace dict_mapper.path_replace_movie(movies) @@ -1079,11 +1076,10 @@ def upgrade_subtitles(): "table_shows.sonarrSeriesId = table_history.sonarrSeriesId INNER JOIN " "table_episodes on table_episodes.sonarrEpisodeId = " "table_history.sonarrEpisodeId WHERE action IN " - "(" + ','.join(map(str, query_actions)) + - ") AND timestamp > ? AND score is not null GROUP BY " + "(" + ','.join(map(str, query_actions)) + ") AND timestamp > ? AND score" + " is not null" + get_exclusion_clause('series') + " GROUP BY " "table_history.video_path, table_history.language", (minimum_timestamp,)) - upgradable_episodes = filter_exclusions(upgradable_episodes, 'series') upgradable_episodes_not_perfect = [] for upgradable_episode in upgradable_episodes: if upgradable_episode['timestamp'] > minimum_timestamp: @@ -1111,9 +1107,9 @@ def upgrade_subtitles(): "table_movies.monitored FROM table_history_movie INNER JOIN table_movies " "on table_movies.radarrId = table_history_movie.radarrId WHERE action IN " "(" + ','.join(map(str, query_actions)) + ") AND timestamp > ? AND score " - "is not null GROUP BY table_history_movie.video_path, " - "table_history_movie.language", (minimum_timestamp,)) - upgradable_movies = filter_exclusions(upgradable_movies, 'movie') + "is not null" + get_exclusion_clause('movie') + " GROUP BY " + "table_history_movie.video_path, table_history_movie.language", + (minimum_timestamp,)) upgradable_movies_not_perfect = [] for upgradable_movie in upgradable_movies: if upgradable_movie['timestamp'] > minimum_timestamp: