diff --git a/bazarr/check_update.py b/bazarr/check_update.py index 89dfb74a9..1e21cb115 100644 --- a/bazarr/check_update.py +++ b/bazarr/check_update.py @@ -9,6 +9,7 @@ import tarfile from get_args import args from config import settings, bazarr_url from queueconfig import notifications +from database import System if not args.no_update and not args.release_update: import git @@ -299,8 +300,4 @@ def updated(restart=True): logging.info('BAZARR Restart failed, please restart Bazarr manualy') updated(restart=False) else: - conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = conn.cursor() - c.execute("UPDATE system SET updated = 1") - conn.commit() - c.close() + System.update({System.updated: 1}).execute() diff --git a/bazarr/create_db.sql b/bazarr/create_db.sql deleted file mode 100644 index dc2188e44..000000000 --- a/bazarr/create_db.sql +++ /dev/null @@ -1,88 +0,0 @@ -BEGIN TRANSACTION; -CREATE TABLE "table_shows" ( - `tvdbId` INTEGER NOT NULL UNIQUE, - `title` TEXT NOT NULL, - `path` TEXT NOT NULL UNIQUE, - `languages` TEXT, - `hearing_impaired` TEXT, - `sonarrSeriesId` INTEGER NOT NULL UNIQUE, - `overview` TEXT, - `poster` TEXT, - `fanart` TEXT, - `audio_language` "text", - `sortTitle` "text", - PRIMARY KEY(`tvdbId`) -); -CREATE TABLE "table_settings_providers" ( - `name` TEXT NOT NULL UNIQUE, - `enabled` INTEGER, - `username` "text", - `password` "text", - PRIMARY KEY(`name`) -); -CREATE TABLE "table_settings_notifier" ( - `name` TEXT, - `url` TEXT, - `enabled` INTEGER, - PRIMARY KEY(`name`) -); -CREATE TABLE "table_settings_languages" ( - `code3` TEXT NOT NULL UNIQUE, - `code2` TEXT, - `name` TEXT NOT NULL, - `enabled` INTEGER, - `code3b` TEXT, - PRIMARY KEY(`code3`) -); -CREATE TABLE "table_history" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `action` INTEGER NOT NULL, - `sonarrSeriesId` INTEGER NOT NULL, - `sonarrEpisodeId` INTEGER NOT NULL, - `timestamp` INTEGER NOT NULL, - `description` TEXT NOT NULL -); -CREATE TABLE "table_episodes" ( - `sonarrSeriesId` INTEGER NOT NULL, - `sonarrEpisodeId` INTEGER NOT NULL UNIQUE, - `title` TEXT NOT NULL, - `path` TEXT NOT NULL, - `season` INTEGER NOT NULL, - `episode` INTEGER NOT NULL, - `subtitles` TEXT, - `missing_subtitles` TEXT, - `scene_name` TEXT, - `monitored` TEXT, - `failedAttempts` "text" -); -CREATE TABLE "table_movies" ( - `tmdbId` TEXT NOT NULL UNIQUE, - `title` TEXT NOT NULL, - `path` TEXT NOT NULL UNIQUE, - `languages` TEXT, - `subtitles` TEXT, - `missing_subtitles` TEXT, - `hearing_impaired` TEXT, - `radarrId` INTEGER NOT NULL UNIQUE, - `overview` TEXT, - `poster` TEXT, - `fanart` TEXT, - `audio_language` "text", - `sceneName` TEXT, - `monitored` TEXT, - `failedAttempts` "text", - PRIMARY KEY(`tmdbId`) -); -CREATE TABLE "table_history_movie" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `action` INTEGER NOT NULL, - `radarrId` INTEGER NOT NULL, - `timestamp` INTEGER NOT NULL, - `description` TEXT NOT NULL -); -CREATE TABLE "system" ( - `configured` TEXT, - `updated` TEXT -); -INSERT INTO `system` (configured, updated) VALUES ('0', '0'); -COMMIT; diff --git a/bazarr/database.py b/bazarr/database.py index 3af75c909..17e9fc619 100644 --- a/bazarr/database.py +++ b/bazarr/database.py @@ -4,13 +4,14 @@ import atexit from get_args import args from peewee import * from playhouse.sqliteq import SqliteQueueDatabase +from playhouse.reflection import generate_models from helper import path_replace, path_replace_movie, path_replace_reverse, path_replace_reverse_movie database = SqliteQueueDatabase( os.path.join(args.config_dir, 'db', 'bazarr.db'), use_gevent=False, # Use the standard library "threading" module. - autostart=True, # The worker thread now must be started manually. + autostart=False, # The worker thread now must be started manually. queue_max_size=256, # Max. # of pending writes that can accumulate. results_timeout=30.0) # Max. time to wait for query to be executed. @@ -33,15 +34,6 @@ class BaseModel(Model): database = database -class SqliteSequence(BaseModel): - name = BareField(null=True) - seq = BareField(null=True) - - class Meta: - table_name = 'sqlite_sequence' - primary_key = False - - class System(BaseModel): configured = TextField(null=True) updated = TextField(null=True) @@ -178,4 +170,23 @@ class TableSettingsNotifier(BaseModel): @atexit.register def _stop_worker_threads(): - database.stop() \ No newline at end of file + database.stop() + + +def database_init(): + database.start() + database.connect() + + models_list = [TableShows, TableEpisodes, TableMovies, TableHistory, TableHistoryMovie, TableSettingsLanguages, + TableSettingsNotifier, System] + + database.create_tables(models_list, safe=True) + + # Insert default values + if System.select().count() == 0: + System.insert( + { + System.updated: 0, + System.configured: 0 + } + ).execute() diff --git a/bazarr/init.py b/bazarr/init.py index b57907036..e0b6f2b83 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -68,115 +68,6 @@ if not os.path.exists(os.path.join(args.config_dir, 'config', 'releases.txt')): config_file = os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini')) cfg = ConfigParser() -try: - # Get SQL script from file - fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r') - script = fd.read() - - # Close SQL script file - fd.close() - - # Open database connection - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - - # Execute script and commit change to database - c.executescript(script) - - # Close database connection - db.close() - - logging.info('BAZARR Database created successfully') -except: - pass - -# Remove unused settings -try: - with open(config_file, 'r') as f: - cfg.read_file(f) -except Exception: - pass -if cfg.has_section('auth'): - if cfg.has_option('auth', 'enabled'): - enabled = cfg.getboolean('auth', 'enabled') - if enabled: - cfg.set('auth', 'type', 'basic') - else: - cfg.set('auth', 'type', 'None') - cfg.remove_option('auth', 'enabled') - with open(config_file, 'w+') as configfile: - cfg.write(configfile) - -if cfg.has_section('general'): - if cfg.has_option('general', 'log_level'): - cfg.remove_option('general', 'log_level') - cfg.set('general', 'debug', 'False') - with open(config_file, 'w+') as configfile: - cfg.write(configfile) - - if cfg.has_option('general', 'only_monitored'): - only_monitored = cfg.get('general', 'only_monitored') - cfg.set('sonarr', 'only_monitored', str(only_monitored)) - cfg.set('radarr', 'only_monitored', str(only_monitored)) - cfg.remove_option('general', 'only_monitored') - with open(config_file, 'w+') as configfile: - cfg.write(configfile) - -# Move providers settings from DB to config file -try: - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - enabled_providers = c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall() - settings_providers = c.execute("SELECT * FROM table_settings_providers").fetchall() - c.execute("DROP TABLE table_settings_providers") - db.close() - - providers_list = [] - if enabled_providers: - for provider in enabled_providers: - providers_list.append(provider[0]) - else: - providers_list = None - - if settings_providers: - for provider in settings_providers: - if provider[0] == 'opensubtitles': - settings.opensubtitles.username = provider[2] - settings.opensubtitles.password = provider[3] - elif provider[0] == 'addic7ed': - settings.addic7ed.username = provider[2] - settings.addic7ed.password = provider[3] - 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) -except: - pass - -if settings.general.throtteled_providers == '' or None: - settings.general.throtteled_providers = '{}' - with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: - settings.write(handle) - -if not os.path.exists(os.path.normpath(os.path.join(args.config_dir, 'config', 'users.json'))): - cork = Cork(os.path.normpath(os.path.join(args.config_dir, 'config')), initialize=True) - - cork._store.roles[''] = 100 - cork._store.save_roles() - - tstamp = str(time.time()) - username = password = '' - cork._store.users[username] = { - 'role': '', - 'hash': cork._hash(username, password), - 'email_addr': username, - 'desc': username, - 'creation_date': tstamp - } - cork._store.save_users() def init_binaries(): diff --git a/bazarr/main.py b/bazarr/main.py index 960f02232..f0cdd8607 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -22,9 +22,12 @@ import operator from get_args import args from init import * -from update_db import * -from database import TableEpisodes, TableShows, TableMovies, TableHistory, TableHistoryMovie, TableSettingsLanguages, \ - TableSettingsNotifier, System +from database import database_init, TableEpisodes, TableShows, TableMovies, TableHistory, TableHistoryMovie, \ + TableSettingsLanguages, TableSettingsNotifier, System + +# Initiate database +database_init() + from notifier import update_notifier from logger import configure_logging, empty_log @@ -54,7 +57,6 @@ from subliminal_patch.extensions import provider_registry as provider_manager reload(sys) sys.setdefaultencoding('utf8') gc.enable() -update_notifier() os.environ["SZ_USER_AGENT"] = "Bazarr/1" os.environ["BAZARR_VERSION"] = bazarr_version @@ -106,6 +108,9 @@ app = SessionMiddleware(app, session_opts) login_auth = settings.auth.type +update_notifier() + + def custom_auth_basic(check): def decorator(func): def wrapper(*a, **ka): @@ -998,11 +1003,11 @@ def edit_movie(no): TableMovies.languages: str(lang), TableMovies.hearing_impaired: hi, TableMovies.forced: forced - }.where( - TableMovies.radarr_id % no - ).execute() - ) - + } + ).where( + TableMovies.radarr_id % no + ).execute() + list_missing_subtitles_movies(no) redirect(ref) diff --git a/bazarr/update_db.py b/bazarr/update_db.py deleted file mode 100644 index 4ce8917e8..000000000 --- a/bazarr/update_db.py +++ /dev/null @@ -1,157 +0,0 @@ -# coding=utf-8 - -import os -import sqlite3 - -from get_args import args -from scheduler import execute_now -from config import settings - -# Check if database exist -if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): - # Open database connection - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - - # Execute tables modifications - try: - c.execute('alter table table_settings_providers add column "username" "text"') - except: - pass - else: - c.execute('UPDATE table_settings_providers SET username=""') - - try: - c.execute('alter table table_settings_providers add column "password" "text"') - except: - pass - else: - c.execute('UPDATE table_settings_providers SET password=""') - - try: - c.execute('alter table table_shows add column "audio_language" "text"') - except: - pass - - try: - c.execute('alter table table_shows add column "sortTitle" "text"') - except: - pass - - try: - c.execute('alter table table_movies add column "sortTitle" "text"') - except: - pass - - try: - rows = c.execute('SELECT name FROM table_settings_notifier WHERE name = "Kodi/XBMC"').fetchall() - if len(rows) == 0: - providers = [['KODI', 'Kodi/XBMC'], ['Windows', 'Windows Notification'], ['Super Toasty', 'Toasty'], - ['PushBullet', 'Pushbullet'], ['Mattermost', 'MatterMost']] - for provider_old, provider_new in providers: - c.execute('UPDATE table_settings_notifier SET name=? WHERE name=?', (provider_new, provider_old)) - except: - pass - - try: - c.execute('alter table table_movies add column "failedAttempts" "text"') - c.execute('alter table table_episodes add column "failedAttempts" "text"') - except: - pass - - try: - c.execute('alter table table_settings_languages add column "code3b" "text"') - except: - pass - - # Commit change to db - db.commit() - - try: - c.execute('alter table table_episodes add column "scene_name" TEXT') - db.commit() - except: - pass - else: - if settings.general.getboolean('use_sonarr'): - execute_now('sync_episodes') - if settings.general.getboolean('use_radarr'): - execute_now('update_movies') - - try: - c.execute('alter table table_episodes add column "monitored" TEXT') - db.commit() - except: - pass - else: - if settings.general.getboolean('use_sonarr'): - execute_now('sync_episodes') - - try: - c.execute('alter table table_movies add column "monitored" TEXT') - db.commit() - except: - pass - 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"') - c.execute('alter table table_movies add column "resolution" "text"') - c.execute('alter table table_movies add column "video_codec" "text"') - c.execute('alter table table_movies add column "audio_codec" "text"') - c.execute('alter table table_movies add column "imdbId" "text"') - db.commit() - except: - pass - else: - if settings.general.getboolean('use_sonarr'): - execute_now('update_series') - 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: - pass - else: - c.execute('UPDATE table_shows SET forced="False"') - db.commit() - - try: - c.execute('alter table table_movies add column "forced" "text"') - db.commit() - except: - pass - else: - c.execute('UPDATE table_movies SET forced="False"') - db.commit() - - db.close()