Move providers config from DB to config file

pull/292/head
Halali 6 years ago
parent 48648d1582
commit ef2dc275ed

@ -1,51 +1,12 @@
# coding=utf-8
import sqlite3
import os
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)
c = db.cursor()
# Remove unsupported providers
providers_in_db = c.execute('SELECT name FROM table_settings_providers').fetchall()
for provider_in_db in providers_in_db:
if provider_in_db[0] not in providers_list:
c.execute('DELETE FROM table_settings_providers WHERE name = ?', (provider_in_db[0], ))
# Commit changes to database table
db.commit()
# Insert providers in database table
for provider_name in providers_list:
c.execute('''INSERT OR IGNORE INTO table_settings_providers(name) VALUES(?)''', (provider_name, ))
# Commit changes to database table
db.commit()
# Close database connection
db.close()
from config import settings
def get_providers():
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()
providers_list = []
if len(enabled_providers) > 0:
for provider in enabled_providers:
providers_list.append(provider[0])
if settings.general.enabled_providers:
for provider in settings.general.enabled_providers.lower().split(','):
providers_list.append(provider)
else:
providers_list = None
@ -53,18 +14,18 @@ def get_providers():
def get_providers_auth():
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()
providers_auth = collections.defaultdict(dict)
if len(enabled_providers) > 0:
for provider in enabled_providers:
providers_auth[provider[0]] = {}
providers_auth[provider[0]]['username'] = provider[2]
providers_auth[provider[0]]['password'] = provider[3]
else:
providers_auth = None
providers_auth = {
'addic7ed': {
'username': settings.addic7ed.username,
'password': settings.addic7ed.password,
},
'opensubtitles': {
'username': settings.opensubtitles.username,
'password': settings.opensubtitles.password,
},
'legendastv': {
'username': settings.legendastv.username,
'password': settings.legendastv.password,
}}
return providers_auth

@ -6,6 +6,7 @@ import time
from cork import Cork
from configparser import ConfigParser
from config import settings
from check_update import check_releases
from get_argv import config_dir
@ -82,6 +83,37 @@ if cfg.has_section('general'):
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(config_dir, 'db', 'bazarr.db'), timeout=30)
c = db.cursor()
enabled_providers = c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall()
c.execute("DROP TABLE table_settings_providers")
db.close()
providers_list = []
if len(enabled_providers) > 0:
for provider in enabled_providers:
providers_list.append(provider[0])
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]
else:
providers_list = None
settings.general.enabled_providers = u'' if not providers_list else ','.join(providers_list)
with open(os.path.join(config_dir, 'config', 'config.ini'), 'w+') as handle:
settings.write(handle)
except:
pass
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)

@ -22,6 +22,7 @@ update_notifier()
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
from subliminal import provider_manager
configure_logging(settings.general.getboolean('debug'))
@ -64,8 +65,7 @@ from six import text_type
import apprise
from get_languages import load_language_in_db, language_from_alpha3
from get_providers import load_providers, get_providers, get_providers_auth
load_providers()
from get_providers import get_providers, get_providers_auth
from get_series import *
from get_episodes import *
@ -336,9 +336,7 @@ def save_wizard():
settings.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")
for item in settings_subliminal_providers:
c.execute("UPDATE table_settings_providers SET enabled = '1' WHERE name = ?", (item,))
settings.general.enabled_providers = u'' if not settings_subliminal_providers else ','.join(settings_subliminal_providers)
settings_subliminal_languages = request.forms.getall('settings_subliminal_languages')
c.execute("UPDATE table_settings_languages SET enabled = 0")
@ -1005,8 +1003,7 @@ def _settings():
c = db.cursor()
c.execute("SELECT * FROM table_settings_languages ORDER BY name")
settings_languages = c.fetchall()
c.execute("SELECT * FROM table_settings_providers ORDER BY name")
settings_providers = c.fetchall()
settings_providers = sorted(provider_manager.names())
c.execute("SELECT * FROM table_settings_notifier ORDER BY name")
settings_notifier = c.fetchall()
c.close()
@ -1218,19 +1215,14 @@ def save_settings():
settings.radarr.full_update = text_type(settings_radarr_sync)
settings_subliminal_providers = request.forms.getall('settings_subliminal_providers')
c.execute("UPDATE table_settings_providers SET enabled = 0")
for item in settings_subliminal_providers:
c.execute("UPDATE table_settings_providers SET enabled = '1' WHERE name = ?", (item,))
settings_addic7ed_username = request.forms.get('settings_addic7ed_username')
settings_addic7ed_password = request.forms.get('settings_addic7ed_password')
c.execute("UPDATE table_settings_providers SET username = ?, password = ? WHERE name = 'addic7ed'", (settings_addic7ed_username, settings_addic7ed_password))
settings_legendastv_username = request.forms.get('settings_legendastv_username')
settings_legendastv_password = request.forms.get('settings_legendastv_password')
c.execute("UPDATE table_settings_providers SET username = ?, password = ? WHERE name = 'legendastv'", (settings_legendastv_username, settings_legendastv_password))
settings_opensubtitles_username = request.forms.get('settings_opensubtitles_username')
settings_opensubtitles_password = request.forms.get('settings_opensubtitles_password')
c.execute("UPDATE table_settings_providers SET username = ?, password = ? WHERE name = 'opensubtitles'", (settings_opensubtitles_username, settings_opensubtitles_password))
settings.general.enabled_providers = u'' if not settings_subliminal_providers else ','.join(settings_subliminal_providers)
settings.addic7ed.username = request.forms.get('settings_addic7ed_username')
settings.addic7ed.password = request.forms.get('settings_addic7ed_password')
settings.legendastv.username = request.forms.get('settings_legendastv_username')
settings.legendastv.password = request.forms.get('settings_legendastv_password')
settings.opensubtitles.username = request.forms.get('settings_opensubtitles_username')
settings.opensubtitles.password = request.forms.get('settings_opensubtitles_password')
settings_subliminal_languages = request.forms.getall('settings_subliminal_languages')
c.execute("UPDATE table_settings_languages SET enabled = 0")

