diff --git a/bazarr/check_update.py b/bazarr/check_update.py index 5be9b65f6..cf103a6c7 100644 --- a/bazarr/check_update.py +++ b/bazarr/check_update.py @@ -1,7 +1,4 @@ -from get_argv import config_dir - -from get_settings import get_general_settings - +# coding=utf-8 import os import logging import sqlite3 @@ -10,6 +7,10 @@ import requests import git +from get_argv import config_dir + +from config import settings + current_working_directory = os.path.dirname(os.path.dirname(__file__)) def gitconfig(): @@ -32,7 +33,7 @@ def gitconfig(): def check_and_apply_update(): gitconfig() check_releases() - branch = get_general_settings()[5] + branch = settings.general.branch g = git.cmd.Git(current_working_directory) g.fetch('origin') result = g.diff('--shortstat', 'origin/' + branch) diff --git a/bazarr/config.py b/bazarr/config.py index b39689d6a..33719cd18 100644 --- a/bazarr/config.py +++ b/bazarr/config.py @@ -44,6 +44,7 @@ def read(config_file): settings = read(os.path.join(os.path.join(config_dir, 'config', 'config.ini'))) +base_url = settings.general.base_url # sonarr url if settings.sonarr.ssl: diff --git a/bazarr/get_episodes.py b/bazarr/get_episodes.py index 11360e059..1c80e5b4e 100644 --- a/bazarr/get_episodes.py +++ b/bazarr/get_episodes.py @@ -1,30 +1,32 @@ -from get_argv import config_dir - +# coding=utf-8 import os import sqlite3 import requests import logging -from get_settings import path_replace +from get_argv import config_dir +from config import settings, url_sonarr +from helper import path_replace from list_subtitles import list_missing_subtitles, store_subtitles, series_full_scan_subtitles, movies_full_scan_subtitles - + + def update_all_episodes(): series_full_scan_subtitles() logging.info('BAZARR All existing episode subtitles indexed from disk.') list_missing_subtitles() logging.info('BAZARR All missing episode subtitles updated in database.') + def update_all_movies(): movies_full_scan_subtitles() logging.info('BAZARR All existing movie subtitles indexed from disk.') list_missing_subtitles() logging.info('BAZARR All missing movie subtitles updated in database.') + def sync_episodes(): logging.debug('BAZARR Starting episode sync from Sonarr.') - from get_settings import get_sonarr_settings - url_sonarr = get_sonarr_settings()[6] - apikey_sonarr = get_sonarr_settings()[4] + apikey_sonarr = settings.sonarr.apikey # Open database connection db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) diff --git a/bazarr/get_languages.py b/bazarr/get_languages.py index bcacbd947..af2d88b6f 100644 --- a/bazarr/get_languages.py +++ b/bazarr/get_languages.py @@ -1,8 +1,10 @@ -from get_argv import config_dir - +# coding=utf-8 +import os import sqlite3 import pycountry -import os + +from get_argv import config_dir + def load_language_in_db(): # Get languages list in langs tuple @@ -11,7 +13,7 @@ def load_language_in_db(): if hasattr(lang, 'alpha_2')] # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() # Insert languages in database table @@ -31,6 +33,7 @@ def load_language_in_db(): # Close database connection db.close() + def language_from_alpha2(lang): db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() @@ -41,6 +44,7 @@ def language_from_alpha2(lang): db.close() return result + def language_from_alpha3(lang): if lang == 'fre': lang = 'fra' @@ -53,6 +57,7 @@ def language_from_alpha3(lang): db.close() return result + def alpha2_from_alpha3(lang): if lang == 'fre': lang = 'fra' @@ -65,6 +70,7 @@ def alpha2_from_alpha3(lang): db.close() return result + def alpha2_from_language(lang): db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() @@ -75,6 +81,7 @@ def alpha2_from_language(lang): db.close() return result + def alpha3_from_alpha2(lang): db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() @@ -85,6 +92,7 @@ def alpha3_from_alpha2(lang): db.close() return result + def alpha3_from_language(lang): db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() @@ -95,5 +103,6 @@ def alpha3_from_language(lang): db.close() return result + if __name__ == '__main__': load_language_in_db() diff --git a/bazarr/get_movies.py b/bazarr/get_movies.py index 18831be20..20e10d759 100644 --- a/bazarr/get_movies.py +++ b/bazarr/get_movies.py @@ -1,23 +1,23 @@ -from get_argv import config_dir - +# coding=utf-8 import os import sqlite3 import requests import logging -from get_settings import get_general_settings, path_replace_movie +from get_argv import config_dir +from config import settings, url_radarr +from helper import path_replace_movie from list_subtitles import store_subtitles_movie, list_missing_subtitles_movies + def update_movies(): logging.debug('BAZARR Starting movie sync from Radarr.') - from get_settings import get_radarr_settings - url_radarr = get_radarr_settings()[6] - apikey_radarr = get_radarr_settings()[4] - movie_default_enabled = get_general_settings()[18] - movie_default_language = get_general_settings()[19] - movie_default_hi = get_general_settings()[20] - - if apikey_radarr == None: + apikey_radarr = settings.radarr.apikey + movie_default_enabled = settings.general.movie_default_enabled + movie_default_language = settings.general.movie_default_language + movie_default_hi = settings.general.movie_default_hi + + if apikey_radarr is None: pass else: get_profile_list() @@ -124,11 +124,9 @@ def update_movies(): list_missing_subtitles_movies() logging.debug('BAZARR All movie missing subtitles updated in database.') + def get_profile_list(): - from get_settings import get_radarr_settings - url_radarr = get_radarr_settings()[6] - # url_radarr_short = get_radarr_settings()[7] - apikey_radarr = get_radarr_settings()[4] + apikey_radarr = settings.radarr.apikey # Get profiles data from radarr global profiles_list @@ -148,10 +146,12 @@ def get_profile_list(): for profile in profiles_json.json(): profiles_list.append([profile['id'], profile['language'].capitalize()]) + def profile_id_to_language(id): for profile in profiles_list: if id == profile[0]: return profile[1] + if __name__ == '__main__': update_movies() diff --git a/bazarr/get_providers.py b/bazarr/get_providers.py index 006f129b4..72fce1a13 100644 --- a/bazarr/get_providers.py +++ b/bazarr/get_providers.py @@ -1,16 +1,19 @@ -from get_argv import config_dir - +# coding=utf-8 import sqlite3 import os -from subliminal import provider_manager import collections +from subliminal import provider_manager + +from get_argv import config_dir + + def load_providers(): # Get providers list from subliminal providers_list = sorted(provider_manager.names()) # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() # Remove unsupported providers @@ -34,7 +37,7 @@ def load_providers(): def get_providers(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() enabled_providers = c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall() c.close() @@ -50,7 +53,7 @@ def get_providers(): def get_providers_auth(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() enabled_providers = c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1 AND username is not NULL AND password is not NULL").fetchall() c.close() @@ -64,4 +67,4 @@ def get_providers_auth(): else: providers_auth = None - return providers_auth \ No newline at end of file + return providers_auth diff --git a/bazarr/get_series.py b/bazarr/get_series.py index 74fa6b2c9..ed2ee3e9b 100644 --- a/bazarr/get_series.py +++ b/bazarr/get_series.py @@ -1,22 +1,21 @@ -from get_argv import config_dir - +# coding=utf-8 import os import sqlite3 import requests import logging -from get_settings import get_general_settings +from get_argv import config_dir +from config import settings, url_sonarr from list_subtitles import list_missing_subtitles + def update_series(): - from get_settings import get_sonarr_settings - url_sonarr = get_sonarr_settings()[6] - apikey_sonarr = get_sonarr_settings()[4] - serie_default_enabled = get_general_settings()[15] - serie_default_language = get_general_settings()[16] - serie_default_hi = get_general_settings()[17] - - if apikey_sonarr == None: + apikey_sonarr = settings.sonarr.apikey + serie_default_enabled = settings.general.serie_default_enabled + serie_default_language = settings.general.serie_default_language + serie_default_hi = settings.general.serie_default_hi + + if apikey_sonarr is None: pass else: get_profile_list() @@ -105,11 +104,9 @@ def update_series(): db.commit() db.close() + def get_profile_list(): - from get_settings import get_sonarr_settings - url_sonarr = get_sonarr_settings()[6] - # url_sonarr_short = get_sonarr_settings()[5] - apikey_sonarr = get_sonarr_settings()[4] + apikey_sonarr = settings.sonarr.apikey # Get profiles data from Sonarr error = False @@ -155,6 +152,7 @@ def get_profile_list(): for profile in profiles_json_v3.json(): profiles_list.append([profile['id'], profile['name'].capitalize()]) + def profile_id_to_language(id): for profile in profiles_list: if id == profile[0]: diff --git a/bazarr/get_settings.py b/bazarr/get_settings.py index 038cbb583..5f63ea3a7 100644 --- a/bazarr/get_settings.py +++ b/bazarr/get_settings.py @@ -390,7 +390,6 @@ def get_radarr_settings(): return [ip, port, base_url, ssl, apikey, full_update, url_radarr , url_radarr_short] - def path_replace(path): for path_mapping in path_mappings: if path_mapping[0] in path: diff --git a/bazarr/get_subtitle.py b/bazarr/get_subtitle.py index f4bad4b0e..a36b7a62f 100644 --- a/bazarr/get_subtitle.py +++ b/bazarr/get_subtitle.py @@ -1,30 +1,35 @@ -from get_argv import config_dir - -import os -import sqlite3 +# coding=utf-8 import ast +import cPickle as pickle +import codecs import logging import operator +import os +import sqlite3 import subprocess import time from datetime import datetime, timedelta + from babelfish import Language -from subliminal import region, scan_video, Video, download_best_subtitles, compute_score, save_subtitles, AsyncProviderPool, score, list_subtitles, download_subtitles +from subliminal import AsyncProviderPool, Video, compute_score, download_subtitles, region, save_subtitles, scan_video, \ + score +from subliminal.providers.legendastv import LegendasTVSubtitle from subliminal.subtitle import get_subtitle_path -from get_languages import language_from_alpha3, alpha2_from_alpha3, alpha3_from_alpha2 -from bs4 import UnicodeDammit -from get_settings import get_general_settings, pp_replace, path_replace, path_replace_movie, path_replace_reverse, path_replace_reverse_movie -from list_subtitles import store_subtitles, list_missing_subtitles, store_subtitles_movie, list_missing_subtitles_movies -from utils import history_log, history_log_movie -from notifier import send_notifications, send_notifications_movie -import cPickle as pickle -import codecs + +from get_argv import config_dir +from get_languages import alpha2_from_alpha3, alpha3_from_alpha2, language_from_alpha3 from get_providers import get_providers, get_providers_auth -from subliminal.providers.legendastv import LegendasTVSubtitle +from config import settings +from helper import path_replace, path_replace_movie, path_replace_reverse, \ + path_replace_reverse_movie, pp_replace +from list_subtitles import list_missing_subtitles, list_missing_subtitles_movies, store_subtitles, store_subtitles_movie +from notifier import send_notifications, send_notifications_movie +from utils import history_log, history_log_movie # configure the cache region.configure('dogpile.cache.memory') + def download_subtitle(path, language, hi, providers, providers_auth, sceneName, media_type): logging.debug('BAZARR Searching subtitles for this file: ' + path) if hi == "True": @@ -37,11 +42,11 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, else: language_set.add(Language(language)) - use_scenename = get_general_settings()[9] - minimum_score = get_general_settings()[8] - minimum_score_movie = get_general_settings()[22] - use_postprocessing = get_general_settings()[10] - postprocessing_cmd = get_general_settings()[11] + use_scenename = settings.general.use_scenename + minimum_score = settings.general.minimum_score + minimum_score_movie = settings.general.minimum_score_movie + use_postprocessing = settings.general.use_postprocessing + postprocessing_cmd = settings.general.postprocessing_cmd try: if sceneName == "None" or use_scenename is False: @@ -100,13 +105,13 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, download_result = False for subtitle in subtitles_list: download_result = p.download_subtitle(subtitle) - if download_result == True: + if download_result: logging.debug('BAZARR Subtitles file downloaded from ' + str(subtitle.provider_name) + ' for this file: ' + path) break else: logging.warning('BAZARR Subtitles file skipped from ' + str(subtitle.provider_name) + ' for this file: ' + path + ' because no content was returned by the provider (probably throttled).') continue - if download_result == False: + if not download_result: logging.error('BAZARR Tried to download a subtitles for file: ' + path + " but we weren't able to do it this time (probably being throttled). Going to retry on next search.") return None except Exception as e: @@ -115,9 +120,9 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, else: try: calculated_score = round(float(compute_score(subtitle, video, hearing_impaired=hi)) / max_score * 100, 2) - if used_sceneName == True: + if used_sceneName: video = scan_video(path) - single = get_general_settings()[7] + single = settings.general.single_language if single is True: result = save_subtitles(video, [subtitle], single=True, encoding='utf-8') else: @@ -132,7 +137,7 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, downloaded_language_code3 = result[0].language.alpha3 downloaded_path = get_subtitle_path(path, downloaded_language_code2) logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path) - if used_sceneName == True: + if used_sceneName: message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(calculated_score) + "% using this scene name: " + sceneName else: message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(calculated_score) + "% using filename guessing." @@ -170,6 +175,7 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, return None logging.debug('BAZARR Ended searching subtitles for file: ' + path) + def manual_search(path, language, hi, providers, providers_auth, sceneName, media_type): logging.debug('BAZARR Manually searching subtitles for this file: ' + path) @@ -187,9 +193,9 @@ def manual_search(path, language, hi, providers, providers_auth, sceneName, medi else: language_set.add(Language(lang)) - use_scenename = get_general_settings()[9] - use_postprocessing = get_general_settings()[10] - postprocessing_cmd = get_general_settings()[11] + use_scenename = settings.general.use_scenename + use_postprocessing = settings.general.use_postprocessing + postprocessing_cmd = settings.general.postprocessing_cmd try: if sceneName == "None" or use_scenename is False: @@ -247,6 +253,7 @@ def manual_search(path, language, hi, providers, providers_auth, sceneName, medi logging.debug('BAZARR Ended searching subtitles for this file: ' + path) return(subtitles_dict) + def manual_download_subtitle(path, language, hi, subtitle, provider, providers_auth, sceneName, media_type): logging.debug('BAZARR Manually downloading subtitles for this file: ' + path) if hi == "True": @@ -258,9 +265,9 @@ def manual_download_subtitle(path, language, hi, subtitle, provider, providers_a type_of_score = 360 elif media_type == 'movie': type_of_score = 120 - use_scenename = get_general_settings()[9] - use_postprocessing = get_general_settings()[10] - postprocessing_cmd = get_general_settings()[11] + use_scenename = settings.general.use_scenename + use_postprocessing = settings.general.use_postprocessing + postprocessing_cmd = settings.general.postprocessing_cmd language = alpha3_from_alpha2(language) if language == 'pob': @@ -286,7 +293,7 @@ def manual_download_subtitle(path, language, hi, subtitle, provider, providers_a logging.exception('BAZARR Error downloading subtitles for this file ' + path) return None else: - single = get_general_settings()[7] + single = settings.general.single_language try: score = round(float(compute_score(subtitle, video, hearing_impaired=hi)) / type_of_score * 100, 2) if used_sceneName == True: @@ -341,13 +348,14 @@ def manual_download_subtitle(path, language, hi, subtitle, provider, providers_a return None logging.debug('BAZARR Ended manually downloading subtitles for file: ' + path) + def series_download_subtitles(no): - if get_general_settings()[24] is True: + if settings.general.only_monitored: monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() episodes_details = c_db.execute('SELECT path, missing_subtitles, sonarrEpisodeId, scene_name FROM table_episodes WHERE sonarrSeriesId = ? AND missing_subtitles != "[]"' + monitored_only_query_string, (no,)).fetchall() series_details = c_db.execute("SELECT hearing_impaired FROM table_shows WHERE sonarrSeriesId = ?", (no,)).fetchone() @@ -368,7 +376,7 @@ def series_download_subtitles(no): def movies_download_subtitles(no): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() movie = c_db.execute("SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired FROM table_movies WHERE radarrId = ?", (no,)).fetchone() c_db.close() @@ -387,7 +395,7 @@ def movies_download_subtitles(no): def wanted_download_subtitles(path): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(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.sonarrSeriesId, table_shows.hearing_impaired, table_episodes.scene_name, table_episodes.failedAttempts FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ? AND missing_subtitles != '[]'", (path_replace_reverse(path),)).fetchall() c_db.close() @@ -408,7 +416,7 @@ def wanted_download_subtitles(path): if language not in att: attempt.append([language, time.time()]) - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() c_db.execute('UPDATE table_episodes SET failedAttempts = ? WHERE sonarrEpisodeId = ?', (unicode(attempt), episode[2])) conn_db.commit() @@ -428,7 +436,7 @@ def wanted_download_subtitles(path): def wanted_download_subtitles_movie(path): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() movies_details = c_db.execute("SELECT path, missing_subtitles, radarrId, radarrId, hearing_impaired, sceneName, failedAttempts FROM table_movies WHERE path = ? AND missing_subtitles != '[]'", (path_replace_reverse_movie(path),)).fetchall() c_db.close() @@ -449,7 +457,7 @@ def wanted_download_subtitles_movie(path): if language not in att: attempt.append([language, time.time()]) - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() c_db.execute('UPDATE table_movies SET failedAttempts = ? WHERE radarrId = ?', (unicode(attempt), movie[2])) conn_db.commit() @@ -469,12 +477,12 @@ def wanted_download_subtitles_movie(path): def wanted_search_missing_subtitles(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace) db.create_function("path_substitution_movie", 1, path_replace_movie) c = db.cursor() - if get_general_settings()[24] is True: + if settings.general.only_monitored: monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" @@ -487,13 +495,11 @@ def wanted_search_missing_subtitles(): c.close() - integration = get_general_settings() - - if integration[12] is True: + if settings.general.use_sonarr: for episode in episodes: wanted_download_subtitles(episode[0]) - if integration[13] is True: + if settings.general.use_radarr: for movie in movies: wanted_download_subtitles_movie(movie[0]) @@ -501,7 +507,7 @@ def wanted_search_missing_subtitles(): def search_active(timestamp): - if get_general_settings()[25] is True: + if settings.general.only_monitored: search_deadline = timedelta(weeks=3) search_delta = timedelta(weeks=1) aa = datetime.fromtimestamp(float(timestamp)) diff --git a/bazarr/helper.py b/bazarr/helper.py index f0c6babbc..ecd29c8fa 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -1,4 +1,5 @@ # coding=utf-8 +import ast import os import re @@ -6,7 +7,7 @@ from config import settings def path_replace(path): - for path_mapping in settings.general.path_mappings: + for path_mapping in ast.literal_eval(settings.general.path_mappings): if path_mapping[0] in path: path = path.replace(path_mapping[0], path_mapping[1]) if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): @@ -18,7 +19,7 @@ def path_replace(path): def path_replace_reverse(path): - for path_mapping in settings.general.path_mappings: + for path_mapping in ast.literal_eval(settings.general.path_mappings): if path_mapping[1] in path: path = path.replace(path_mapping[1], path_mapping[0]) if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): @@ -30,7 +31,7 @@ def path_replace_reverse(path): def path_replace_movie(path): - for path_mapping in settings.general.path_mappings_movie: + for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): if path_mapping[0] in path: path = path.replace(path_mapping[0], path_mapping[1]) if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): @@ -42,7 +43,7 @@ def path_replace_movie(path): def path_replace_reverse_movie(path): - for path_mapping in settings.general.path_mappings_movie: + for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): if path_mapping[1] in path: path = path.replace(path_mapping[1], path_mapping[0]) if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): diff --git a/bazarr/init.py b/bazarr/init.py index c94854a82..3e8bc2639 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -1,3 +1,4 @@ +# coding=utf-8 import os import sqlite3 import logging @@ -5,10 +6,11 @@ import time from cork import Cork from configparser import ConfigParser +from check_update import check_releases from get_argv import config_dir # Check if config_dir exist -if os.path.exists(config_dir) is False: +if not os.path.exists(config_dir): # Create config_dir directory tree try: os.mkdir(os.path.join(config_dir)) @@ -17,22 +19,21 @@ if os.path.exists(config_dir) is False: logging.exception("BAZARR The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).") exit(2) -if os.path.exists(os.path.join(config_dir, 'config')) is False: +if not os.path.exists(os.path.join(config_dir, 'config')): os.mkdir(os.path.join(config_dir, 'config')) logging.debug("BAZARR Created config folder") -if os.path.exists(os.path.join(config_dir, 'db')) is False: +if not os.path.exists(os.path.join(config_dir, 'db')): os.mkdir(os.path.join(config_dir, 'db')) logging.debug("BAZARR Created db folder") -if os.path.exists(os.path.join(config_dir, 'log')) is False: +if not os.path.exists(os.path.join(config_dir, 'log')): os.mkdir(os.path.join(config_dir, 'log')) logging.debug("BAZARR Created log folder") if not os.path.exists(os.path.join(config_dir, 'config', 'releases.txt')): - from check_update import check_releases check_releases() logging.debug("BAZARR Created releases file") -config_file = os.path.normpath(os.path.join(config_dir, 'config/config.ini')) +config_file = os.path.normpath(os.path.join(config_dir, 'config', 'config.ini')) cfg = ConfigParser() try: @@ -44,7 +45,7 @@ try: fd.close() # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() # Execute script and commit change to database @@ -81,7 +82,7 @@ if cfg.has_section('general'): with open(config_file, 'w+') as configfile: cfg.write(configfile) -if os.path.exists(os.path.normpath(os.path.join(config_dir, 'config/users.json'))) is False: +if not os.path.exists(os.path.normpath(os.path.join(config_dir, 'config', 'users.json'))): cork = Cork(os.path.normpath(os.path.join(config_dir, 'config')), initialize=True) cork._store.roles[''] = 100 diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index 5b6af3620..4a60a9929 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -1,22 +1,24 @@ -from get_argv import config_dir - +import ast import gc -import os -import enzyme -import babelfish import logging -from subliminal import core +import os import sqlite3 -import ast +from itertools import islice + +import enzyme import langdetect from bs4 import UnicodeDammit -from itertools import islice +from subliminal import core -from get_settings import path_replace_reverse, path_replace, path_replace_reverse_movie, path_replace_movie, get_general_settings +from get_argv import config_dir from get_languages import alpha2_from_alpha3 +from config import settings +from helper import path_replace, path_replace_movie, path_replace_reverse, \ + path_replace_reverse_movie gc.enable() + def store_subtitles(file): logging.debug('BAZARR started subtitles indexing for this file: ' + file) actual_subtitles = [] @@ -29,7 +31,7 @@ def store_subtitles(file): for subtitle_track in mkv.subtitle_tracks: try: - if alpha2_from_alpha3(subtitle_track.language) != None: + if alpha2_from_alpha3(subtitle_track.language) is not None: lang = str(alpha2_from_alpha3(subtitle_track.language)) logging.debug("BAZARR embedded subtitles detected: " + lang) actual_subtitles.append([lang,None]) @@ -51,7 +53,7 @@ def store_subtitles(file): pass else: for subtitle, language in subtitles.iteritems(): - if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: + if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)): logging.debug("BAZARR external subtitles detected: " + "pb") actual_subtitles.append([str("pb"), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) elif str(language) != 'und': @@ -101,7 +103,7 @@ def store_subtitles_movie(file): for subtitle_track in mkv.subtitle_tracks: try: - if alpha2_from_alpha3(subtitle_track.language) != None: + if alpha2_from_alpha3(subtitle_track.language) is not None: lang = str(alpha2_from_alpha3(subtitle_track.language)) logging.debug("BAZARR embedded subtitles detected: " + lang) actual_subtitles.append([lang, None]) @@ -146,7 +148,7 @@ def store_subtitles_movie(file): logging.debug("BAZARR external subtitles detected and analysis guessed this language: " + str(detected_language)) actual_subtitles.append([str(detected_language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() logging.debug("BAZARR storing those languages to DB: " + str(actual_subtitles)) c_db.execute("UPDATE table_movies SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse_movie(file))) @@ -167,30 +169,30 @@ def list_missing_subtitles(*no): query_string = " WHERE table_shows.sonarrSeriesId = " + str(no[0]) except: pass - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() episodes_subtitles = c_db.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.subtitles, table_shows.languages FROM table_episodes INNER JOIN table_shows on table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId" + query_string).fetchall() c_db.close() missing_subtitles_global = [] - use_embedded_subs = get_general_settings()[23] + use_embedded_subs = settings.general.use_embedded_subs for episode_subtitles in episodes_subtitles: actual_subtitles_temp = [] actual_subtitles = [] desired_subtitles = [] missing_subtitles = [] - if episode_subtitles[1] != None: - if use_embedded_subs is True: + if episode_subtitles[1] is not None: + if use_embedded_subs: actual_subtitles = ast.literal_eval(episode_subtitles[1]) else: actual_subtitles_temp = ast.literal_eval(episode_subtitles[1]) for subtitle in actual_subtitles_temp: - if subtitle[1] != None: + if subtitle[1] is not None: actual_subtitles.append(subtitle) - if episode_subtitles[2] != None: + if episode_subtitles[2] is not None: desired_subtitles = ast.literal_eval(episode_subtitles[2]) actual_subtitles_list = [] - if desired_subtitles == None: + if desired_subtitles is None: missing_subtitles_global.append(tuple(['[]', episode_subtitles[0]])) else: for item in actual_subtitles: @@ -201,7 +203,7 @@ def list_missing_subtitles(*no): missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]])) - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() c_db.executemany("UPDATE table_episodes SET missing_subtitles = ? WHERE sonarrEpisodeId = ?", (missing_subtitles_global)) conn_db.commit() @@ -214,30 +216,30 @@ def list_missing_subtitles_movies(*no): query_string = " WHERE table_movies.radarrId = " + str(no[0]) except: pass - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() movies_subtitles = c_db.execute("SELECT radarrId, subtitles, languages FROM table_movies" + query_string).fetchall() c_db.close() missing_subtitles_global = [] - use_embedded_subs = get_general_settings()[23] + use_embedded_subs = settings.general.use_embedded_subs for movie_subtitles in movies_subtitles: actual_subtitles_temp = [] actual_subtitles = [] desired_subtitles = [] missing_subtitles = [] - if movie_subtitles[1] != None: - if use_embedded_subs is True: + if movie_subtitles[1] is not None: + if use_embedded_subs: actual_subtitles = ast.literal_eval(movie_subtitles[1]) else: actual_subtitles_temp = ast.literal_eval(movie_subtitles[1]) for subtitle in actual_subtitles_temp: - if subtitle[1] != None: + if subtitle[1] is not None: actual_subtitles.append(subtitle) - if movie_subtitles[2] != None: + if movie_subtitles[2] is not None: desired_subtitles = ast.literal_eval(movie_subtitles[2]) actual_subtitles_list = [] - if desired_subtitles == None: + if desired_subtitles is None: missing_subtitles_global.append(tuple(['[]', movie_subtitles[0]])) else: for item in actual_subtitles: @@ -248,14 +250,15 @@ def list_missing_subtitles_movies(*no): missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) missing_subtitles_global.append(tuple([str(missing_subtitles), movie_subtitles[0]])) - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() c_db.executemany("UPDATE table_movies SET missing_subtitles = ? WHERE radarrId = ?", (missing_subtitles_global)) conn_db.commit() c_db.close() + def series_full_scan_subtitles(): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() episodes = c_db.execute("SELECT path FROM table_episodes").fetchall() c_db.close() @@ -266,7 +269,7 @@ def series_full_scan_subtitles(): gc.collect() def movies_full_scan_subtitles(): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() movies = c_db.execute("SELECT path FROM table_movies").fetchall() c_db.close() @@ -277,7 +280,7 @@ def movies_full_scan_subtitles(): gc.collect() def series_scan_subtitles(no): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() episodes = c_db.execute("SELECT path FROM table_episodes WHERE sonarrSeriesId = ?", (no,)).fetchall() c_db.close() @@ -289,7 +292,7 @@ def series_scan_subtitles(no): def movies_scan_subtitles(no): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() movies = c_db.execute("SELECT path FROM table_movies WHERE radarrId = ?", (no,)).fetchall() c_db.close() diff --git a/bazarr/logger.py b/bazarr/logger.py index 22a4f7b8a..2e5fd18a8 100644 --- a/bazarr/logger.py +++ b/bazarr/logger.py @@ -162,4 +162,5 @@ class PublicIPFilter(ArgsFilteringFilter): def empty_log(): - fh.doRollover() \ No newline at end of file + fh.doRollover() + \ No newline at end of file diff --git a/bazarr/main.py b/bazarr/main.py index 3d92c3118..b52581b5d 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -11,8 +11,6 @@ reload(sys) sys.setdefaultencoding('utf8') sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../libs/')) -import os -import sys import signal import sqlite3 from init import * @@ -21,21 +19,21 @@ from notifier import update_notifier update_notifier() -from get_settings import get_general_settings, get_proxy_settings import logging from logger import configure_logging, empty_log +from config import settings, url_sonarr, url_radarr, url_radarr_short, url_sonarr_short, base_url -configure_logging(get_general_settings()[4]) +configure_logging(settings.general.debug) import requests -if get_proxy_settings()[0] != 'None': - if get_proxy_settings()[3] != '' and get_proxy_settings()[4] != '': - proxy = get_proxy_settings()[0] + '://' + get_proxy_settings()[3] + ':' + get_proxy_settings()[4] + '@' + get_proxy_settings()[1] + ':' + get_proxy_settings()[2] +if settings.proxy.type != 'None': + if settings.proxy.username != '' and settings.proxy.password != '': + proxy = settings.proxy.type + '://' + settings.proxy.username + ':' + settings.proxy.password + '@' + settings.proxy.url + ':' + settings.proxy.port else: - proxy = get_proxy_settings()[0] + '://' + get_proxy_settings()[1] + ':' + get_proxy_settings()[2] + proxy = settings.proxy.type + '://' + settings.proxy.url + ':' + settings.proxy.port os.environ['HTTP_PROXY'] = str(proxy) os.environ['HTTPS_PROXY'] = str(proxy) - os.environ['NO_PROXY'] = str(get_proxy_settings()[5]) + os.environ['NO_PROXY'] = str(settings.proxy.exclude) from bottle import route, run, template, static_file, request, redirect, response, HTTPError, app, hook import bottle @@ -71,7 +69,7 @@ load_providers() from get_series import * from get_episodes import * -from get_settings import base_url, ip, port, path_replace, path_replace_movie +from helper import path_replace, path_replace_movie if no_update is False: from check_update import check_and_apply_update from list_subtitles import store_subtitles, store_subtitles_movie, series_scan_subtitles, movies_scan_subtitles, list_missing_subtitles, list_missing_subtitles_movies @@ -80,23 +78,22 @@ from utils import history_log, history_log_movie from scheduler import * from notifier import send_notifications, send_notifications_movie + # Reset restart required warning on start -conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) +conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() c.execute("UPDATE system SET configured = 0, updated = 0") conn.commit() c.close() logging.debug('Bazarr version: %s', bazarr_version) -logging.debug('Bazarr branch: %s', get_general_settings()[5]) +logging.debug('Bazarr branch: %s', settings.general.branch) logging.debug('Operating system: %s', platform.platform()) logging.debug('Python version: %s', platform.python_version()) # Load languages in database load_language_in_db() -from get_settings import get_auth_settings - aaa = Cork(os.path.normpath(os.path.join(config_dir, 'config'))) app = app() @@ -109,13 +106,13 @@ session_opts = { 'session.validate_key': True } app = SessionMiddleware(app, session_opts) -login_auth = get_auth_settings()[0] +login_auth = settings.auth.type def custom_auth_basic(check): def decorator(func): def wrapper(*a, **ka): - if get_auth_settings()[0] == 'basic': + if settings.auth.type == 'basic': user, password = request.auth or (None, None) if user is None or not check(user, password): err = HTTPError(401, "Access denied") @@ -128,11 +125,11 @@ def custom_auth_basic(check): return wrapper return decorator + def check_credentials(user, pw): - from get_settings import get_auth_settings - username = get_auth_settings()[1] - password = get_auth_settings()[2] + username = settings.auth.username + password = settings.auth.password if hashlib.md5(pw).hexdigest() == password and user == username: return True return False @@ -178,6 +175,7 @@ def redirect_root(): authorize() redirect (base_url) + @route(base_url + 'shutdown') def shutdown(): try: @@ -189,6 +187,7 @@ def shutdown(): stop_file.close() server.stop() + @route(base_url + 'restart') def restart(): try: @@ -211,19 +210,15 @@ def restart(): @custom_auth_basic(check_credentials) def wizard(): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() settings_languages = c.execute("SELECT * FROM table_settings_languages ORDER BY name").fetchall() settings_providers = c.execute("SELECT * FROM table_settings_providers ORDER BY name").fetchall() c.close() - settings_general = get_general_settings() - settings_sonarr = get_sonarr_settings() - settings_radarr = get_radarr_settings() - - return template('wizard', bazarr_version=bazarr_version, settings_general=settings_general, + return template('wizard', bazarr_version=bazarr_version, settings=settings, settings_languages=settings_languages, settings_providers=settings_providers, - settings_sonarr=settings_sonarr, settings_radarr=settings_radarr, base_url=base_url) + base_url=base_url) @route(base_url + 'save_wizard', method='POST') @@ -231,7 +226,7 @@ def wizard(): def save_wizard(): authorize() - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() settings_general_ip = request.forms.get('settings_general_ip') @@ -278,39 +273,39 @@ def save_wizard(): cfg.set('general', 'port', text_type(settings_general_port)) cfg.set('general', 'base_url', text_type(settings_general_baseurl)) cfg.set('general', 'path_mappings', text_type(settings_general_pathmapping)) - cfg.set('general', 'log_level', text_type(get_general_settings()[4])) - cfg.set('general', 'branch', text_type(get_general_settings()[5])) - cfg.set('general', 'auto_update', text_type(get_general_settings()[6])) + cfg.set('general', 'debug', text_type(settings.general.debug)) + cfg.set('general', 'branch', text_type(settings.general.branch)) + cfg.set('general', 'auto_update', text_type(settings.general.auto_update)) cfg.set('general', 'single_language', text_type(settings_general_single_language)) - cfg.set('general', 'minimum_score', text_type(get_general_settings()[8])) - cfg.set('general', 'use_scenename', text_type(text_type(get_general_settings()[9]))) - cfg.set('general', 'use_postprocessing', text_type(get_general_settings()[10])) - cfg.set('general', 'postprocessing_cmd', text_type(get_general_settings()[11])) + cfg.set('general', 'minimum_score', text_type(settings.general.minimum_score)) + cfg.set('general', 'use_scenename', text_type(text_type(settings.general.use_scenename))) + cfg.set('general', 'use_postprocessing', text_type(settings.general.use_postprocessing)) + cfg.set('general', 'postprocessing_cmd', text_type(settings.general.postprocessing_cmd)) cfg.set('general', 'use_sonarr', text_type(settings_general_use_sonarr)) cfg.set('general', 'use_radarr', text_type(settings_general_use_radarr)) cfg.set('general', 'path_mappings_movie', text_type(settings_general_pathmapping_movie)) - cfg.set('general', 'page_size', text_type(get_general_settings()[21])) - cfg.set('general', 'minimum_score_movie', text_type(get_general_settings()[22])) - cfg.set('general', 'use_embedded_subs', text_type(get_general_settings()[23])) - cfg.set('general', 'only_monitored', text_type(get_general_settings()[24])) + cfg.set('general', 'page_size', text_type(settings.general.page_size)) + cfg.set('general', 'minimum_score_movie', text_type(settings.general.minimum_score_movie)) + cfg.set('general', 'use_embedded_subs', text_type(settings.general.use_embedded_subs)) + cfg.set('general', 'only_monitored', text_type(settings.general.only_monitored)) cfg.set('general', 'adaptive_searching', text_type(settings_general_adaptive_searching)) if not cfg.has_section('proxy'): cfg.add_section('proxy') - cfg.set('proxy', 'type', text_type(get_proxy_settings()[0])) - cfg.set('proxy', 'url', text_type(get_proxy_settings()[1])) - cfg.set('proxy', 'port', text_type(get_proxy_settings()[2])) - cfg.set('proxy', 'username', text_type(get_proxy_settings()[3])) - cfg.set('proxy', 'password', text_type(get_proxy_settings()[4])) - cfg.set('proxy', 'exclude', text_type(get_proxy_settings()[5])) + cfg.set('proxy', 'type', text_type(settings.proxy.type)) + cfg.set('proxy', 'url', text_type(settings.proxy.url)) + cfg.set('proxy', 'port', text_type(settings.proxy.port)) + cfg.set('proxy', 'username', text_type(settings.proxy.username)) + cfg.set('proxy', 'password', text_type(settings.proxy.password)) + cfg.set('proxy', 'exclude', text_type(settings.proxy.exclude)) if not cfg.has_section('auth'): cfg.add_section('auth') - cfg.set('auth', 'type', text_type(get_auth_settings()[0])) - cfg.set('auth', 'username', text_type(get_auth_settings()[1])) - cfg.set('auth', 'password', text_type(get_auth_settings()[2])) + cfg.set('auth', 'type', text_type(settings.auth.type)) + cfg.set('auth', 'username', text_type(settings.auth.username)) + cfg.set('auth', 'password', text_type(settings.auth.password)) settings_sonarr_ip = request.forms.get('settings_sonarr_ip') settings_sonarr_port = request.forms.get('settings_sonarr_port') @@ -330,7 +325,7 @@ def save_wizard(): cfg.set('sonarr', 'base_url', text_type(settings_sonarr_baseurl)) cfg.set('sonarr', 'ssl', text_type(settings_sonarr_ssl)) cfg.set('sonarr', 'apikey', text_type(settings_sonarr_apikey)) - cfg.set('sonarr', 'full_update', text_type(get_sonarr_settings()[5])) + cfg.set('sonarr', 'full_update', text_type(settings.sonarr.full_update)) settings_radarr_ip = request.forms.get('settings_radarr_ip') settings_radarr_port = request.forms.get('settings_radarr_port') @@ -354,7 +349,7 @@ def save_wizard(): cfg.set('radarr', 'base_url', text_type(settings_radarr_baseurl)) cfg.set('radarr', 'ssl', text_type(settings_radarr_ssl)) cfg.set('radarr', 'apikey', text_type(settings_radarr_apikey)) - cfg.set('radarr', 'full_update', text_type(get_radarr_settings()[5])) + cfg.set('radarr', 'full_update', text_type(settings.radarr.full_update)) settings_subliminal_providers = request.forms.getall('settings_subliminal_providers') c.execute("UPDATE table_settings_providers SET enabled = 0") @@ -442,9 +437,7 @@ def download_log(): @custom_auth_basic(check_credentials) def image_proxy(url): authorize() - url_sonarr = get_sonarr_settings()[6] - url_sonarr_short = get_sonarr_settings()[7] - apikey = get_sonarr_settings()[4] + apikey = settings.sonarr.apikey url_image = url_sonarr_short + '/' + url + '?apikey=' + apikey try: image_buffer = BytesIO(requests.get(url_sonarr + '/api' + url_image.split(url_sonarr)[1], timeout=15, verify=False).content) @@ -460,9 +453,7 @@ def image_proxy(url): @custom_auth_basic(check_credentials) def image_proxy_movies(url): authorize() - url_radarr = get_radarr_settings()[6] - url_radarr_short = get_radarr_settings()[7] - apikey = get_radarr_settings()[4] + apikey = settings.radarr.apikey try: url_image = (url_radarr_short + '/' + url + '?apikey=' + apikey).replace('/fanart.jpg', '/banner.jpg') image_buffer = BytesIO(requests.get(url_radarr + '/api' + url_image.split(url_radarr)[1], timeout=15, verify=False).content) @@ -481,9 +472,9 @@ def image_proxy_movies(url): @custom_auth_basic(check_credentials) def redirect_root(): authorize() - if get_general_settings()[12] is True: + if settings.general.use_sonarr is True: redirect(base_url + 'series') - elif get_general_settings()[13] is True: + elif settings.general.use_radarr is True: redirect(base_url + 'movies') elif os.path.exists(os.path.join(config_dir, 'config/config.ini')) is False: redirect(base_url + 'wizard') @@ -495,9 +486,9 @@ def redirect_root(): @custom_auth_basic(check_credentials) def series(): authorize() - single_language = get_general_settings()[7] + single_language = settings.general.single_language - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace) c = db.cursor() @@ -507,11 +498,11 @@ def series(): page = request.GET.page if page == "": page = "1" - page_size = int(get_general_settings()[21]) + page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(missing_count / (page_size + 0.0))) - if get_general_settings()[24] is True: + if settings.general.only_monitored is True: monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" @@ -528,7 +519,7 @@ def series(): output = template('series', bazarr_version=bazarr_version, rows=data, missing_subtitles_list=missing_subtitles_list, total_subtitles_list=total_subtitles_list, languages=languages, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, - single_language=single_language, page_size=page_size, current_port=port) + single_language=single_language, page_size=page_size, current_port=settings.general.port) return output @@ -536,9 +527,9 @@ def series(): @custom_auth_basic(check_credentials) def serieseditor(): authorize() - single_language = get_general_settings()[7] + single_language = settings.general.single_language - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace) c = db.cursor() @@ -551,7 +542,7 @@ def serieseditor(): c.execute("SELECT code2, name FROM table_settings_languages WHERE enabled = 1") languages = c.fetchall() c.close() - output = template('serieseditor', bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, base_url=base_url, single_language=single_language, current_port=port) + output = template('serieseditor', bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, base_url=base_url, single_language=single_language, current_port=settings.general.port) return output @@ -559,18 +550,18 @@ def serieseditor(): @custom_auth_basic(check_credentials) def search_json(query): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() search_list = [] - if get_general_settings()[12] is True: + if settings.general.use_sonarr is True: c.execute("SELECT title, sonarrSeriesId FROM table_shows WHERE title LIKE ? ORDER BY title", ('%' + query + '%',)) series = c.fetchall() for serie in series: search_list.append(dict([('name', serie[0]), ('url', base_url + 'episodes/' + str(serie[1]))])) - if get_general_settings()[13] is True: + if settings.general.use_radarr is True: c.execute("SELECT title, radarrId FROM table_movies WHERE title LIKE ? ORDER BY title", ('%' + query + '%',)) movies = c.fetchall() for movie in movies: @@ -593,7 +584,7 @@ def edit_series(no): else: lang = 'None' - single_language = get_general_settings()[7] + single_language = settings.general.single_language if single_language is True: if str(lang) == "['None']": lang = 'None' @@ -610,7 +601,7 @@ def edit_series(no): else: hi = "False" - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() c.execute("UPDATE table_shows SET languages = ?, hearing_impaired = ? WHERE sonarrSeriesId LIKE ?", (str(lang), hi, no)) conn.commit() @@ -620,6 +611,7 @@ def edit_series(no): redirect(ref) + @route(base_url + 'edit_serieseditor', method='POST') @custom_auth_basic(check_credentials) def edit_serieseditor(): @@ -631,7 +623,7 @@ def edit_serieseditor(): lang = request.forms.getall('languages') hi = request.forms.get('hearing_impaired') - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() for serie in series: @@ -652,14 +644,14 @@ def edit_serieseditor(): redirect(ref) + @route(base_url + 'episodes/', method='GET') @custom_auth_basic(check_credentials) def episodes(no): authorize() - # single_language = get_general_settings()[7] - url_sonarr_short = get_sonarr_settings()[7] + # single_language = settings.general.single_language - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) conn.create_function("path_substitution", 1, path_replace) c = conn.cursor() @@ -676,15 +668,16 @@ def episodes(no): 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=port) + 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) + @route(base_url + 'movies') @custom_auth_basic(check_credentials) def movies(): authorize() - single_language = get_general_settings()[7] + single_language = settings.general.single_language - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace_movie) c = db.cursor() @@ -694,7 +687,7 @@ def movies(): page = request.GET.page if page == "": page = "1" - page_size = int(get_general_settings()[21]) + page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(missing_count / (page_size + 0.0))) @@ -703,16 +696,17 @@ def movies(): c.execute("SELECT code2, name FROM table_settings_languages WHERE enabled = 1") languages = c.fetchall() c.close() - output = template('movies', bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, single_language=single_language, page_size=page_size, current_port=port) + output = template('movies', bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, single_language=single_language, page_size=page_size, current_port=settings.general.port) return output + @route(base_url + 'movieseditor') @custom_auth_basic(check_credentials) def movieseditor(): authorize() - single_language = get_general_settings()[7] + single_language = settings.general.single_language - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace_movie) c = db.cursor() @@ -725,9 +719,10 @@ def movieseditor(): c.execute("SELECT code2, name FROM table_settings_languages WHERE enabled = 1") languages = c.fetchall() c.close() - output = template('movieseditor', bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, base_url=base_url, single_language=single_language, current_port=port) + output = template('movieseditor', bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, base_url=base_url, single_language=single_language, current_port=settings.general.port) return output + @route(base_url + 'edit_movieseditor', method='POST') @custom_auth_basic(check_credentials) def edit_movieseditor(): @@ -739,7 +734,7 @@ def edit_movieseditor(): lang = request.forms.getall('languages') hi = request.forms.get('hearing_impaired') - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() for movie in movies: @@ -760,6 +755,7 @@ def edit_movieseditor(): redirect(ref) + @route(base_url + 'edit_movie/', method='POST') @custom_auth_basic(check_credentials) def edit_movie(no): @@ -782,7 +778,7 @@ def edit_movie(no): else: hi = "False" - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() c.execute("UPDATE table_movies SET languages = ?, hearing_impaired = ? WHERE radarrId LIKE ?", (str(lang), hi, no)) conn.commit() @@ -792,14 +788,14 @@ def edit_movie(no): redirect(ref) + @route(base_url + 'movie/', method='GET') @custom_auth_basic(check_credentials) def movie(no): authorize() - # single_language = get_general_settings()[7] - url_radarr_short = get_radarr_settings()[7] + # single_language = settings.general.single_language - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) conn.create_function("path_substitution", 1, path_replace_movie) c = conn.cursor() @@ -810,7 +806,8 @@ def movie(no): 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=port) + 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) + @route(base_url + 'scan_disk/', method='GET') @custom_auth_basic(check_credentials) @@ -832,6 +829,7 @@ def scan_disk_movie(no): redirect(ref) + @route(base_url + 'search_missing_subtitles/', method='GET') @custom_auth_basic(check_credentials) def search_missing_subtitles(no): @@ -842,6 +840,7 @@ def search_missing_subtitles(no): redirect(ref) + @route(base_url + 'search_missing_subtitles_movie/', method='GET') @custom_auth_basic(check_credentials) def search_missing_subtitles_movie(no): @@ -852,17 +851,19 @@ def search_missing_subtitles_movie(no): redirect(ref) + @route(base_url + 'history') @custom_auth_basic(check_credentials) def history(): authorize() - return template('history', bazarr_version=bazarr_version, base_url=base_url, current_port=port) + return template('history', bazarr_version=bazarr_version, base_url=base_url, current_port=settings.general.port) + @route(base_url + 'historyseries') @custom_auth_basic(check_credentials) def historyseries(): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() c.execute("SELECT COUNT(*) FROM table_history") @@ -871,7 +872,7 @@ def historyseries(): page = request.GET.page if page == "": page = "1" - page_size = int(get_general_settings()[21]) + page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(row_count / (page_size + 0.0))) @@ -894,13 +895,14 @@ def historyseries(): data = c.fetchall() 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=port) + 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) + @route(base_url + 'historymovies') @custom_auth_basic(check_credentials) def historymovies(): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() c.execute("SELECT COUNT(*) FROM table_history_movie") @@ -909,7 +911,7 @@ def historymovies(): page = request.GET.page if page == "": page = "1" - page_size = int(get_general_settings()[21]) + page_size = int(settings.general.page_size) offset = (int(page) - 1) * page_size max_page = int(math.ceil(row_count / (page_size + 0.0))) @@ -932,23 +934,25 @@ def historymovies(): data = c.fetchall() c.close() data = reversed(sorted(data, key=operator.itemgetter(2))) - return template('historymovies', 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=port) + return template('historymovies', 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) + @route(base_url + 'wanted') @custom_auth_basic(check_credentials) def wanted(): authorize() - return template('wanted', bazarr_version=bazarr_version, base_url=base_url, current_port=port) + return template('wanted', bazarr_version=bazarr_version, base_url=base_url, current_port=settings.general.port) + @route(base_url + 'wantedseries') @custom_auth_basic(check_credentials) def wantedseries(): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace) c = db.cursor() - if get_general_settings()[24] is True: + if settings.general.only_monitored is True: monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" @@ -959,24 +963,25 @@ def wantedseries(): page = request.GET.page if page == "": page = "1" - page_size = int(get_general_settings()[21]) + 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,)) data = c.fetchall() c.close() - return template('wantedseries', bazarr_version=bazarr_version, rows=data, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, page_size=page_size, current_port=port) + return template('wantedseries', bazarr_version=bazarr_version, rows=data, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, page_size=page_size, current_port=settings.general.port) + @route(base_url + 'wantedmovies') @custom_auth_basic(check_credentials) def wantedmovies(): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) db.create_function("path_substitution", 1, path_replace_movie) c = db.cursor() - if get_general_settings()[24] is True: + if settings.general.only_monitored is True: monitored_only_query_string = ' AND monitored = "True"' else: monitored_only_query_string = "" @@ -987,14 +992,15 @@ def wantedmovies(): page = request.GET.page if page == "": page = "1" - page_size = int(get_general_settings()[21]) + 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,)) data = c.fetchall() c.close() - return template('wantedmovies', bazarr_version=bazarr_version, rows=data, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, page_size=page_size, current_port=port) + return template('wantedmovies', bazarr_version=bazarr_version, rows=data, missing_count=missing_count, page=page, max_page=max_page, base_url=base_url, page_size=page_size, current_port=settings.general.port) + @route(base_url + 'wanted_search_missing_subtitles') @custom_auth_basic(check_credentials) @@ -1006,11 +1012,12 @@ def wanted_search_missing_subtitles_list(): redirect(ref) + @route(base_url + 'settings') @custom_auth_basic(check_credentials) -def settings(): +def _settings(): authorize() - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() c.execute("SELECT * FROM table_settings_languages ORDER BY name") settings_languages = c.fetchall() @@ -1020,14 +1027,9 @@ def settings(): settings_notifier = c.fetchall() c.close() - from get_settings import get_general_settings, get_proxy_settings, get_auth_settings, get_radarr_settings, get_sonarr_settings - settings_general = get_general_settings() - settings_proxy = get_proxy_settings() - settings_auth = get_auth_settings() - settings_sonarr = get_sonarr_settings() - settings_radarr = get_radarr_settings() - return template('settings', bazarr_version=bazarr_version, settings_general=settings_general, settings_proxy=settings_proxy, settings_auth=settings_auth, settings_languages=settings_languages, settings_providers=settings_providers, settings_sonarr=settings_sonarr, settings_radarr=settings_radarr, settings_notifier=settings_notifier, base_url=base_url, current_port=port) + 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) + @route(base_url + 'save_settings', method='POST') @custom_auth_basic(check_credentials) @@ -1035,7 +1037,7 @@ def save_settings(): authorize() ref = request.environ['HTTP_REFERER'] - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() settings_general_ip = request.forms.get('settings_general_ip') @@ -1107,9 +1109,7 @@ def save_settings(): settings_general_use_radarr = 'True' settings_page_size = request.forms.get('settings_page_size') - settings_general = get_general_settings() - - before = (unicode(settings_general[0]), int(settings_general[1]), unicode(settings_general[2]), unicode(settings_general[3]), unicode(settings_general[12]), unicode(settings_general[13]), unicode(settings_general[14])) + before = (unicode(settings.general.ip), int(settings.general.port), unicode(settings.general.base_url), unicode(settings.general.path_mappings), unicode(settings.general.use_sonarr), unicode(settings.general.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)) from six import text_type @@ -1141,9 +1141,6 @@ def save_settings(): if after != before: configured() - get_general_settings() - - settings_proxy = get_proxy_settings() if not cfg.has_section('proxy'): cfg.add_section('proxy') @@ -1155,7 +1152,7 @@ def save_settings(): settings_proxy_password = request.forms.get('settings_proxy_password') settings_proxy_exclude = request.forms.get('settings_proxy_exclude') - before_proxy_password = (unicode(settings_proxy[0]), unicode(settings_proxy[5])) + before_proxy_password = (unicode(settings.proxy.type), unicode(settings.proxy.exclude)) if before_proxy_password[0] != settings_proxy_type: configured() if before_proxy_password[1] == settings_proxy_password: @@ -1172,15 +1169,13 @@ def save_settings(): cfg.set('proxy', 'password', text_type(settings_proxy_password)) cfg.set('proxy', 'exclude', text_type(settings_proxy_exclude)) - settings_auth = get_auth_settings() - 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 get_auth_settings()[0] != settings_auth_type: + if settings.auth.type != settings_auth_type: configured() - if settings_auth[2] == settings_auth_password: + if settings.auth.password == settings_auth_password: cfg.set('auth', 'type', text_type(settings_auth_type)) cfg.set('auth', 'username', text_type(settings_auth_username)) else: @@ -1204,7 +1199,7 @@ def save_settings(): else: aaa._beaker_session.delete() else: - if settings_auth[2] != settings_auth_password: + if settings.auth.password != settings_auth_password: aaa.user(settings_auth_username).update(role='', pwd=settings_auth_password) if settings_auth_type == 'basic' or settings_auth_type == 'None': pass @@ -1308,7 +1303,7 @@ def save_settings(): with open(config_file, 'wb') as f: cfg.write(f) - configure_logging(get_general_settings()[4]) + configure_logging(settings.general.debug) notifiers = c.execute("SELECT * FROM table_settings_notifier ORDER BY name").fetchall() for notifier in notifiers: @@ -1334,6 +1329,7 @@ def save_settings(): redirect(ref) + @route(base_url + 'check_update') @custom_auth_basic(check_credentials) def check_update(): @@ -1345,10 +1341,12 @@ def check_update(): redirect(ref) + @route(base_url + 'system') @custom_auth_basic(check_credentials) def system(): authorize() + def get_time_from_interval(interval): interval_clean = interval.split('[') interval_clean = interval_clean[1][:-1] @@ -1390,6 +1388,7 @@ def system(): return text + def get_time_from_cron(cron): text = "at " hour = str(cron[5]) @@ -1427,7 +1426,6 @@ def system(): return text - task_list = [] for job in scheduler.get_jobs(): if job.next_run_time is not None: @@ -1445,16 +1443,15 @@ def system(): for i, l in enumerate(f, 1): pass row_count = i - page_size = int(get_general_settings()[21]) + page_size = int(settings.general.page_size) max_page = int(math.ceil(row_count / (page_size + 0.0))) with open(os.path.join(config_dir, 'config', 'releases.txt'), 'r') as f: releases = ast.literal_eval(f.read()) import platform - use_sonarr = get_general_settings()[12] - url_sonarr = get_sonarr_settings()[6] - apikey_sonarr = get_sonarr_settings()[4] + use_sonarr = settings.general.use_sonarr + apikey_sonarr = settings.sonarr.apikey sv = url_sonarr + "/api/system/status?apikey=" + apikey_sonarr sonarr_version = '' if use_sonarr: @@ -1463,9 +1460,8 @@ def system(): except: pass - use_radarr = get_general_settings()[13] - url_radarr = get_radarr_settings()[6] - apikey_radarr = get_radarr_settings()[4] + use_radarr = settings.general.use_radarr + apikey_radarr = settings.radarr.apikey rv = url_radarr + "/api/system/status?apikey=" + apikey_radarr radarr_version = '' if use_radarr: @@ -1480,14 +1476,14 @@ def system(): operating_system=platform.platform(), python_version=platform.python_version(), config_dir=config_dir, bazarr_dir=os.path.normcase(os.getcwd()), base_url=base_url, task_list=task_list, row_count=row_count, max_page=max_page, page_size=page_size, - releases=releases, current_port=port) + releases=releases, current_port=settings.general.port) @route(base_url + 'logs/') @custom_auth_basic(check_credentials) def get_logs(page): authorize() - page_size = int(get_general_settings()[21]) + page_size = int(settings.general.page_size) begin = (page * page_size) - page_size end = (page * page_size) - 1 logs_complete = [] @@ -1495,7 +1491,8 @@ def get_logs(page): logs_complete.append(line.rstrip()) logs = logs_complete[begin:end] - return template('logs', logs=logs, base_url=base_url, current_port=port) + return template('logs', logs=logs, base_url=base_url, current_port=settings.general.port) + @route(base_url + 'execute/') @custom_auth_basic(check_credentials) @@ -1575,6 +1572,7 @@ def get_subtitle(): except OSError: pass + @route(base_url + 'manual_search', method='POST') @custom_auth_basic(check_credentials) def manual_search_json(): @@ -1592,6 +1590,7 @@ def manual_search_json(): data = manual_search(episodePath, language, hi, providers_list, providers_auth, sceneName, 'series') return dict(data=data) + @route(base_url + 'manual_get_subtitle', method='POST') @custom_auth_basic(check_credentials) def manual_get_subtitle(): @@ -1621,6 +1620,7 @@ def manual_get_subtitle(): except OSError: pass + @route(base_url + 'get_subtitle_movie', method='POST') @custom_auth_basic(check_credentials) def get_subtitle_movie(): @@ -1648,6 +1648,7 @@ def get_subtitle_movie(): except OSError: pass + @route(base_url + 'manual_search_movie', method='POST') @custom_auth_basic(check_credentials) def manual_search_movie_json(): @@ -1665,6 +1666,7 @@ def manual_search_movie_json(): data = manual_search(moviePath, language, hi, providers_list, providers_auth, sceneName, 'movie') return dict(data=data) + @route(base_url + 'manual_get_subtitle_movie', method='POST') @custom_auth_basic(check_credentials) def manual_get_subtitle_movie(): @@ -1693,45 +1695,51 @@ def manual_get_subtitle_movie(): except OSError: pass + def configured(): - conn = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = conn.cursor() c.execute("UPDATE system SET configured = 1") conn.commit() c.close() + @route(base_url + 'api/series/wanted') def api_wanted(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute("SELECT table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_episodes.missing_subtitles 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").fetchall() c.close() return dict(subtitles=data) + @route(base_url + 'api/series/history') def api_history(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute("SELECT table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, strftime('%Y-%m-%d', datetime(table_history.timestamp, 'unixepoch')), table_history.description FROM table_history INNER JOIN table_shows on table_shows.sonarrSeriesId = table_history.sonarrSeriesId INNER JOIN table_episodes on table_episodes.sonarrEpisodeId = table_history.sonarrEpisodeId WHERE table_history.action = '1' ORDER BY id DESC").fetchall() c.close() return dict(subtitles=data) + @route(base_url + 'api/movies/wanted') def api_wanted(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute("SELECT table_movies.title, table_movies.missing_subtitles FROM table_movies WHERE table_movies.missing_subtitles != '[]' ORDER BY table_movies._rowid_ DESC").fetchall() c.close() return dict(subtitles=data) + @route(base_url + 'api/movies/history') def api_history(): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute("SELECT table_movies.title, strftime('%Y-%m-%d', datetime(table_history_movie.timestamp, 'unixepoch')), table_history_movie.description FROM table_history_movie INNER JOIN table_movies on table_movies.radarrId = table_history_movie.radarrId WHERE table_history_movie.action = '1' ORDER BY id DESC").fetchall() c.close() return dict(subtitles=data) + @route(base_url + 'test_url//', method='GET') @custom_auth_basic(check_credentials) def test_url(protocol, url): @@ -1761,9 +1769,9 @@ import warnings # Mute DeprecationWarning warnings.simplefilter("ignore", DeprecationWarning) -server = CherryPyWSGIServer((str(ip), int(port)), app) +server = CherryPyWSGIServer((str(settings.general.ip), int(settings.general.port)), app) try: - logging.info('BAZARR is started and waiting for request on http://' + str(ip) + ':' + str(port) + str(base_url)) + logging.info('BAZARR is started and waiting for request on http://' + str(settings.general.ip) + ':' + str(settings.general.port) + str(base_url)) # print 'Bazarr is started and waiting for request on http://' + str(ip) + ':' + str(port) + str(base_url) server.start() except KeyboardInterrupt: diff --git a/bazarr/notifier.py b/bazarr/notifier.py index 52d7533c1..3b6f34c11 100644 --- a/bazarr/notifier.py +++ b/bazarr/notifier.py @@ -1,10 +1,11 @@ -from get_argv import config_dir - +# coding=utf-8 import apprise import os import sqlite3 import logging +from get_argv import config_dir + def update_notifier(): # define apprise object @@ -16,7 +17,7 @@ def update_notifier(): notifiers_new = [] notifiers_old = [] - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() notifiers_current = c_db.execute('SELECT name FROM table_settings_notifier').fetchall() for x in results['schemas']: @@ -40,37 +41,41 @@ def update_notifier(): def get_notifier_providers(): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) 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 + def get_series_name(sonarrSeriesId): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() data = c_db.execute('SELECT title FROM table_shows WHERE sonarrSeriesId = ?', (sonarrSeriesId,)).fetchone() c_db.close() return data[0] + def get_episode_name(sonarrEpisodeId): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() 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] + def get_movies_name(radarrId): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + conn_db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() data = c_db.execute('SELECT title FROM table_movies WHERE radarrId = ?', (radarrId,)).fetchone() c_db.close() return data[0] + def send_notifications(sonarrSeriesId, sonarrEpisodeId, message): providers = get_notifier_providers() series = get_series_name(sonarrSeriesId) @@ -101,4 +106,5 @@ def send_notifications_movie(radarrId, message): apobj.notify( title='Bazarr notification', body=movie + ' : ' + message, - ) \ No newline at end of file + ) + \ No newline at end of file diff --git a/bazarr/scheduler.py b/bazarr/scheduler.py index 27f5b6321..a5348ec38 100644 --- a/bazarr/scheduler.py +++ b/bazarr/scheduler.py @@ -1,11 +1,10 @@ from get_argv import no_update - -from get_settings import get_general_settings, automatic, get_radarr_settings, get_sonarr_settings -from get_series import update_series -from get_episodes import update_all_episodes, update_all_movies, sync_episodes +from get_episodes import sync_episodes, update_all_episodes, update_all_movies from get_movies import update_movies -from list_subtitles import store_subtitles +from get_series import update_series +from config import settings from get_subtitle import wanted_search_missing_subtitles + if no_update is False: from check_update import check_and_apply_update, check_releases else: @@ -18,12 +17,10 @@ from datetime import datetime import pytz from tzlocal import get_localzone -integration = get_general_settings() - def sonarr_full_update(): - if integration[12] is True: - full_update = get_sonarr_settings()[5] + if settings.general.use_sonarr: + full_update = settings.sonarr.full_update if full_update == "Daily": scheduler.add_job(update_all_episodes, CronTrigger(hour=4), max_instances=1, coalesce=True, misfire_grace_time=15, id='update_all_episodes', @@ -39,8 +36,8 @@ def sonarr_full_update(): def radarr_full_update(): - if integration[13] is True: - full_update = get_radarr_settings()[5] + if settings.general.use_radarr: + full_update = settings.radarr.full_update if full_update == "Daily": scheduler.add_job(update_all_movies, CronTrigger(hour=5), max_instances=1, coalesce=True, misfire_grace_time=15, id='update_all_movies', name='Update all movies subtitles from disk', replace_existing=True) @@ -64,7 +61,7 @@ else: scheduler = BackgroundScheduler() if no_update is False: - if automatic is True: + if settings.sonarr.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') else: scheduler.add_job(check_and_apply_update, CronTrigger(year='2100'), hour=4, id='update_bazarr', name='Update bazarr from source on Github') @@ -73,19 +70,21 @@ else: scheduler.add_job(check_releases, IntervalTrigger(hours=6), max_instances=1, coalesce=True, misfire_grace_time=15, id='update_release', name='Update release info') -if integration[12] is True: +if settings.general.use_sonarr: scheduler.add_job(update_series, IntervalTrigger(minutes=1), max_instances=1, coalesce=True, misfire_grace_time=15, id='update_series', name='Update series list from Sonarr') scheduler.add_job(sync_episodes, IntervalTrigger(minutes=5), max_instances=1, coalesce=True, misfire_grace_time=15, id='sync_episodes', name='Sync episodes with Sonarr') -if integration[13] is True: +if settings.general.use_radarr: scheduler.add_job(update_movies, IntervalTrigger(minutes=5), max_instances=1, coalesce=True, misfire_grace_time=15, id='update_movies', name='Update movies list from Radarr') -if integration[12] is True or integration[13] is True: +if settings.general.use_sonarr or settings.general.use_radarr: scheduler.add_job(wanted_search_missing_subtitles, IntervalTrigger(hours=3), max_instances=1, coalesce=True, misfire_grace_time=15, id='wanted_search_missing_subtitles', name='Search for wanted subtitles') sonarr_full_update() radarr_full_update() scheduler.start() + def shutdown_scheduler(): - scheduler.shutdown(wait=True) \ No newline at end of file + scheduler.shutdown(wait=True) + \ No newline at end of file diff --git a/bazarr/update_db.py b/bazarr/update_db.py index f9b55e808..e60356803 100644 --- a/bazarr/update_db.py +++ b/bazarr/update_db.py @@ -1,12 +1,15 @@ -from get_argv import config_dir - +# coding=utf-8 import os import sqlite3 +from get_argv import config_dir +from scheduler import execute_now +from config import settings + # Check if database exist -if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) == True: +if os.path.exists(os.path.join(config_dir, 'db', 'bazarr.db')): # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() # Execute tables modifications @@ -70,13 +73,9 @@ if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) == True: except: pass else: - from scheduler import execute_now - from get_settings import get_general_settings - - integration = get_general_settings() - if integration[12] is True: + if settings.general.use_sonarr: execute_now('sync_episodes') - if integration[13] is True: + if settings.general.use_radarr: execute_now('update_movies') try: @@ -85,11 +84,7 @@ if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) == True: except: pass else: - from scheduler import execute_now - from get_settings import get_general_settings - - integration = get_general_settings() - if integration[12] is True: + if settings.general.use_sonarr: execute_now('sync_episodes') try: @@ -98,11 +93,8 @@ if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) == True: except: pass else: - from scheduler import execute_now - from get_settings import get_general_settings - - integration = get_general_settings() - if integration[13] is True: + if settings.general.use_radarr: execute_now('update_movies') - db.close() \ No newline at end of file + db.close() + \ No newline at end of file diff --git a/bazarr/utils.py b/bazarr/utils.py index 10acfebdc..6d5059b68 100644 --- a/bazarr/utils.py +++ b/bazarr/utils.py @@ -1,12 +1,14 @@ -from get_argv import config_dir - +# coding=utf-8 import os import sqlite3 import time +from get_argv import config_dir + + def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() # Get Sonarr API URL from database config table @@ -21,7 +23,7 @@ def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): def history_log_movie(action, radarrId, description): # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() history = c.execute('''INSERT INTO table_history_movie(action, radarrId, timestamp, description) VALUES (?, ?, ?, ?)''', (action, radarrId, time.time(), description)) diff --git a/views/episodes.tpl b/views/episodes.tpl index 8c3addbe7..ef82c2524 100644 --- a/views/episodes.tpl +++ b/views/episodes.tpl @@ -220,9 +220,9 @@ end if missing_languages is not None: from get_subtitle import search_active - from get_settings import get_general_settings + from config import settings for language in missing_languages: - if episode[10] is not None and get_general_settings()[25] and language in episode[10]: + if episode[10] is not None and settings.general.adaptive_searching and language in episode[10]: for lang in ast.literal_eval(episode[10]): if language in lang: if search_active(lang[1]): diff --git a/views/history.tpl b/views/history.tpl index 2a2733fa7..a1e62383f 100644 --- a/views/history.tpl +++ b/views/history.tpl @@ -47,14 +47,12 @@ % include('menu.tpl') - % from get_argv import config_dir - % import os - % from get_settings import get_general_settings + % from config import settings
diff --git a/views/menu.tpl b/views/menu.tpl index 153066a6d..1b008a027 100644 --- a/views/menu.tpl +++ b/views/menu.tpl @@ -24,9 +24,9 @@ % import os % import sqlite3 - % from get_settings import get_general_settings + % from config import settings - %if get_general_settings()[24]: + %if settings.general.only_monitored: % monitored_only_query_string = ' AND monitored = "True"' %else: % monitored_only_query_string = "" @@ -50,13 +50,13 @@