From daa6d87e7639bdf79795133680b205ea890ae195 Mon Sep 17 00:00:00 2001 From: Halali Date: Tue, 11 Jun 2019 20:45:48 +0200 Subject: [PATCH] Reformat code --- bazarr.py | 17 +- bazarr/get_args.py | 4 +- bazarr/get_languages.py | 4 +- bazarr/get_subtitle.py | 151 +++++++++------- bazarr/helper.py | 9 +- bazarr/init.py | 14 +- bazarr/libs.py | 2 +- bazarr/list_subtitles.py | 33 ++-- bazarr/logger.py | 3 +- bazarr/main.py | 368 ++++++++++++++++++++------------------- bazarr/notifier.py | 34 ++-- bazarr/queueconfig.py | 12 +- bazarr/scheduler.py | 18 +- bazarr/update_db.py | 18 +- bazarr/utils.py | 41 +++-- 15 files changed, 381 insertions(+), 347 deletions(-) diff --git a/bazarr.py b/bazarr.py index a6e7740e7..5e559fe03 100644 --- a/bazarr.py +++ b/bazarr.py @@ -1,7 +1,6 @@ # coding=utf-8 import subprocess as sp -import threading import time import os import sys @@ -14,7 +13,7 @@ dir_name = os.path.dirname(__file__) def start_bazarr(): script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:] - + ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE) print "Bazarr starting..." try: @@ -27,17 +26,18 @@ def start_bazarr(): if __name__ == '__main__': restartfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.restart')) stopfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.stop')) - + try: os.remove(restartfile) except: pass - + try: os.remove(stopfile) except: pass - + + def daemon(): if os.path.exists(stopfile): try: @@ -47,7 +47,7 @@ if __name__ == '__main__': else: print 'Bazarr exited.' os._exit(0) - + if os.path.exists(restartfile): try: os.remove(restartfile) @@ -55,9 +55,10 @@ if __name__ == '__main__': print 'Unable to delete restart file.' else: start_bazarr() - + + start_bazarr() - + # Keep the script running forever. while True: daemon() diff --git a/bazarr/get_args.py b/bazarr/get_args.py index 2f4bec9fe..a92052729 100644 --- a/bazarr/get_args.py +++ b/bazarr/get_args.py @@ -9,7 +9,7 @@ parser = argparse.ArgumentParser() def get_args(): parser.register('type', bool, strtobool) - + config_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'data')) parser.add_argument('-c', '--config', default=config_dir, type=str, metavar="DIR", dest="config_dir", help="Directory containing the configuration (default: %s)" % config_dir) @@ -21,7 +21,7 @@ def get_args(): help="Enable console debugging (default: False)") parser.add_argument('--release-update', default=False, type=bool, const=True, metavar="BOOL", nargs="?", help="Enable file based updater (default: False)") - + return parser.parse_args() diff --git a/bazarr/get_languages.py b/bazarr/get_languages.py index 6d8b96182..f5e83a7cd 100644 --- a/bazarr/get_languages.py +++ b/bazarr/get_languages.py @@ -115,13 +115,13 @@ def get_language_set(): languages = c.execute('''SELECT code3 FROM table_settings_languages WHERE enabled = 1''').fetchall() db.close() language_set = set() - + for lang in languages: if lang[0] == 'pob': language_set.add(Language('por', 'BR')) else: language_set.add(Language(lang[0])) - + return language_set diff --git a/bazarr/get_subtitle.py b/bazarr/get_subtitle.py index f84ef3335..0d4b834e8 100644 --- a/bazarr/get_subtitle.py +++ b/bazarr/get_subtitle.py @@ -65,7 +65,7 @@ def get_video(path, title, sceneName, use_scenename, providers=None, media_type= video.used_scene_name = dont_use_actual_file video.original_name = original_name video.original_path = original_path - refine_from_db(original_path,video) + refine_from_db(original_path, video) logging.debug('BAZARR is using those video object properties: %s', vars(video)) return video @@ -100,25 +100,26 @@ def get_scores(video, media_type, min_score_movie_perc=60 * 100 / 120.0, min_sco return min_score, max_score, set(scores) -def download_subtitle(path, language, hi, forced, providers, providers_auth, sceneName, title, media_type, forced_minimum_score=None, is_upgrade=False): +def download_subtitle(path, language, hi, forced, providers, providers_auth, sceneName, title, media_type, + forced_minimum_score=None, is_upgrade=False): # fixme: supply all missing languages, not only one, to hit providers only once who support multiple languages in # one query - + if settings.general.getboolean('utf8_encode'): os.environ["SZ_KEEP_ENCODING"] = "" else: os.environ["SZ_KEEP_ENCODING"] = True - + logging.debug('BAZARR Searching subtitles for this file: ' + path) if hi == "True": hi = "force HI" else: hi = "force non-HI" language_set = set() - + if not isinstance(language, types.ListType): language = [language] - + if forced == "True": providers_auth['podnapisi']['only_foreign'] = True providers_auth['subscene']['only_foreign'] = True @@ -127,7 +128,7 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce providers_auth['podnapisi']['only_foreign'] = False providers_auth['subscene']['only_foreign'] = False providers_auth['opensubtitles']['only_foreign'] = False - + for l in language: if l == 'pob': lang_obj = Language('por', 'BR') @@ -160,7 +161,7 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce if video: min_score, max_score, scores = get_scores(video, media_type, min_score_movie_perc=int(minimum_score_movie), min_score_series_perc=int(minimum_score)) - + if providers: if forced_minimum_score: min_score = int(forced_minimum_score) + 1 @@ -187,7 +188,8 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce try: fld = get_target_folder(path) - chmod = int(settings.general.chmod, 8) if not sys.platform.startswith('win') and settings.general.getboolean('chmod_enabled') else None + chmod = int(settings.general.chmod, 8) if not sys.platform.startswith( + 'win') and settings.general.getboolean('chmod_enabled') else None saved_subtitles = save_subtitles(video.original_path, subtitles, single=single, tags=None, # fixme directory=fld, @@ -224,7 +226,8 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, - downloaded_language_code2, downloaded_language_code3, subtitle.language.forced) + downloaded_language_code2, downloaded_language_code3, + subtitle.language.forced) try: if os.name == 'nt': codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, @@ -259,7 +262,7 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce reversed_path = path_replace_reverse(path) else: reversed_path = path_replace_reverse_movie(path) - + return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score, subtitle.language.forced if not saved_any: @@ -270,7 +273,7 @@ def download_subtitle(path, language, hi, forced, providers, providers_auth, sce def manual_search(path, language, hi, forced, providers, providers_auth, sceneName, title, media_type): logging.debug('BAZARR Manually searching subtitles for this file: ' + path) - + final_subtitles = [] if hi == "True": @@ -278,7 +281,7 @@ def manual_search(path, language, hi, forced, providers, providers_auth, sceneNa else: hi = "force non-HI" language_set = set() - + if forced == "True": providers_auth['podnapisi']['only_foreign'] = True providers_auth['subscene']['only_foreign'] = True @@ -287,7 +290,7 @@ def manual_search(path, language, hi, forced, providers, providers_auth, sceneNa providers_auth['podnapisi']['only_foreign'] = False providers_auth['subscene']['only_foreign'] = False providers_auth['opensubtitles']['only_foreign'] = False - + for lang in ast.literal_eval(language): lang = alpha3_from_alpha2(lang) if lang == 'pob': @@ -341,7 +344,7 @@ def manual_search(path, language, hi, forced, providers, providers_auth, sceneNa if can_verify_series and not {"series", "season", "episode"}.issubset(matches): logging.debug(u"BAZARR Skipping %s, because it doesn't match our series/episode", s) continue - + score = compute_score(matches, s, video, hearing_impaired=hi) not_matched = scores - matches s.score = score @@ -359,14 +362,15 @@ def manual_search(path, language, hi, forced, providers, providers_auth, sceneNa return final_subtitles -def manual_download_subtitle(path, language, hi, forced, subtitle, provider, providers_auth, sceneName, title, media_type): +def manual_download_subtitle(path, language, hi, forced, subtitle, provider, providers_auth, sceneName, title, + media_type): logging.debug('BAZARR Manually downloading subtitles for this file: ' + path) - + if settings.general.getboolean('utf8_encode'): os.environ["SZ_KEEP_ENCODING"] = "" else: os.environ["SZ_KEEP_ENCODING"] = True - + subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64")) use_scenename = settings.general.getboolean('use_scenename') use_postprocessing = settings.general.getboolean('use_postprocessing') @@ -394,7 +398,8 @@ def manual_download_subtitle(path, language, hi, forced, subtitle, provider, pro try: score = round(subtitle.score / max_score * 100, 2) fld = get_target_folder(path) - chmod = int(settings.general.chmod, 8) if not sys.platform.startswith('win') and settings.general.getboolean('chmod_enabled') else None + chmod = int(settings.general.chmod, 8) if not sys.platform.startswith( + 'win') and settings.general.getboolean('chmod_enabled') else None saved_subtitles = save_subtitles(video.original_path, [subtitle], single=single, tags=None, # fixme directory=fld, @@ -423,7 +428,8 @@ def manual_download_subtitle(path, language, hi, forced, subtitle, provider, pro if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, - downloaded_language_code2, downloaded_language_code3, subtitle.language.forced) + downloaded_language_code2, downloaded_language_code3, + subtitle.language.forced) try: if os.name == 'nt': codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, @@ -452,12 +458,12 @@ def manual_download_subtitle(path, language, hi, forced, subtitle, provider, pro 'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.info('BAZARR Post-processing result for file ' + path + ' : ' + out) - + if media_type == 'series': reversed_path = path_replace_reverse(path) else: reversed_path = path_replace_reverse_movie(path) - + return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score, subtitle.language.forced else: logging.error( @@ -486,14 +492,15 @@ def series_download_subtitles(no): providers_auth = get_providers_auth() count_episodes_details = len(episodes_details) - + for i, episode in enumerate(episodes_details, 1): for language in ast.literal_eval(episode[1]): if language is not None: - notifications.write(msg='Searching for series subtitles...', queue='get_subtitle', item=i, length=count_episodes_details) + notifications.write(msg='Searching for series subtitles...', queue='get_subtitle', item=i, + length=count_episodes_details) result = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), - series_details[0], series_details[2], providers_list, - providers_auth, str(episode[3]), series_details[1], 'series') + series_details[0], series_details[2], providers_list, + providers_auth, str(episode[3]), series_details[1], 'series') if result is not None: message = result[0] path = result[1] @@ -505,9 +512,10 @@ def series_download_subtitles(no): history_log(1, no, episode[2], message, path, language_code, provider, score) send_notifications(no, episode[2], message) list_missing_subtitles(no) - + if count_episodes_details: - notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', button='refresh', queue='get_subtitle') + notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', + button='refresh', queue='get_subtitle') def episode_download_subtitles(no): @@ -515,16 +523,17 @@ def episode_download_subtitles(no): monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" - + conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() episodes_details = c_db.execute( - 'SELECT table_episodes.path, table_episodes.missing_subtitles, table_episodes.sonarrEpisodeId, table_episodes.scene_name, table_shows.hearing_impaired, table_shows.title, table_shows.sonarrSeriesId, table_shows.forced FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.sonarrEpisodeId = ?' + monitored_only_query_string, (no,)).fetchall() + 'SELECT table_episodes.path, table_episodes.missing_subtitles, table_episodes.sonarrEpisodeId, table_episodes.scene_name, table_shows.hearing_impaired, table_shows.title, table_shows.sonarrSeriesId, table_shows.forced FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.sonarrEpisodeId = ?' + monitored_only_query_string, + (no,)).fetchall() c_db.close() - + providers_list = get_providers() providers_auth = get_providers_auth() - + for episode in episodes_details: for language in ast.literal_eval(episode[1]): if language is not None: @@ -544,7 +553,7 @@ def episode_download_subtitles(no): store_subtitles(path_replace(episode[0])) history_log(1, episode[6], episode[2], message, path, language_code, provider, score) send_notifications(episode[6], episode[2], message) - + list_missing_subtitles(episode[6]) @@ -555,17 +564,17 @@ def movies_download_subtitles(no): "SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired, title, forced FROM table_movies WHERE radarrId = ?", (no,)).fetchone() c_db.close() - + providers_list = get_providers() providers_auth = get_providers_auth() count_movie = len(ast.literal_eval(movie[1])) - + for i, language in enumerate(ast.literal_eval(movie[1]), 1): if language is not None: notifications.write(msg='Searching for movies subtitles', queue='get_subtitle', item=i, length=count_movie) result = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], - movie[6], providers_list, providers_auth, str(movie[3]), movie[5], 'movie') + movie[6], providers_list, providers_auth, str(movie[3]), movie[5], 'movie') if result is not None: message = result[0] path = result[1] @@ -577,9 +586,10 @@ def movies_download_subtitles(no): history_log_movie(1, no, message, path, language_code, provider, score) send_notifications_movie(no, message) list_missing_subtitles_movies(no) - + if count_movie: - notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', button='refresh', queue='get_subtitle') + notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', + button='refresh', queue='get_subtitle') def wanted_download_subtitles(path, l, count_episodes): @@ -617,10 +627,11 @@ def wanted_download_subtitles(path, l, count_episodes): if attempt[i][0] == language: language = language.split(':')[0] if search_active(attempt[i][1]): - notifications.write(msg='Searching for series subtitles...', queue='get_subtitle', item=l, length=count_episodes) + notifications.write(msg='Searching for series subtitles...', queue='get_subtitle', item=l, + length=count_episodes) result = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), - episode[4], episode[8], providers_list, providers_auth, - str(episode[5]), episode[7], 'series') + episode[4], episode[8], providers_list, providers_auth, + str(episode[5]), episode[7], 'series') if result is not None: message = result[0] path = result[1] @@ -671,10 +682,11 @@ def wanted_download_subtitles_movie(path, l, count_movies): if attempt[i][0] == language: language = language.split(':')[0] if search_active(attempt[i][1]) is True: - notifications.write(msg='Searching for movies subtitles...', queue='get_subtitle', item=l, length=count_movies) + notifications.write(msg='Searching for movies subtitles...', queue='get_subtitle', item=l, + length=count_movies) result = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), - movie[4], movie[8], providers_list, providers_auth, str(movie[5]), - movie[7], 'movie') + movie[4], movie[8], providers_list, providers_auth, str(movie[5]), + movie[7], 'movie') if result is not None: message = result[0] path = result[1] @@ -696,7 +708,7 @@ def wanted_search_missing_subtitles(): db.create_function("path_substitution", 1, path_replace) db.create_function("path_substitution_movie", 1, path_replace_movie) c = db.cursor() - + if settings.sonarr.getboolean('only_monitored'): monitored_only_query_string_sonarr = ' AND monitored = "True"' else: @@ -706,7 +718,7 @@ def wanted_search_missing_subtitles(): monitored_only_query_string_radarr = ' AND monitored = "True"' else: monitored_only_query_string_radarr = "" - + c.execute( "SELECT path_substitution(path) FROM table_episodes WHERE missing_subtitles != '[]'" + monitored_only_query_string_sonarr) episodes = c.fetchall() @@ -719,7 +731,7 @@ def wanted_search_missing_subtitles(): providers = get_providers() if settings.general.getboolean('use_sonarr'): if providers: - count_episodes= len(episodes) + count_episodes = len(episodes) for i, episode in enumerate(episodes, 1): wanted_download_subtitles(episode[0], i, count_episodes) else: @@ -736,8 +748,9 @@ def wanted_search_missing_subtitles(): logging.info("BAZARR All providers are throttled") logging.info('BAZARR Finished searching for missing subtitles. Check histories for more information.') - - notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', button='refresh', queue='get_subtitle') + + notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', + button='refresh', queue='get_subtitle') def search_active(timestamp): @@ -763,7 +776,9 @@ def refine_from_db(path, video): if isinstance(video, Episode): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - data = c.execute("SELECT table_shows.title, table_episodes.season, table_episodes.episode, table_episodes.title, table_shows.year, table_shows.tvdbId, table_shows.alternateTitles, table_episodes.format, table_episodes.resolution, table_episodes.video_codec, table_episodes.audio_codec FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ?", (unicode(path_replace_reverse(path)),)).fetchone() + data = c.execute( + "SELECT table_shows.title, table_episodes.season, table_episodes.episode, table_episodes.title, table_shows.year, table_shows.tvdbId, table_shows.alternateTitles, table_episodes.format, table_episodes.resolution, table_episodes.video_codec, table_episodes.audio_codec FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ?", + (unicode(path_replace_reverse(path)),)).fetchone() db.close() if data: video.series, year, country = series_re.match(data[0]).groups() @@ -785,10 +800,12 @@ def refine_from_db(path, video): elif isinstance(video, Movie): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - data = c.execute("SELECT title, year, alternativeTitles, format, resolution, video_codec, audio_codec, imdbId FROM table_movies WHERE path = ?", (unicode(path_replace_reverse_movie(path)),)).fetchone() + data = c.execute( + "SELECT title, year, alternativeTitles, format, resolution, video_codec, audio_codec, imdbId FROM table_movies WHERE path = ?", + (unicode(path_replace_reverse_movie(path)),)).fetchone() db.close() if data: - video.title = re.sub(r'(\(\d\d\d\d\))' , '', data[0]) + video.title = re.sub(r'(\(\d\d\d\d\))', '', data[0]) if data[1]: if int(data[1]) > 0: video.year = int(data[1]) if data[7]: video.imdb_id = data[7] @@ -801,7 +818,7 @@ def refine_from_db(path, video): if data[5]: video.video_codec = data[5] if not video.audio_codec: if data[6]: video.audio_codec = data[6] - + return video @@ -809,12 +826,12 @@ def upgrade_subtitles(): days_to_upgrade_subs = settings.general.days_to_upgrade_subs minimum_timestamp = ((datetime.now() - timedelta(days=int(days_to_upgrade_subs))) - datetime(1970, 1, 1)).total_seconds() - + if settings.general.getboolean('upgrade_manual'): query_actions = [1, 2, 3] else: query_actions = [1, 3] - + db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() episodes_list = c.execute("""SELECT table_history.video_path, table_history.language, table_history.score, @@ -839,25 +856,25 @@ def upgrade_subtitles(): GROUP BY table_history_movie.video_path, table_history_movie.language""", (minimum_timestamp,)).fetchall() db.close() - + episodes_to_upgrade = [] if settings.general.getboolean('use_sonarr'): for episode in episodes_list: if os.path.exists(path_replace(episode[0])) and int(episode[2]) < 357: episodes_to_upgrade.append(episode) - + movies_to_upgrade = [] if settings.general.getboolean('use_radarr'): for movie in movies_list: if os.path.exists(path_replace_movie(movie[0])) and int(movie[2]) < 117: movies_to_upgrade.append(movie) - + providers_list = get_providers() providers_auth = get_providers_auth() count_episode_to_upgrade = len(episodes_to_upgrade) count_movie_to_upgrade = len(movies_to_upgrade) - + if settings.general.getboolean('use_sonarr'): for i, episode in enumerate(episodes_to_upgrade, 1): if episode[9] != "None": @@ -868,18 +885,18 @@ def upgrade_subtitles(): forced_languages = [l + ":forced" for l in desired_languages] + desired_languages else: forced_languages = desired_languages - + if episode[1] in forced_languages: notifications.write(msg='Upgrading series subtitles...', queue='upgrade_subtitle', item=i, length=count_episode_to_upgrade) - + if episode[1].endswith('forced'): language = episode[1].split(':')[0] is_forced = "True" else: language = episode[1] is_forced = "False" - + result = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), episode[3], is_forced, providers_list, providers_auth, str(episode[4]), episode[5], 'series', forced_minimum_score=int(episode[2]), @@ -894,7 +911,7 @@ def upgrade_subtitles(): store_subtitles(path_replace(episode[0])) history_log(3, episode[6], episode[7], message, path, language_code, provider, score) send_notifications(episode[6], episode[7], message) - + if settings.general.getboolean('use_radarr'): for i, movie in enumerate(movies_to_upgrade, 1): if movie[8] != "None": @@ -905,18 +922,18 @@ def upgrade_subtitles(): forced_languages = [l + ":forced" for l in desired_languages] + desired_languages else: forced_languages = desired_languages - + if movie[1] in forced_languages: notifications.write(msg='Upgrading movie subtitles...', - queue='upgrade_subtitle', item=i, length=count_movie_to_upgrade) - + queue='upgrade_subtitle', item=i, length=count_movie_to_upgrade) + if movie[1].endswith('forced'): language = movie[1].split(':')[0] is_forced = "True" else: language = movie[1] is_forced = "False" - + result = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[3], is_forced, providers_list, providers_auth, str(movie[4]), movie[5], 'movie', forced_minimum_score=int(movie[2]), is_upgrade=True) diff --git a/bazarr/helper.py b/bazarr/helper.py index 3aff2dec3..5a0c55e6c 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -4,7 +4,6 @@ import os import re import types import logging -import sqlite3 import chardet from bs4 import UnicodeDammit @@ -85,7 +84,7 @@ def get_target_folder(file_path): if subfolder != "current" and fld_custom: # specific subFolder requested, create it if it doesn't exist fld_base = os.path.split(file_path)[0] - + if subfolder == "absolute": # absolute folder fld = fld_custom @@ -93,9 +92,9 @@ def get_target_folder(file_path): fld = os.path.join(fld_base, fld_custom) else: fld = None - + fld = force_unicode(fld) - + if not os.path.isdir(fld): try: os.makedirs(fld) @@ -104,7 +103,7 @@ def get_target_folder(file_path): fld = None else: fld = None - + return fld diff --git a/bazarr/init.py b/bazarr/init.py index b53755bee..b020e48e0 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -22,7 +22,8 @@ if settings.general.anti_captcha_provider == 'anti-captcha' and settings.anticap os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = settings.anticaptcha.anti_captcha_key elif settings.general.anti_captcha_provider == 'death-by-captcha' and settings.deathbycaptcha.username != "" and settings.deathbycaptcha.password != "": os.environ["ANTICAPTCHA_CLASS"] = 'DeathByCaptchaProxyLess' - os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = ':'.join({settings.deathbycaptcha.username, settings.deathbycaptcha.password}) + os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = ':'.join( + {settings.deathbycaptcha.username, settings.deathbycaptcha.password}) else: os.environ["ANTICAPTCHA_CLASS"] = '' @@ -123,7 +124,7 @@ try: providers_list.append(provider[0]) else: providers_list = None - + if settings_providers: for provider in settings_providers: if provider[0] == 'opensubtitles': @@ -135,7 +136,7 @@ try: elif provider[0] == 'legendastv': settings.legendastv.username = provider[2] settings.legendastv.password = provider[3] - + settings.general.enabled_providers = u'' if not providers_list else ','.join(providers_list) with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) @@ -167,20 +168,21 @@ if not os.path.exists(os.path.normpath(os.path.join(args.config_dir, 'config', ' def init_binaries(): exe = get_binary("unrar") - + rarfile.UNRAR_TOOL = exe rarfile.ORIG_UNRAR_TOOL = exe try: rarfile.custom_check([rarfile.UNRAR_TOOL], True) except: logging.debug("custom check failed for: %s", exe) - + rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS logging.info("Using UnRAR from: %s", exe) unrar = exe - + return unrar + init_binaries() diff --git a/bazarr/libs.py b/bazarr/libs.py index 6c2a92c2c..37b8809ac 100644 --- a/bazarr/libs.py +++ b/bazarr/libs.py @@ -1,7 +1,7 @@ # coding=utf-8 -import sys import os +import sys def set_libs(): diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index 71b709db6..c14e8712d 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -47,7 +47,8 @@ def store_subtitles(file): logging.debug("BAZARR unable to index this unrecognized language: " + subtitle_language) pass except Exception as e: - logging.exception("BAZARR error when trying to analyze this %s file: %s" % (os.path.splitext(file)[1], file)) + logging.exception( + "BAZARR error when trying to analyze this %s file: %s" % (os.path.splitext(file)[1], file)) pass brazilian_portuguese = [".pt-br", ".pob", "pb"] @@ -71,7 +72,7 @@ def store_subtitles(file): logging.debug("BAZARR external subtitles detected: " + "pb:forced") actual_subtitles.append( [str("pb:forced"), path_replace_reverse(subtitle_path)]) - + elif str(language) != 'und': logging.debug("BAZARR external subtitles detected: " + str(language)) actual_subtitles.append( @@ -136,9 +137,10 @@ def store_subtitles_movie(file): logging.debug("BAZARR unable to index this unrecognized language: " + subtitle_language) pass except Exception as e: - logging.exception("BAZARR error when trying to analyze this %s file: %s" % (os.path.splitext(file)[1], file)) + logging.exception( + "BAZARR error when trying to analyze this %s file: %s" % (os.path.splitext(file)[1], file)) pass - + dest_folder = get_subtitle_destination_folder() or '' subliminal_patch.core.CUSTOM_PATHS = [dest_folder] if dest_folder else [] brazilian_portuguese = [".pt-br", ".pob", "pb"] @@ -154,19 +156,23 @@ def store_subtitles_movie(file): if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: logging.debug("BAZARR external subtitles detected: " + "pb") actual_subtitles.append( - [str("pb"), path_replace_reverse_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle))]) + [str("pb"), + path_replace_reverse_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle))]) elif str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese_forced)) is True: logging.debug("BAZARR external subtitles detected: " + "pb:forced") actual_subtitles.append( - [str("pb:forced"), path_replace_reverse_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle))]) + [str("pb:forced"), + path_replace_reverse_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle))]) elif str(language) != 'und': logging.debug("BAZARR external subtitles detected: " + str(language)) actual_subtitles.append( - [str(language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle))]) + [str(language), + path_replace_reverse_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle))]) else: if os.path.splitext(subtitle)[1] != ".sub": logging.debug("BAZARR falling back to file content analysis to detect language.") - with open(path_replace_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle)), 'r') as f: + with open(path_replace_movie(os.path.join(os.path.dirname(file), dest_folder, subtitle)), + 'r') as f: text = list(islice(f, 100)) text = ' '.join(text) encoding = UnicodeDammit(text) @@ -270,7 +276,8 @@ def list_missing_subtitles_movies(*no): pass conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() - movies_subtitles = c_db.execute("SELECT radarrId, subtitles, languages, forced FROM table_movies" + query_string).fetchall() + movies_subtitles = c_db.execute( + "SELECT radarrId, subtitles, languages, forced FROM table_movies" + query_string).fetchall() c_db.close() missing_subtitles_global = [] @@ -326,7 +333,7 @@ def series_full_scan_subtitles(): episodes = c_db.execute("SELECT path FROM table_episodes").fetchall() c_db.close() count_episodes = len(episodes) - + for i, episode in enumerate(episodes, 1): notifications.write(msg='Updating all episodes subtitles from disk...', queue='list_subtitles_series', item=i, length=count_episodes) @@ -341,7 +348,7 @@ def movies_full_scan_subtitles(): movies = c_db.execute("SELECT path FROM table_movies").fetchall() c_db.close() count_movies = len(movies) - + for i, movie in enumerate(movies, 1): notifications.write(msg='Updating all movies subtitles from disk...', queue='list_subtitles_movies', item=i, length=count_movies) @@ -376,7 +383,7 @@ def movies_scan_subtitles(no): def get_external_subtitles_path(file, subtitle): fld = os.path.dirname(file) - + if settings.general.subfolder == "current": path = os.path.join(fld, subtitle) elif settings.general.subfolder == "absolute": @@ -397,5 +404,5 @@ def get_external_subtitles_path(file, subtitle): path = None else: path = None - + return path diff --git a/bazarr/logger.py b/bazarr/logger.py index e95d2259f..1315947f9 100644 --- a/bazarr/logger.py +++ b/bazarr/logger.py @@ -86,13 +86,12 @@ def configure_logging(debug=False): logging.getLogger("subliminal").setLevel(logging.CRITICAL) logging.getLogger("subliminal_patch").setLevel(logging.CRITICAL) logging.getLogger("subzero").setLevel(logging.ERROR) - + logging.getLogger("enzyme").setLevel(logging.CRITICAL) logging.getLogger("guessit").setLevel(logging.WARNING) logging.getLogger("rebulk").setLevel(logging.WARNING) logging.getLogger("stevedore.extension").setLevel(logging.CRITICAL) logging.getLogger("geventwebsocket.handler").setLevel(logging.WARNING) - class MyFilter(logging.Filter): diff --git a/bazarr/main.py b/bazarr/main.py index d8475bd1e..c66b92e1b 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -17,7 +17,6 @@ import warnings import queueconfig import platform import apprise -import re from get_args import args from init import * @@ -31,10 +30,12 @@ try: import gevent except ImportError: import logging + logging.exception('BAZARR require gevent Python module to be installed using pip.') try: import os from get_args import args + stop_file = open(os.path.join(args.config_dir, "bazarr.stop"), "w") except Exception as e: logging.error('BAZARR Cannot create bazarr.stop file.') @@ -43,7 +44,6 @@ except ImportError: stop_file.close() os._exit(0) - from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler @@ -51,8 +51,8 @@ from io import BytesIO from six import text_type from beaker.middleware import SessionMiddleware from cork import Cork -from bottle import route, run, template, static_file, request, redirect, response, HTTPError, app, hook, abort -from datetime import datetime, timedelta +from bottle import route, template, static_file, request, redirect, response, HTTPError, app, hook +from datetime import timedelta from get_languages import load_language_in_db, language_from_alpha3 from get_providers import get_providers, get_providers_auth, list_throttled_providers from get_series import * @@ -61,12 +61,12 @@ from get_episodes import * from list_subtitles import store_subtitles, store_subtitles_movie, series_scan_subtitles, movies_scan_subtitles, \ list_missing_subtitles, list_missing_subtitles_movies from get_subtitle import download_subtitle, series_download_subtitles, movies_download_subtitles, \ - wanted_download_subtitles, wanted_search_missing_subtitles, manual_search, manual_download_subtitle, upgrade_subtitles + manual_search, manual_download_subtitle from utils import history_log, history_log_movie from scheduler import * from notifier import send_notifications, send_notifications_movie from config import settings, url_sonarr, url_radarr, url_radarr_short, url_sonarr_short, base_url -from helper import path_replace_movie, get_subtitle_destination_folder +from helper import path_replace_movie from subliminal_patch.extensions import provider_registry as provider_manager reload(sys) @@ -137,9 +137,9 @@ def custom_auth_basic(check): return func(*a, **ka) else: return func(*a, **ka) - + return wrapper - + return decorator @@ -292,7 +292,7 @@ def save_wizard(): settings_upgrade_manual = 'False' else: settings_upgrade_manual = 'True' - + settings.general.ip = text_type(settings_general_ip) settings.general.port = text_type(settings_general_port) settings.general.base_url = text_type(settings_general_baseurl) @@ -307,7 +307,7 @@ def save_wizard(): settings.general.upgrade_subs = text_type(settings_upgrade_subs) settings.general.days_to_upgrade_subs = text_type(settings_days_to_upgrade_subs) settings.general.upgrade_manual = text_type(settings_upgrade_manual) - + settings_sonarr_ip = request.forms.get('settings_sonarr_ip') settings_sonarr_port = request.forms.get('settings_sonarr_port') settings_sonarr_baseurl = request.forms.get('settings_sonarr_baseurl') @@ -355,31 +355,31 @@ def save_wizard(): settings_subliminal_providers = request.forms.getall('settings_subliminal_providers') settings.general.enabled_providers = u'' if not settings_subliminal_providers else ','.join( settings_subliminal_providers) - + settings_addic7ed_random_agents = request.forms.get('settings_addic7ed_random_agents') if settings_addic7ed_random_agents is None: settings_addic7ed_random_agents = 'False' else: settings_addic7ed_random_agents = 'True' - + settings_opensubtitles_vip = request.forms.get('settings_opensubtitles_vip') if settings_opensubtitles_vip is None: settings_opensubtitles_vip = 'False' else: settings_opensubtitles_vip = 'True' - + settings_opensubtitles_ssl = request.forms.get('settings_opensubtitles_ssl') if settings_opensubtitles_ssl is None: settings_opensubtitles_ssl = 'False' else: settings_opensubtitles_ssl = 'True' - + settings_opensubtitles_skip_wrong_fps = request.forms.get('settings_opensubtitles_skip_wrong_fps') if settings_opensubtitles_skip_wrong_fps is None: settings_opensubtitles_skip_wrong_fps = 'False' else: settings_opensubtitles_skip_wrong_fps = 'True' - + settings.addic7ed.username = request.forms.get('settings_addic7ed_username') settings.addic7ed.password = request.forms.get('settings_addic7ed_password') settings.addic7ed.random_agents = text_type(settings_addic7ed_random_agents) @@ -438,13 +438,13 @@ def save_wizard(): else: settings_movie_default_hi = 'True' settings.general.movie_default_hi = text_type(settings_movie_default_hi) - + settings_movie_default_forced = str(request.forms.getall('settings_movie_default_forced')) settings.general.movie_default_forced = text_type(settings_movie_default_forced) - + with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) - + conn.commit() c.close() @@ -463,10 +463,10 @@ def static(path): def emptylog(): authorize() ref = request.environ['HTTP_REFERER'] - + empty_log() logging.info('BAZARR Log file emptied') - + redirect(ref) @@ -580,15 +580,15 @@ def series(): def serieseditor(): authorize() single_language = settings.general.getboolean('single_language') - + db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace) c = db.cursor() - + c.execute("SELECT COUNT(*) FROM table_shows") missing_count = c.fetchone() missing_count = missing_count[0] - + c.execute( "SELECT tvdbId, title, path_substitution(path), languages, hearing_impaired, sonarrSeriesId, poster, audio_language, forced FROM table_shows ORDER BY title ASC") data = c.fetchall() @@ -614,13 +614,16 @@ def search_json(query): ('%' + query + '%',)) series = c.fetchall() for serie in series: - search_list.append(dict([('name', re.sub(r'\ \(\d{4}\)', '', serie[0]) + ' (' + serie[2] + ')'), ('url', base_url + 'episodes/' + str(serie[1]))])) + search_list.append(dict([('name', re.sub(r'\ \(\d{4}\)', '', serie[0]) + ' (' + serie[2] + ')'), + ('url', base_url + 'episodes/' + str(serie[1]))])) if settings.general.getboolean('use_radarr'): - c.execute("SELECT title, radarrId, year FROM table_movies WHERE title LIKE ? ORDER BY title", ('%' + query + '%',)) + c.execute("SELECT title, radarrId, year FROM table_movies WHERE title LIKE ? ORDER BY title", + ('%' + query + '%',)) movies = c.fetchall() for movie in movies: - search_list.append(dict([('name', re.sub(r'\ \(\d{4}\)', '', movie[0]) + ' (' + movie[2] + ')'), ('url', base_url + 'movie/' + str(movie[1]))])) + search_list.append(dict([('name', re.sub(r'\ \(\d{4}\)', '', movie[0]) + ' (' + movie[2] + ')'), + ('url', base_url + 'movie/' + str(movie[1]))])) c.close() response.content_type = 'application/json' @@ -632,13 +635,13 @@ def search_json(query): def edit_series(no): authorize() ref = request.environ['HTTP_REFERER'] - + lang = request.forms.getall('languages') if len(lang) > 0: pass else: lang = 'None' - + single_language = settings.general.getboolean('single_language') if single_language: if str(lang) == "['None']": @@ -648,24 +651,24 @@ def edit_series(no): else: if str(lang) == "['']": lang = '[]' - + hi = request.forms.get('hearing_impaired') forced = request.forms.get('forced') - + if hi == "on": hi = "True" else: hi = "False" - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() c.execute("UPDATE table_shows SET languages = ?, hearing_impaired = ?, forced = ? WHERE sonarrSeriesId LIKE ?", (str(lang), hi, forced, no)) conn.commit() c.close() - + list_missing_subtitles(no) - + redirect(ref) @@ -674,16 +677,16 @@ def edit_series(no): def edit_serieseditor(): authorize() ref = request.environ['HTTP_REFERER'] - + series = request.forms.get('series') series = ast.literal_eval(str('[' + series + ']')) lang = request.forms.getall('languages') hi = request.forms.get('hearing_impaired') forced = request.forms.get('forced') - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() - + for serie in series: if str(lang) != "[]" and str(lang) != "['']": if str(lang) == "['None']": @@ -695,13 +698,13 @@ def edit_serieseditor(): c.execute("UPDATE table_shows SET hearing_impaired = ? WHERE sonarrSeriesId LIKE ?", (hi, serie)) if forced != '': c.execute("UPDATE table_shows SET forced = ? WHERE sonarrSeriesId LIKE ?", (forced, serie)) - + conn.commit() c.close() - + for serie in series: list_missing_subtitles(serie) - + redirect(ref) @@ -710,17 +713,17 @@ def edit_serieseditor(): def episodes(no): authorize() # single_language = settings.general.getboolean('single_language') - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) conn.create_function("path_substitution", 1, path_replace) c = conn.cursor() - + series_details = [] series_details = c.execute( "SELECT title, overview, poster, fanart, hearing_impaired, tvdbid, audio_language, languages, path_substitution(path), forced FROM table_shows WHERE sonarrSeriesId LIKE ?", (str(no),)).fetchone() tvdbid = series_details[5] - + episodes = c.execute( "SELECT title, path_substitution(path), season, episode, subtitles, sonarrSeriesId, missing_subtitles, sonarrEpisodeId, scene_name, monitored, failedAttempts FROM table_episodes WHERE sonarrSeriesId LIKE ? ORDER BY episode ASC", (str(no),)).fetchall() @@ -731,7 +734,7 @@ def episodes(no): seasons_list = [] for key, season in itertools.groupby(episodes, operator.itemgetter(2)): seasons_list.append(list(season)) - + return template('episodes', bazarr_version=bazarr_version, no=no, details=series_details, languages=languages, seasons=seasons_list, url_sonarr_short=url_sonarr_short, base_url=base_url, tvdbid=tvdbid, number=number, current_port=settings.general.port) @@ -742,11 +745,11 @@ def episodes(no): def movies(): authorize() single_language = settings.general.getboolean('single_language') - + db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace_movie) c = db.cursor() - + c.execute("SELECT COUNT(*) FROM table_movies") missing_count = c.fetchone() missing_count = missing_count[0] @@ -756,7 +759,7 @@ def movies(): page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(missing_count / (page_size + 0.0))) - + c.execute( "SELECT tmdbId, title, path_substitution(path), languages, hearing_impaired, radarrId, poster, audio_language, monitored, sceneName, forced FROM table_movies ORDER BY sortTitle ASC LIMIT ? OFFSET ?", (page_size, offset,)) @@ -775,15 +778,15 @@ def movies(): def movieseditor(): authorize() single_language = settings.general.getboolean('single_language') - + db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace_movie) c = db.cursor() - + c.execute("SELECT COUNT(*) FROM table_movies") missing_count = c.fetchone() missing_count = missing_count[0] - + c.execute( "SELECT tmdbId, title, path_substitution(path), languages, hearing_impaired, radarrId, poster, audio_language, forced FROM table_movies ORDER BY title ASC") data = c.fetchall() @@ -801,16 +804,16 @@ def movieseditor(): def edit_movieseditor(): authorize() ref = request.environ['HTTP_REFERER'] - + movies = request.forms.get('movies') movies = ast.literal_eval(str('[' + movies + ']')) lang = request.forms.getall('languages') hi = request.forms.get('hearing_impaired') forced = request.forms.get('forced') - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() - + for movie in movies: if str(lang) != "[]" and str(lang) != "['']": if str(lang) == "['None']": @@ -822,13 +825,13 @@ def edit_movieseditor(): c.execute("UPDATE table_movies SET hearing_impaired = ? WHERE radarrId LIKE ?", (hi, movie)) if forced != '': c.execute("UPDATE table_movies SET forced = ? WHERE radarrId LIKE ?", (forced, movie)) - + conn.commit() c.close() - + for movie in movies: list_missing_subtitles_movies(movie) - + redirect(ref) @@ -837,13 +840,13 @@ def edit_movieseditor(): def edit_movie(no): authorize() ref = request.environ['HTTP_REFERER'] - + lang = request.forms.getall('languages') if len(lang) > 0: pass else: lang = 'None' - + single_language = settings.general.getboolean('single_language') if single_language: if str(lang) == "['None']": @@ -853,23 +856,24 @@ def edit_movie(no): else: if str(lang) == "['']": lang = '[]' - + hi = request.forms.get('hearing_impaired') forced = request.forms.get('forced') - + if hi == "on": hi = "True" else: hi = "False" - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() - c.execute("UPDATE table_movies SET languages = ?, hearing_impaired = ?, forced = ? WHERE radarrId LIKE ?", (str(lang), hi, forced, no)) + c.execute("UPDATE table_movies SET languages = ?, hearing_impaired = ?, forced = ? WHERE radarrId LIKE ?", + (str(lang), hi, forced, no)) conn.commit() c.close() - + list_missing_subtitles_movies(no) - + redirect(ref) @@ -877,21 +881,20 @@ def edit_movie(no): @custom_auth_basic(check_credentials) def movie(no): authorize() - # single_language = settings.general.getboolean('single_language') - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) conn.create_function("path_substitution", 1, path_replace_movie) c = conn.cursor() - + movies_details = [] movies_details = c.execute( "SELECT title, overview, poster, fanart, hearing_impaired, tmdbid, audio_language, languages, path_substitution(path), subtitles, radarrId, missing_subtitles, sceneName, monitored, failedAttempts, forced FROM table_movies WHERE radarrId LIKE ?", (str(no),)).fetchone() tmdbid = movies_details[5] - + languages = c.execute("SELECT code2, name FROM table_settings_languages WHERE enabled = 1").fetchall() c.close() - + return template('movie', bazarr_version=bazarr_version, no=no, details=movies_details, languages=languages, url_radarr_short=url_radarr_short, base_url=base_url, tmdbid=tmdbid, current_port=settings.general.port) @@ -902,9 +905,9 @@ def movie(no): def scan_disk(no): authorize() ref = request.environ['HTTP_REFERER'] - + series_scan_subtitles(no) - + redirect(ref) @@ -913,9 +916,9 @@ def scan_disk(no): def scan_disk_movie(no): authorize() ref = request.environ['HTTP_REFERER'] - + movies_scan_subtitles(no) - + redirect(ref) @@ -926,7 +929,7 @@ def search_missing_subtitles(no): ref = request.environ['HTTP_REFERER'] add_job(series_download_subtitles, args=[no], name=('search_missing_subtitles_' + str(no))) - + redirect(ref) @@ -935,9 +938,9 @@ def search_missing_subtitles(no): def search_missing_subtitles_movie(no): authorize() ref = request.environ['HTTP_REFERER'] - + add_job(movies_download_subtitles, args=[no], name=('movies_download_subtitles_' + str(no))) - + redirect(ref) @@ -954,7 +957,7 @@ def historyseries(): authorize() db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - + c.execute("SELECT COUNT(*) FROM table_history") row_count = c.fetchone() row_count = row_count[0] @@ -964,7 +967,7 @@ def historyseries(): page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(row_count / (page_size + 0.0))) - + now = datetime.now() today = [] thisweek = [] @@ -979,24 +982,24 @@ def historyseries(): if now - timedelta(weeks=52) <= datetime.fromtimestamp(stat[0]) <= now: thisyear.append(datetime.fromtimestamp(stat[0]).date()) stats = [len(today), len(thisweek), len(thisyear), total] - + c.execute( "SELECT table_history.action, table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_history.timestamp, table_history.description, table_history.sonarrSeriesId, table_episodes.path, table_shows.languages, table_history.language, table_history.score, table_shows.forced FROM table_history LEFT JOIN table_shows on table_shows.sonarrSeriesId = table_history.sonarrSeriesId LEFT JOIN table_episodes on table_episodes.sonarrEpisodeId = table_history.sonarrEpisodeId ORDER BY id DESC LIMIT ? OFFSET ?", (page_size, offset,)) data = c.fetchall() - + upgradable_episodes = [] upgradable_episodes_not_perfect = [] if settings.general.getboolean('upgrade_subs'): days_to_upgrade_subs = settings.general.days_to_upgrade_subs minimum_timestamp = ((datetime.now() - timedelta(days=int(days_to_upgrade_subs))) - datetime(1970, 1, 1)).total_seconds() - + if settings.general.getboolean('upgrade_manual'): query_actions = [1, 2, 3] else: query_actions = [1, 3] - + upgradable_episodes = c.execute("""SELECT video_path, MAX(timestamp), score FROM table_history WHERE action IN (""" + ','.join(map(str, query_actions)) + """) AND @@ -1011,11 +1014,11 @@ def historyseries(): else: if int(upgradable_episode[2]) < 360: upgradable_episodes_not_perfect.append(upgradable_episode) - + c.close() - + data = reversed(sorted(data, key=operator.itemgetter(4))) - + return template('historyseries', bazarr_version=bazarr_version, rows=data, row_count=row_count, page=page, max_page=max_page, stats=stats, base_url=base_url, page_size=page_size, current_port=settings.general.port, upgradable_episodes=upgradable_episodes_not_perfect) @@ -1027,7 +1030,7 @@ def historymovies(): authorize() db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - + c.execute("SELECT COUNT(*) FROM table_history_movie") row_count = c.fetchone() row_count = row_count[0] @@ -1037,7 +1040,7 @@ def historymovies(): page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(row_count / (page_size + 0.0))) - + now = datetime.now() today = [] thisweek = [] @@ -1052,24 +1055,24 @@ def historymovies(): if now - timedelta(weeks=52) <= datetime.fromtimestamp(stat[0]) <= now: thisyear.append(datetime.fromtimestamp(stat[0]).date()) stats = [len(today), len(thisweek), len(thisyear), total] - + c.execute( "SELECT table_history_movie.action, table_movies.title, table_history_movie.timestamp, table_history_movie.description, table_history_movie.radarrId, table_history_movie.video_path, table_movies.languages, table_history_movie.language, table_history_movie.score, table_movies.forced FROM table_history_movie LEFT JOIN table_movies on table_movies.radarrId = table_history_movie.radarrId ORDER BY id DESC LIMIT ? OFFSET ?", (page_size, offset,)) data = c.fetchall() - + upgradable_movies = [] upgradable_movies_not_perfect = [] if settings.general.getboolean('upgrade_subs'): days_to_upgrade_subs = settings.general.days_to_upgrade_subs minimum_timestamp = ((datetime.now() - timedelta(days=int(days_to_upgrade_subs))) - datetime(1970, 1, 1)).total_seconds() - + if settings.general.getboolean('upgrade_manual'): query_actions = [1, 2, 3] else: query_actions = [1, 3] - + upgradable_movies = c.execute("""SELECT video_path, MAX(timestamp), score FROM table_history_movie WHERE action IN (""" + ','.join(map(str, query_actions)) + """) AND @@ -1084,7 +1087,7 @@ def historymovies(): else: if int(upgradable_movie[2]) < 120: upgradable_movies_not_perfect.append(upgradable_movie) - + c.close() data = reversed(sorted(data, key=operator.itemgetter(2))) return template('historymovies', bazarr_version=bazarr_version, rows=data, row_count=row_count, @@ -1106,12 +1109,12 @@ def wantedseries(): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace) c = db.cursor() - + if settings.sonarr.getboolean('only_monitored'): monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" - + c.execute("SELECT COUNT(*) FROM table_episodes WHERE missing_subtitles != '[]'" + monitored_only_query_string) missing_count = c.fetchone() missing_count = missing_count[0] @@ -1121,7 +1124,7 @@ def wantedseries(): page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(missing_count / (page_size + 0.0))) - + c.execute( "SELECT table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_episodes.missing_subtitles, table_episodes.sonarrSeriesId, path_substitution(table_episodes.path), table_shows.hearing_impaired, table_episodes.sonarrEpisodeId, table_episodes.scene_name, table_episodes.failedAttempts FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.missing_subtitles != '[]'" + monitored_only_query_string + " ORDER BY table_episodes._rowid_ DESC LIMIT ? OFFSET ?", (page_size, offset,)) @@ -1139,12 +1142,12 @@ def wantedmovies(): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace_movie) c = db.cursor() - + if settings.radarr.getboolean('only_monitored'): monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" - + c.execute("SELECT COUNT(*) FROM table_movies WHERE missing_subtitles != '[]'" + monitored_only_query_string) missing_count = c.fetchone() missing_count = missing_count[0] @@ -1154,7 +1157,7 @@ def wantedmovies(): page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(missing_count / (page_size + 0.0))) - + c.execute( "SELECT title, missing_subtitles, radarrId, path_substitution(path), hearing_impaired, sceneName, failedAttempts FROM table_movies WHERE missing_subtitles != '[]'" + monitored_only_query_string + " ORDER BY _rowid_ DESC LIMIT ? OFFSET ?", (page_size, offset,)) @@ -1188,7 +1191,7 @@ def _settings(): c.execute("SELECT * FROM table_settings_notifier ORDER BY name") settings_notifier = c.fetchall() c.close() - + return template('settings', bazarr_version=bazarr_version, settings=settings, settings_languages=settings_languages, settings_providers=settings_providers, settings_notifier=settings_notifier, base_url=base_url, current_port=settings.general.port) @@ -1199,10 +1202,10 @@ def _settings(): def save_settings(): authorize() ref = request.environ['HTTP_REFERER'] - + conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() - + settings_general_ip = request.forms.get('settings_general_ip') settings_general_port = request.forms.get('settings_general_port') settings_general_baseurl = request.forms.get('settings_general_baseurl') @@ -1310,14 +1313,14 @@ def save_settings(): settings_anti_captcha_key = request.forms.get('settings_anti_captcha_key') settings_death_by_captcha_username = request.forms.get('settings_death_by_captcha_username') settings_death_by_captcha_password = request.forms.get('settings_death_by_captcha_password') - + before = (unicode(settings.general.ip), int(settings.general.port), unicode(settings.general.base_url), unicode(settings.general.path_mappings), unicode(settings.general.getboolean('use_sonarr')), unicode(settings.general.getboolean('use_radarr')), unicode(settings.general.path_mappings_movie)) after = (unicode(settings_general_ip), int(settings_general_port), unicode(settings_general_baseurl), unicode(settings_general_pathmapping), unicode(settings_general_use_sonarr), unicode(settings_general_use_radarr), unicode(settings_general_pathmapping_movie)) - + settings.general.ip = text_type(settings_general_ip) settings.general.port = text_type(settings_general_port) settings.general.base_url = text_type(settings_general_baseurl) @@ -1349,7 +1352,7 @@ def save_settings(): settings.anticaptcha.anti_captcha_key = text_type(settings_anti_captcha_key) settings.deathbycaptcha.username = text_type(settings_death_by_captcha_username) settings.deathbycaptcha.password = text_type(settings_death_by_captcha_password) - + # set anti-captcha provider and key if settings.general.anti_captcha_provider == 'anti-captcha': os.environ["ANTICAPTCHA_CLASS"] = 'AntiCaptchaProxyLess' @@ -1360,7 +1363,7 @@ def save_settings(): {settings.deathbycaptcha.username, settings.deathbycaptcha.password}) else: os.environ["ANTICAPTCHA_CLASS"] = '' - + settings.general.minimum_score_movie = text_type(settings_general_minimum_score_movies) settings.general.use_embedded_subs = text_type(settings_general_embedded) settings.general.utf8_encode = text_type(settings_general_utf8_encode) @@ -1370,7 +1373,7 @@ def save_settings(): if after != before: configured() - + settings_proxy_type = request.forms.get('settings_proxy_type') settings_proxy_url = request.forms.get('settings_proxy_url') settings_proxy_port = request.forms.get('settings_proxy_port') @@ -1394,11 +1397,11 @@ def save_settings(): settings.proxy.username = text_type(settings_proxy_username) settings.proxy.password = text_type(settings_proxy_password) settings.proxy.exclude = text_type(settings_proxy_exclude) - + settings_auth_type = request.forms.get('settings_auth_type') settings_auth_username = request.forms.get('settings_auth_username') settings_auth_password = request.forms.get('settings_auth_password') - + if settings.auth.type != settings_auth_type: configured() if settings.auth.password == settings_auth_password: @@ -1431,7 +1434,7 @@ def save_settings(): pass else: aaa._beaker_session.delete() - + settings_sonarr_ip = request.forms.get('settings_sonarr_ip') settings_sonarr_port = request.forms.get('settings_sonarr_port') settings_sonarr_baseurl = request.forms.get('settings_sonarr_baseurl') @@ -1447,7 +1450,7 @@ def save_settings(): else: settings_sonarr_only_monitored = 'True' settings_sonarr_sync = request.forms.get('settings_sonarr_sync') - + settings.sonarr.ip = text_type(settings_sonarr_ip) settings.sonarr.port = text_type(settings_sonarr_port) settings.sonarr.base_url = text_type(settings_sonarr_baseurl) @@ -1455,7 +1458,7 @@ def save_settings(): settings.sonarr.apikey = text_type(settings_sonarr_apikey) settings.sonarr.only_monitored = text_type(settings_sonarr_only_monitored) settings.sonarr.full_update = text_type(settings_sonarr_sync) - + settings_radarr_ip = request.forms.get('settings_radarr_ip') settings_radarr_port = request.forms.get('settings_radarr_port') settings_radarr_baseurl = request.forms.get('settings_radarr_baseurl') @@ -1471,7 +1474,7 @@ def save_settings(): else: settings_radarr_only_monitored = 'True' settings_radarr_sync = request.forms.get('settings_radarr_sync') - + settings.radarr.ip = text_type(settings_radarr_ip) settings.radarr.port = text_type(settings_radarr_port) settings.radarr.base_url = text_type(settings_radarr_baseurl) @@ -1479,35 +1482,35 @@ def save_settings(): settings.radarr.apikey = text_type(settings_radarr_apikey) settings.radarr.only_monitored = text_type(settings_radarr_only_monitored) settings.radarr.full_update = text_type(settings_radarr_sync) - + settings_subliminal_providers = request.forms.getall('settings_subliminal_providers') settings.general.enabled_providers = u'' if not settings_subliminal_providers else ','.join( settings_subliminal_providers) - + settings_addic7ed_random_agents = request.forms.get('settings_addic7ed_random_agents') if settings_addic7ed_random_agents is None: settings_addic7ed_random_agents = 'False' else: settings_addic7ed_random_agents = 'True' - + settings_opensubtitles_vip = request.forms.get('settings_opensubtitles_vip') if settings_opensubtitles_vip is None: settings_opensubtitles_vip = 'False' else: settings_opensubtitles_vip = 'True' - + settings_opensubtitles_ssl = request.forms.get('settings_opensubtitles_ssl') if settings_opensubtitles_ssl is None: settings_opensubtitles_ssl = 'False' else: settings_opensubtitles_ssl = 'True' - + settings_opensubtitles_skip_wrong_fps = request.forms.get('settings_opensubtitles_skip_wrong_fps') if settings_opensubtitles_skip_wrong_fps is None: settings_opensubtitles_skip_wrong_fps = 'False' else: settings_opensubtitles_skip_wrong_fps = 'True' - + settings.addic7ed.username = request.forms.get('settings_addic7ed_username') settings.addic7ed.password = request.forms.get('settings_addic7ed_password') settings.addic7ed.random_agents = text_type(settings_addic7ed_random_agents) @@ -1523,61 +1526,61 @@ def save_settings(): settings.xsubs.password = request.forms.get('settings_xsubs_password') settings.napisy24.username = request.forms.get('settings_napisy24_username') settings.napisy24.password = request.forms.get('settings_napisy24_password') - + settings_subliminal_languages = request.forms.getall('settings_subliminal_languages') c.execute("UPDATE table_settings_languages SET enabled = 0") for item in settings_subliminal_languages: c.execute("UPDATE table_settings_languages SET enabled = '1' WHERE code2 = ?", (item,)) - + settings_serie_default_enabled = request.forms.get('settings_serie_default_enabled') if settings_serie_default_enabled is None: settings_serie_default_enabled = 'False' else: settings_serie_default_enabled = 'True' settings.general.serie_default_enabled = text_type(settings_serie_default_enabled) - + settings_serie_default_languages = str(request.forms.getall('settings_serie_default_languages')) if settings_serie_default_languages == "['None']": settings_serie_default_languages = 'None' settings.general.serie_default_language = text_type(settings_serie_default_languages) - + settings_serie_default_hi = request.forms.get('settings_serie_default_hi') if settings_serie_default_hi is None: settings_serie_default_hi = 'False' else: settings_serie_default_hi = 'True' settings.general.serie_default_hi = text_type(settings_serie_default_hi) - + settings_serie_default_forced = str(request.forms.get('settings_serie_default_forced')) settings.general.serie_default_forced = text_type(settings_serie_default_forced) - + settings_movie_default_enabled = request.forms.get('settings_movie_default_enabled') if settings_movie_default_enabled is None: settings_movie_default_enabled = 'False' else: settings_movie_default_enabled = 'True' settings.general.movie_default_enabled = text_type(settings_movie_default_enabled) - + settings_movie_default_languages = str(request.forms.getall('settings_movie_default_languages')) if settings_movie_default_languages == "['None']": settings_movie_default_languages = 'None' settings.general.movie_default_language = text_type(settings_movie_default_languages) - + settings_movie_default_hi = request.forms.get('settings_movie_default_hi') if settings_movie_default_hi is None: settings_movie_default_hi = 'False' else: settings_movie_default_hi = 'True' settings.general.movie_default_hi = text_type(settings_movie_default_hi) - + settings_movie_default_forced = str(request.forms.get('settings_movie_default_forced')) settings.general.movie_default_forced = text_type(settings_movie_default_forced) - + with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: settings.write(handle) - + configure_logging(settings.general.getboolean('debug') or args.debug) - + notifiers = c.execute("SELECT * FROM table_settings_notifier ORDER BY name").fetchall() for notifier in notifiers: enabled = request.forms.get('settings_notifier_' + notifier[0] + '_enabled') @@ -1588,17 +1591,16 @@ def save_settings(): notifier_url = request.forms.get('settings_notifier_' + notifier[0] + '_url') c.execute("UPDATE table_settings_notifier SET enabled = ?, url = ? WHERE name = ?", (enabled, notifier_url, notifier[0])) - + conn.commit() c.close() - + schedule_update_job() sonarr_full_update() radarr_full_update() - + logging.info('BAZARR Settings saved succesfully.') - - + if ref.find('saved=true') > 0: redirect(ref) else: @@ -1610,10 +1612,10 @@ def save_settings(): def check_update(): authorize() ref = request.environ['HTTP_REFERER'] - + if not args.no_update: check_and_apply_update() - + redirect(ref) @@ -1632,26 +1634,26 @@ def system(): ('minute', 60), ('second', 1) ] - + strings = [] for period_name, period_seconds in periods: if seconds > period_seconds: period_value, seconds = divmod(seconds, period_seconds) has_s = 's' if period_value > 1 else '' strings.append("%s %s%s" % (period_value, period_name, has_s)) - + return ", ".join(strings) - + def get_time_from_cron(cron): text = "" sun = str(cron[4]) hour = str(cron[5]) minute = str(cron[6]) second = str(cron[7]) - + if sun != "*": text = "Sunday at " - + if hour != "0" and hour != "*": text = text + hour if hour == "0" or hour == "1": @@ -1660,7 +1662,7 @@ def system(): text = text + "am" else: text = text + " hours" - + if minute != "*" and second != "0": text = text + ", " elif minute == "*" and second != "0": @@ -1673,7 +1675,7 @@ def system(): text = text + " minute" else: text = text + " minutes" - + if second != "0" and second != "*": text = text + " and " if second != "0" and second != "*": @@ -1686,9 +1688,9 @@ def system(): text = "everyday at " + text elif text == "": text = "Never" - + return text - + task_list = [] for job in scheduler.get_jobs(): if isinstance(job.trigger, CronTrigger): @@ -1698,22 +1700,23 @@ def system(): next_run = pretty.date(job.next_run_time.replace(tzinfo=None)) else: next_run = pretty.date(job.next_run_time.replace(tzinfo=None)) - + if isinstance(job.trigger, IntervalTrigger): interval = "every " + get_time_from_interval(job.trigger.__getstate__()['interval']) task_list.append([job.name, interval, next_run, job.id]) elif isinstance(job.trigger, CronTrigger): task_list.append([job.name, get_time_from_cron(job.trigger.fields), next_run, job.id]) - + throttled_providers = list_throttled_providers() - + try: with open(os.path.join(args.config_dir, 'config', 'releases.txt'), 'r') as f: releases = ast.literal_eval(f.read()) except Exception as e: releases = [] - logging.exception('BAZARR cannot parse releases caching file: ' + os.path.join(args.config_dir, 'config', 'releases.txt')) - + logging.exception( + 'BAZARR cannot parse releases caching file: ' + os.path.join(args.config_dir, 'config', 'releases.txt')) + use_sonarr = settings.general.getboolean('use_sonarr') apikey_sonarr = settings.sonarr.apikey sv = url_sonarr + "/api/system/status?apikey=" + apikey_sonarr @@ -1723,7 +1726,7 @@ def system(): sonarr_version = requests.get(sv, timeout=15, verify=False).json()['version'] except: pass - + use_radarr = settings.general.getboolean('use_radarr') apikey_radarr = settings.radarr.apikey rv = url_radarr + "/api/system/status?apikey=" + apikey_radarr @@ -1733,9 +1736,9 @@ def system(): radarr_version = requests.get(rv, timeout=15, verify=False).json()['version'] except: pass - + page_size = int(settings.general.page_size) - + return template('system', bazarr_version=bazarr_version, sonarr_version=sonarr_version, radarr_version=radarr_version, operating_system=platform.platform(), python_version=platform.python_version(), @@ -1753,7 +1756,7 @@ def get_logs(): lin = [] lin = line.split('|') logs.append(lin) - + return dict(data=logs) @@ -1762,9 +1765,9 @@ def get_logs(): def execute_task(taskid): authorize() ref = request.environ['HTTP_REFERER'] - + execute_now(taskid) - + redirect(ref) @@ -1796,7 +1799,7 @@ def remove_subtitles_movie(): language = request.forms.get('language') subtitlesPath = request.forms.get('subtitlesPath') radarrId = request.forms.get('radarrId') - + try: os.remove(subtitlesPath) result = language_from_alpha3(language) + " subtitles deleted from disk." @@ -1812,7 +1815,7 @@ def remove_subtitles_movie(): def get_subtitle(): authorize() ref = request.environ['HTTP_REFERER'] - + episodePath = request.forms.get('episodePath') sceneName = request.forms.get('sceneName') language = request.forms.get('language') @@ -1821,11 +1824,10 @@ def get_subtitle(): sonarrSeriesId = request.forms.get('sonarrSeriesId') sonarrEpisodeId = request.forms.get('sonarrEpisodeId') title = request.forms.get('title') - # tvdbid = request.forms.get('tvdbid') - + providers_list = get_providers() providers_auth = get_providers_auth() - + try: result = download_subtitle(episodePath, language, hi, forced, providers_list, providers_auth, sceneName, title, 'series') @@ -1856,10 +1858,10 @@ def manual_search_json(): hi = request.forms.get('hi') forced = request.forms.get('forced') title = request.forms.get('title') - + providers_list = get_providers() providers_auth = get_providers_auth() - + data = manual_search(episodePath, language, hi, forced, providers_list, providers_auth, sceneName, title, 'series') return dict(data=data) @@ -1869,7 +1871,7 @@ def manual_search_json(): def manual_get_subtitle(): authorize() ref = request.environ['HTTP_REFERER'] - + episodePath = request.forms.get('episodePath') sceneName = request.forms.get('sceneName') language = request.forms.get('language') @@ -1880,12 +1882,12 @@ def manual_get_subtitle(): sonarrSeriesId = request.forms.get('sonarrSeriesId') sonarrEpisodeId = request.forms.get('sonarrEpisodeId') title = request.forms.get('title') - - providers_list = get_providers() + providers_auth = get_providers_auth() - + try: - result = manual_download_subtitle(episodePath, language, hi, forced, subtitle, selected_provider, providers_auth, + result = manual_download_subtitle(episodePath, language, hi, forced, subtitle, selected_provider, + providers_auth, sceneName, title, 'series') if result is not None: message = result[0] @@ -1908,21 +1910,21 @@ def manual_get_subtitle(): def get_subtitle_movie(): authorize() ref = request.environ['HTTP_REFERER'] - + moviePath = request.forms.get('moviePath') sceneName = request.forms.get('sceneName') language = request.forms.get('language') hi = request.forms.get('hi') forced = request.forms.get('forced') radarrId = request.forms.get('radarrId') - # tmdbid = request.forms.get('tmdbid') title = request.forms.get('title') - + providers_list = get_providers() providers_auth = get_providers_auth() - + try: - result = download_subtitle(moviePath, language, hi, forced, providers_list, providers_auth, sceneName, title, 'movie') + result = download_subtitle(moviePath, language, hi, forced, providers_list, providers_auth, sceneName, title, + 'movie') if result is not None: message = result[0] path = result[1] @@ -1943,18 +1945,17 @@ def get_subtitle_movie(): @custom_auth_basic(check_credentials) def manual_search_movie_json(): authorize() - ref = request.environ['HTTP_REFERER'] - + moviePath = request.forms.get('moviePath') sceneName = request.forms.get('sceneName') language = request.forms.get('language') hi = request.forms.get('hi') forced = request.forms.get('forced') title = request.forms.get('title') - + providers_list = get_providers() providers_auth = get_providers_auth() - + data = manual_search(moviePath, language, hi, forced, providers_list, providers_auth, sceneName, title, 'movie') return dict(data=data) @@ -1964,7 +1965,7 @@ def manual_search_movie_json(): def manual_get_subtitle_movie(): authorize() ref = request.environ['HTTP_REFERER'] - + moviePath = request.forms.get('moviePath') sceneName = request.forms.get('sceneName') language = request.forms.get('language') @@ -1974,10 +1975,9 @@ def manual_get_subtitle_movie(): subtitle = request.forms.get('subtitle') radarrId = request.forms.get('radarrId') title = request.forms.get('title') - - providers_list = get_providers() + providers_auth = get_providers_auth() - + try: result = manual_download_subtitle(moviePath, language, hi, forced, subtitle, selected_provider, providers_auth, sceneName, title, 'movie') @@ -2063,7 +2063,7 @@ def test_notification(protocol, provider): provider = urllib.unquote(provider) apobj = apprise.Apprise() apobj.add(protocol + "://" + provider) - + apobj.notify( title='Bazarr test notification', body=('Test notification') @@ -2084,9 +2084,11 @@ def notifications(): def running_tasks_list(): return dict(tasks=running_tasks) + # Mute DeprecationWarning warnings.simplefilter("ignore", DeprecationWarning) -server = WSGIServer((str(settings.general.ip), (int(args.port) if args.port else int(settings.general.port))), app, handler_class=WebSocketHandler) +server = WSGIServer((str(settings.general.ip), (int(args.port) if args.port else int(settings.general.port))), app, + handler_class=WebSocketHandler) try: logging.info('BAZARR is started and waiting for request on http://' + str(settings.general.ip) + ':' + (str( args.port) if args.port else str(settings.general.port)) + str(base_url)) diff --git a/bazarr/notifier.py b/bazarr/notifier.py index 8dbf566a0..5aa7bd8ba 100644 --- a/bazarr/notifier.py +++ b/bazarr/notifier.py @@ -11,13 +11,13 @@ from get_args import args def update_notifier(): # define apprise object a = apprise.Apprise() - + # Retrieve all of the details results = a.details() - + notifiers_new = [] notifiers_old = [] - + conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() notifiers_current = c_db.execute('SELECT name FROM table_settings_notifier').fetchall() @@ -28,15 +28,15 @@ def update_notifier(): else: notifiers_old.append(x['service_name']) notifier_current = [i[0] for i in notifiers_current] - + notifiers_to_delete = list(set(notifier_current) - set(notifiers_old)) - + for notifier_new in notifiers_new: c_db.execute('INSERT INTO `table_settings_notifier` (name, enabled) VALUES (?, ?);', (notifier_new, '0')) - + for notifier_to_delete in notifiers_to_delete: c_db.execute('DELETE FROM `table_settings_notifier` WHERE name=?', (notifier_to_delete,)) - + conn_db.commit() c_db.close() @@ -46,7 +46,7 @@ def get_notifier_providers(): c_db = conn_db.cursor() providers = c_db.execute('SELECT name, url FROM table_settings_notifier WHERE enabled = 1').fetchall() c_db.close() - + return providers @@ -55,7 +55,7 @@ def get_series_name(sonarrSeriesId): c_db = conn_db.cursor() data = c_db.execute('SELECT title FROM table_shows WHERE sonarrSeriesId = ?', (sonarrSeriesId,)).fetchone() c_db.close() - + return data[0] @@ -65,7 +65,7 @@ def get_episode_name(sonarrEpisodeId): data = c_db.execute('SELECT title, season, episode FROM table_episodes WHERE sonarrEpisodeId = ?', (sonarrEpisodeId,)).fetchone() c_db.close() - + return data[0], data[1], data[2] @@ -74,7 +74,7 @@ def get_movies_name(radarrId): c_db = conn_db.cursor() data = c_db.execute('SELECT title FROM table_movies WHERE radarrId = ?', (radarrId,)).fetchone() c_db.close() - + return data[0] @@ -82,13 +82,13 @@ def send_notifications(sonarrSeriesId, sonarrEpisodeId, message): providers = get_notifier_providers() series = get_series_name(sonarrSeriesId) episode = get_episode_name(sonarrEpisodeId) - + apobj = apprise.Apprise() - + for provider in providers: if provider[1] is not None: apobj.add(provider[1]) - + apobj.notify( title='Bazarr notification', body=(series + ' - S' + str(episode[1]).zfill(2) + 'E' + str(episode[2]).zfill(2) + ' - ' + episode[ @@ -99,13 +99,13 @@ def send_notifications(sonarrSeriesId, sonarrEpisodeId, message): def send_notifications_movie(radarrId, message): providers = get_notifier_providers() movie = get_movies_name(radarrId) - + apobj = apprise.Apprise() - + for provider in providers: if provider[1] is not None: apobj.add(provider[1]) - + apobj.notify( title='Bazarr notification', body=movie + ' : ' + message, diff --git a/bazarr/queueconfig.py b/bazarr/queueconfig.py index ad33e3218..fd33c1850 100644 --- a/bazarr/queueconfig.py +++ b/bazarr/queueconfig.py @@ -6,11 +6,11 @@ class Notify: """ This class is used to read or write items to the notifications deque. """ - + def __init__(self): self.queue = deque(maxlen=10) - - def write(self, msg, type='info', duration='temporary', button='null', queue='main', item=0, length=0): + + def write(self, msg, type='info', duration='temporary', button='null', queue='main', item=0, length=0): """ :param msg: The message to display. :type msg: str @@ -27,15 +27,15 @@ class Notify: :param length: The length of the group of item for progress bar :type length: int """ - + self.queue.append(json.dumps([msg, type, duration, button, queue, item, length])) - + def read(self): """ :return: Return the oldest notification available. :rtype: str """ - + if self.queue: return self.queue.popleft() diff --git a/bazarr/scheduler.py b/bazarr/scheduler.py index bcce6e41b..b51aa2ce3 100644 --- a/bazarr/scheduler.py +++ b/bazarr/scheduler.py @@ -67,7 +67,6 @@ if str(get_localzone()) == "local": else: scheduler = BackgroundScheduler() - global running_tasks running_tasks = [] @@ -81,19 +80,25 @@ def task_listener(event): scheduler.add_listener(task_listener, EVENT_JOB_SUBMITTED | EVENT_JOB_EXECUTED) + def schedule_update_job(): if not args.no_update: if settings.general.getboolean('auto_update'): scheduler.add_job(check_and_apply_update, IntervalTrigger(hours=6), max_instances=1, coalesce=True, - misfire_grace_time=15, id='update_bazarr', name='Update bazarr from source on Github' if not args.release_update else 'Update bazarr from release on Github', replace_existing=True) + misfire_grace_time=15, id='update_bazarr', + name='Update bazarr from source on Github' if not args.release_update else 'Update bazarr from release on Github', + replace_existing=True) else: scheduler.add_job(check_and_apply_update, CronTrigger(year='2100'), hour=4, id='update_bazarr', - name='Update bazarr from source on Github' if not args.release_update else 'Update bazarr from release on Github', replace_existing=True) + name='Update bazarr from source on Github' if not args.release_update else 'Update bazarr from release on Github', + replace_existing=True) scheduler.add_job(check_releases, IntervalTrigger(hours=6), max_instances=1, coalesce=True, - misfire_grace_time=15, id='update_release', name='Update release info', replace_existing=True) - + misfire_grace_time=15, id='update_release', name='Update release info', + replace_existing=True) + else: - scheduler.add_job(check_releases, IntervalTrigger(hours=6), max_instances=1, coalesce=True, misfire_grace_time=15, + scheduler.add_job(check_releases, IntervalTrigger(hours=6), max_instances=1, coalesce=True, + misfire_grace_time=15, id='update_release', name='Update release info', replace_existing=True) @@ -123,7 +128,6 @@ scheduler.start() def add_job(job, name=None, max_instances=1, coalesce=True, args=None): - scheduler.add_job(job, DateTrigger(run_date=datetime.now()), name=name, id=name, max_instances=max_instances, coalesce=coalesce, args=args) diff --git a/bazarr/update_db.py b/bazarr/update_db.py index 49732b27a..4ce8917e8 100644 --- a/bazarr/update_db.py +++ b/bazarr/update_db.py @@ -95,16 +95,16 @@ if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): else: if settings.general.getboolean('use_radarr'): execute_now('update_movies') - + try: c.execute('alter table table_shows add column "year" "text"') c.execute('alter table table_shows add column "alternateTitles" "text"') - + c.execute('alter table table_episodes add column "format" "text"') c.execute('alter table table_episodes add column "resolution" "text"') c.execute('alter table table_episodes add column "video_codec" "text"') c.execute('alter table table_episodes add column "audio_codec" "text"') - + c.execute('alter table table_movies add column "year" "text"') c.execute('alter table table_movies add column "alternativeTitles" "text"') c.execute('alter table table_movies add column "format" "text"') @@ -121,22 +121,22 @@ if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): execute_now('sync_episodes') if settings.general.getboolean('use_radarr'): execute_now('update_movies') - + try: c.execute('alter table table_history add column "video_path" "text"') c.execute('alter table table_history add column "language" "text"') c.execute('alter table table_history add column "provider" "text"') c.execute('alter table table_history add column "score" "text"') - + c.execute('alter table table_history_movie add column "video_path" "text"') c.execute('alter table table_history_movie add column "language" "text"') c.execute('alter table table_history_movie add column "provider" "text"') c.execute('alter table table_history_movie add column "score" "text"') - + db.commit() except: pass - + try: c.execute('alter table table_shows add column "forced" "text"') except: @@ -144,7 +144,7 @@ if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): else: c.execute('UPDATE table_shows SET forced="False"') db.commit() - + try: c.execute('alter table table_movies add column "forced" "text"') db.commit() @@ -153,5 +153,5 @@ if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): else: c.execute('UPDATE table_movies SET forced="False"') db.commit() - + db.close() diff --git a/bazarr/utils.py b/bazarr/utils.py index 159bbb42e..7432abf6e 100644 --- a/bazarr/utils.py +++ b/bazarr/utils.py @@ -9,61 +9,64 @@ from whichcraft import which from get_args import args -def history_log(action, sonarrSeriesId, sonarrEpisodeId, description, video_path=None, language=None, provider=None, score=None, forced=False): +def history_log(action, sonarrSeriesId, sonarrEpisodeId, description, video_path=None, language=None, provider=None, + score=None, forced=False): # Open database connection db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - + if forced: language = language + ":forced" - + # Get Sonarr API URL from database config table history = c.execute( '''INSERT INTO table_history(action, sonarrSeriesId, sonarrEpisodeId, timestamp, description, video_path, language, provider, score) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''', (action, sonarrSeriesId, sonarrEpisodeId, time.time(), description, video_path, language, provider, score)) - + # Commit changes to DB db.commit() - + # Close database connection db.close() -def history_log_movie(action, radarrId, description, video_path=None, language=None, provider=None, score=None, forced=False): +def history_log_movie(action, radarrId, description, video_path=None, language=None, provider=None, score=None, + forced=False): # Open database connection db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - + if forced: language = language + ":forced" - + history = c.execute( '''INSERT INTO table_history_movie(action, radarrId, timestamp, description, video_path, language, provider, score) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', (action, radarrId, time.time(), description, video_path, language, provider, score)) - + # Commit changes to DB db.commit() - + # Close database connection db.close() + def get_binary(name): binaries_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'bin')) - + exe = None installed_exe = which(name) - + if installed_exe and os.path.isfile(installed_exe): return installed_exe else: - if platform.system() == "Windows": # Windows + if platform.system() == "Windows": # Windows exe = os.path.abspath(os.path.join(binaries_dir, "Windows", "i386", name, "%s.exe" % name)) - - elif platform.system() == "Darwin": # MacOSX + + elif platform.system() == "Darwin": # MacOSX exe = os.path.abspath(os.path.join(binaries_dir, "MacOSX", "i386", name, name)) - - elif platform.system() == "Linux": # Linux + + elif platform.system() == "Linux": # Linux exe = os.path.abspath(os.path.join(binaries_dir, "Linux", platform.machine(), name, name)) - + if exe and os.path.isfile(exe): - return exe \ No newline at end of file + return exe