From 75a0a5ace42d0c52f938169c80ab32bbfb7919b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:05:28 -0400 Subject: [PATCH] WIP --- bazarr/create_db.sql | 88 +++++++++++++++++++++++++++++++++++++++++++ bazarr/database.py | 58 +++++++++++++++++++++++++++- bazarr/init.py | 25 +++++++++++- libs/sqlite3worker.py | 2 +- 4 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 bazarr/create_db.sql diff --git a/bazarr/create_db.sql b/bazarr/create_db.sql new file mode 100644 index 000000000..dc2188e44 --- /dev/null +++ b/bazarr/create_db.sql @@ -0,0 +1,88 @@ +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 e6142b34a..94fc46cba 100644 --- a/bazarr/database.py +++ b/bazarr/database.py @@ -5,6 +5,30 @@ from six import string_types from get_args import args from helper import path_replace, path_replace_movie, path_replace_reverse, path_replace_reverse_movie + +def db_init(): + import sqlite3 + import os + import logging + + from get_args import args + + if not os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): + # 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') + + database = Sqlite3Worker(os.path.join(args.config_dir, 'db', 'bazarr.db'), max_queue_size=256, as_dict=True) @@ -57,4 +81,36 @@ class SqliteDictPathMapper: return path_replace(values_dict) -dict_mapper = SqliteDictPathMapper() \ No newline at end of file +dict_mapper = SqliteDictPathMapper() + + +def db_upgrade(): + columnToAdd = [ + ['table_shows', 'year', 'text'], + ['table_shows', 'alternateTitles', 'text'], + ['table_shows', 'forced', 'text', 'False'], + ['table_episodes', 'format', 'text'], + ['table_episodes', 'resolution', 'text'], + ['table_episodes', 'video_codec', 'text'], + ['table_episodes', 'audio_codec', 'text'], + ['table_episodes', 'episode_file_id', 'integer'], + ['table_movies', 'sortTitle', 'text'], + ['table_movies', 'year', 'text'], + ['table_movies', 'alternativeTitles', 'text'], + ['table_movies', 'format', 'text'], + ['table_movies', 'resolution', 'text'], + ['table_movies', 'video_codec', 'text'], + ['table_movies', 'audio_codec', 'text'], + ['table_movies', 'imdbId', 'text'], + ['table_movies', 'forced', 'text', 'False'], + ['table_movies', 'movie_file_id', 'integer'] + ] + + for column in columnToAdd: + try: + if len(column) == 3: + database.execute('''ALTER TABLE {0} ADD COLUMN "{1}" "{2}"'''.format(column[0], column[1], column[2])) + else: + database.execute('''ALTER TABLE {0} ADD COLUMN "{1}" "{2}" DEFAULT "{3}"'''.format(column[0], column[1], column[2], column[3])) + except: + pass diff --git a/bazarr/init.py b/bazarr/init.py index 552848bb7..98e4c053c 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -8,9 +8,7 @@ import rarfile from cork import Cork from ConfigParser2 import ConfigParser from config import settings -from check_update import check_releases from get_args import args -from utils import get_binary from dogpile.cache.region import register_backend as register_cache_backend import subliminal @@ -54,6 +52,27 @@ if not os.path.exists(os.path.join(args.config_dir, 'cache')): os.mkdir(os.path.join(args.config_dir, 'cache')) logging.debug("BAZARR Created cache folder") +# create database file +if not os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): + import sqlite3 + # 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') + +# upgrade database schema +from database import db_upgrade +db_upgrade() + # Configure dogpile file caching for Subliminal request register_cache_backend("subzero.cache.file", "subzero.cache_backends.file", "SZFileBackend") subliminal.region.configure('subzero.cache.file', expiration_time=datetime.timedelta(days=30), @@ -61,6 +80,7 @@ subliminal.region.configure('subzero.cache.file', expiration_time=datetime.timed subliminal.region.backend.sync() if not os.path.exists(os.path.join(args.config_dir, 'config', 'releases.txt')): + from check_update import check_releases check_releases() logging.debug("BAZARR Created releases file") @@ -87,6 +107,7 @@ if not os.path.exists(os.path.normpath(os.path.join(args.config_dir, 'config', ' def init_binaries(): + from utils import get_binary exe = get_binary("unrar") rarfile.UNRAR_TOOL = exe diff --git a/libs/sqlite3worker.py b/libs/sqlite3worker.py index 2247f7adb..ab7c1b8ce 100644 --- a/libs/sqlite3worker.py +++ b/libs/sqlite3worker.py @@ -144,7 +144,7 @@ class Sqlite3Worker(threading.Thread): def close(self): """Close down the thread and close the sqlite3 database file.""" self.exit_set = True - self.sql_queue.put((self.exit_token, "", ""), timeout=5) + self.sql_queue.put((self.exit_token, "", "", ""), timeout=5) # Sleep and check that the thread is done before returning. while self.thread_running: time.sleep(.01) # Don't kill the CPU waiting.