Modify Sonarr base_url validation before building URL

pull/56/merge
morpheus65535 7 years ago
parent 60db173179
commit 5e2586a59a

@ -16,19 +16,6 @@ def update_all_episodes():
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c = db.cursor() c = db.cursor()
# Get Sonarr API URL from database config table
c.execute('''SELECT * FROM table_settings_sonarr''')
config_sonarr = c.fetchone()
if config_sonarr[3] == 1:
protocol_sonarr = "https"
else:
protocol_sonarr = "http"
if config_sonarr[2] == "":
base_url_sonarr = ""
else:
base_url_sonarr = "/" + config_sonarr[2].strip("/")
apikey_sonarr = config_sonarr[4]
# Get current episodes id in DB # Get current episodes id in DB
current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall() current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall()
current_episodes_db_list = [x[0] for x in current_episodes_db] current_episodes_db_list = [x[0] for x in current_episodes_db]
@ -39,7 +26,7 @@ def update_all_episodes():
seriesIdList = c.fetchall() seriesIdList = c.fetchall()
for seriesId in seriesIdList: for seriesId in seriesIdList:
# Get episodes data for a series from Sonarr # Get episodes data for a series from Sonarr
url_sonarr_api_episode = protocol_sonarr + "://" + config_sonarr[0] + ":" + str(config_sonarr[1]) + base_url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr
r = requests.get(url_sonarr_api_episode) r = requests.get(url_sonarr_api_episode)
for episode in r.json(): for episode in r.json():
if episode['hasFile'] and episode['episodeFile']['size'] > 20480: if episode['hasFile'] and episode['episodeFile']['size'] > 20480:
@ -85,19 +72,6 @@ def add_new_episodes():
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c = db.cursor() c = db.cursor()
# Get Sonarr API URL from database config table
c.execute('''SELECT * FROM table_settings_sonarr''')
config_sonarr = c.fetchone()
if config_sonarr[3] == 1:
protocol_sonarr = "https"
else:
protocol_sonarr = "http"
if config_sonarr[2] == "":
base_url_sonarr = ""
else:
base_url_sonarr = "/" + config_sonarr[2].strip("/")
apikey_sonarr = config_sonarr[4]
if apikey_sonarr == None: if apikey_sonarr == None:
# Close database connection # Close database connection
c.close() c.close()
@ -113,7 +87,7 @@ def add_new_episodes():
seriesIdList = c.fetchall() seriesIdList = c.fetchall()
for seriesId in seriesIdList: for seriesId in seriesIdList:
# Get episodes data for a series from Sonarr # Get episodes data for a series from Sonarr
url_sonarr_api_episode = protocol_sonarr + "://" + config_sonarr[0] + ":" + str(config_sonarr[1]) + base_url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr
r = requests.get(url_sonarr_api_episode) r = requests.get(url_sonarr_api_episode)
for episode in r.json(): for episode in r.json():
if episode['hasFile'] and episode['episodeFile']['size'] > 20480: if episode['hasFile'] and episode['episodeFile']['size'] > 20480:

@ -3,34 +3,37 @@ import os
import ast import ast
def get_sonarr_settings(): def get_sonarr_settings():
# Open database connection # Open database connection
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c = db.cursor() c = db.cursor()
# Get Sonarr API URL from database config table # Get Sonarr API URL from database config table
c.execute('''SELECT * FROM table_settings_sonarr''') c.execute('''SELECT * FROM table_settings_sonarr''')
config_sonarr = c.fetchone() config_sonarr = c.fetchone()
# Close database connection # Close database connection
db.close() db.close()
# Build Sonarr URL # Build Sonarr URL
ip_sonarr = config_sonarr[0] ip_sonarr = config_sonarr[0]
port_sonarr = str(config_sonarr[1]) port_sonarr = str(config_sonarr[1])
baseurl_sonarr = config_sonarr[2] baseurl_sonarr = config_sonarr[2]
ssl_sonarr = config_sonarr[3] ssl_sonarr = config_sonarr[3]
apikey_sonarr = config_sonarr[4] apikey_sonarr = config_sonarr[4]
if ssl_sonarr == 1: if ssl_sonarr == 1:
protocol_sonarr = "https" protocol_sonarr = "https"
else: else:
protocol_sonarr = "http" protocol_sonarr = "http"
if baseurl_sonarr == "/":
base_url_sonarr = "" if baseurl_sonarr == None:
else: baseurl_sonarr = "/"
base_url_sonarr = "/" + baseurl_sonarr.strip("/") if baseurl_sonarr.startswith("/") == False:
baseurl_sonarr = "/" + baseurl_sonarr
url_sonarr = protocol_sonarr + "://" + ip_sonarr + ":" + port_sonarr + base_url_sonarr if baseurl_sonarr.endswith("/"):
url_sonarr_short = protocol_sonarr + "://" + ip_sonarr + ":" + port_sonarr baseurl_sonarr = baseurl_sonarr[:-1]
return [url_sonarr, url_sonarr_short, apikey_sonarr] url_sonarr = protocol_sonarr + "://" + ip_sonarr + ":" + port_sonarr + baseurl_sonarr
url_sonarr_short = protocol_sonarr + "://" + ip_sonarr + ":" + port_sonarr
return [url_sonarr, url_sonarr_short, apikey_sonarr]
Loading…
Cancel
Save