@ -1069,11 +1069,12 @@
<select name="settings_subliminal_providers" id="settings_providers" multiple="" class="ui fluid search selection dropdown">
<option value="">Providers</option>
%enabled_providers = []
%providers = settings.general.enabled_providers.lower().split(',')
%for provider in settings_providers:
<option value="{{provider[0]}}">{{provider[0]}}</option>
%if provider[1] == True:
% enabled_providers.append(str(provider[0]))
<option value="{{provider}}">{{provider}}</option>
%end
%for provider in providers:
%enabled_providers.append(str(provider))
%end
</select>
</div>
@ -1103,20 +1104,14 @@
<div class="right aligned four wide column">
<label>addic7ed</label>
</div>
%for provider in settings_providers:
% if provider[0] == 'addic7ed':
% addic7ed_username = provider[2]
% addic7ed_password = provider[3]
% end
%end
<div class="five wide column">
<div class="ui fluid input">
<input name="settings_addic7ed_username" type="text" value="{{addic7ed_username if addic7ed_username != None else ''}}">
<input name="settings_addic7ed_username" type="text" value="{{settings.addic7ed.username if settings.addic7ed.username != None else ''}}">
</div>
</div>
<div class="five wide column">
<div class="ui fluid input">
<input name="settings_addic7ed_password" type="password" value="{{addic7ed_password if addic7ed_password != None else ''}}">
<input name="settings_addic7ed_password" type="password" value="{{settings.addic7ed.password if settings.addic7ed.password != None else ''}}">
</div>
</div>
</div>
@ -1124,20 +1119,14 @@
<div class="right aligned four wide column">
<label>legendastv</label>
</div>
%for provider in settings_providers:
% if provider[0] == 'legendastv':
% legendastv_username = provider[2]
% legendastv_password = provider[3]
% end
%end
<div class="five wide column">
<div class="ui fluid input">
<input name="settings_legendastv_username" type="text" value="{{legendastv_username if legendastv_username != None else ''}}">
<input name="settings_legendastv_username" type="text" value="{{settings.legendastv.username if settings.legendastv.username != None else ''}}">
</div>
</div>
<div class="five wide column">
<div class="ui fluid input">
<input name="settings_legendastv_password" type="password" value="{{legendastv_password if legendastv_password != None else ''}}">
<input name="settings_legendastv_password" type="password" value="{{settings.legendastv.password if settings.legendastv.password != None else ''}}">
</div>
</div>
</div>
@ -1145,20 +1134,14 @@
<div class="right aligned four wide column">
<label>opensubtitles</label>
</div>
%for provider in settings_providers:
% if provider[0] == 'opensubtitles':
% opensubtitles_username = provider[2]
% opensubtitles_password = provider[3]
% end
%end
<div class="five wide column">
<div class="ui fluid input">
<input name="settings_opensubtitles_username" type="text" value="{{opensubtitles_username if opensubtitles_username != None else ''}}">
<input name="settings_opensubtitles_username" type="text" value="{{settings.opensubtitles.username if settings.opensubtitles.username != None else ''}}">
</div>
</div>
<div class="five wide column">
<div class="ui fluid input">
<input name="settings_opensubtitles_password" type="password" value="{{opensubtitles_password if opensubtitles_password != None else ''}}">
<input name="settings_opensubtitles_password" type="password" value="{{settings.opensubtitles.password if settings.opensubtitles.password != None else ''}}">
</div>
</div>
</div>

@ -50,8 +50,8 @@
<div id="shutdown" class="ui icon button" data-tooltip="Shutdown" data-inverted=""><i class="red power off icon"></i></div>
<div id="restart" class="ui icon button" data-tooltip="Restart" data-inverted=""><i class="redo alternate icon"></i></div>
% from config import settings
% if settings.auth.type is not 'None':
<div id="logout" class="ui icon button" data-tooltip="Logout" data-inverted=""><i class="sign-out icon"></i></div>
% if settings.auth.type != "None":
<div id="logout" class="ui icon button" data-tooltip="Logout" data-inverted=""><i class="sign-out icon"></i></div>
% end
</div>
<div class="ui top attached tabular menu">

Loading…
Cancel
Save