bazarr/bazarr/database.py

137 lines
5.0 KiB
Python
Raw Normal View History

import os
2019-10-22 02:13:37 +00:00
from sqlite3worker import Sqlite3Worker
from get_args import args
2020-05-19 13:27:13 +00:00
from helper import path_mappings
2019-10-28 04:05:28 +00:00
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')
2019-10-25 02:35:04 +00:00
database = Sqlite3Worker(os.path.join(args.config_dir, 'db', 'bazarr.db'), max_queue_size=256, as_dict=True)
2019-10-24 16:14:21 +00:00
class SqliteDictConverter:
def __init__(self):
2019-10-28 00:45:15 +00:00
self.keys_insert = tuple()
self.keys_update = tuple()
self.values = tuple()
self.question_marks = tuple()
2019-10-26 18:52:22 +00:00
def convert(self, values_dict):
2019-10-24 16:14:21 +00:00
if type(values_dict) is dict:
2019-10-28 10:45:42 +00:00
self.keys_insert = tuple()
self.keys_update = tuple()
self.values = tuple()
self.question_marks = tuple()
2019-10-28 00:45:15 +00:00
temp_keys = list()
temp_values = list()
for item in values_dict.items():
temp_keys.append(item[0])
temp_values.append(item[1])
self.keys_insert = ','.join(temp_keys)
self.keys_update = ','.join([k + '=?' for k in temp_keys])
self.values = tuple(temp_values)
self.question_marks = ','.join(list('?'*len(values_dict)))
2019-10-24 16:14:21 +00:00
return self
else:
pass
dict_converter = SqliteDictConverter()
2019-10-26 18:52:22 +00:00
class SqliteDictPathMapper:
def __init__(self):
pass
def path_replace(self, values_dict):
2019-10-27 01:16:59 +00:00
if type(values_dict) is list:
for item in values_dict:
2020-05-19 13:27:13 +00:00
item['path'] = path_mappings.path_replace(item['path'])
2019-10-27 01:16:59 +00:00
elif type(values_dict) is dict:
2020-05-19 13:27:13 +00:00
values_dict['path'] = path_mappings.path_replace(values_dict['path'])
2019-10-27 03:17:14 +00:00
else:
2020-05-19 13:27:13 +00:00
return path_mappings.path_replace(values_dict)
2019-10-26 18:52:22 +00:00
def path_replace_movie(self, values_dict):
2019-10-27 01:16:59 +00:00
if type(values_dict) is list:
for item in values_dict:
2020-05-19 13:27:13 +00:00
item['path'] = path_mappings.path_replace_movie(item['path'])
2019-10-27 01:16:59 +00:00
elif type(values_dict) is dict:
2020-05-19 13:27:13 +00:00
values_dict['path'] = path_mappings.path_replace_movie(values_dict['path'])
2019-10-27 03:17:14 +00:00
else:
2020-05-19 13:27:13 +00:00
return path_mappings.path_replace_movie(values_dict)
2019-10-26 18:52:22 +00:00
2019-10-28 04:05:28 +00:00
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'],
2019-10-28 04:20:59 +00:00
['table_movies', 'movie_file_id', 'integer'],
['table_history', 'video_path', 'text'],
['table_history', 'language', 'text'],
['table_history', 'provider', 'text'],
['table_history', 'score', 'text'],
['table_history_movie', 'video_path', 'text'],
['table_history_movie', 'language', 'text'],
['table_history_movie', 'provider', 'text'],
['table_history_movie', 'score', 'text']
2019-10-28 04:05:28 +00:00
]
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
2020-03-25 00:59:57 +00:00
# Fix null languages, hearing-impaired and forced for series and movies.
database.execute("UPDATE table_shows SET languages = '[]' WHERE languages is null")
database.execute("UPDATE table_shows SET hearing_impaired = 'False' WHERE hearing_impaired is null")
database.execute("UPDATE table_shows SET forced = 'False' WHERE forced is null")
database.execute("UPDATE table_movies SET languages = '[]' WHERE languages is null")
database.execute("UPDATE table_movies SET hearing_impaired = 'False' WHERE hearing_impaired is null")
database.execute("UPDATE table_movies SET forced = 'False' WHERE forced is null")