2018-08-16 02:01:49 +00:00
|
|
|
from get_argv import config_dir
|
|
|
|
|
2017-09-16 00:49:46 +00:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import requests
|
2018-07-11 02:31:58 +00:00
|
|
|
import logging
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
from get_settings import get_general_settings
|
2018-08-15 20:51:46 +00:00
|
|
|
from list_subtitles import list_missing_subtitles
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2017-10-03 02:59:45 +00:00
|
|
|
def update_series():
|
2018-08-23 19:58:15 +00:00
|
|
|
from get_settings import get_sonarr_settings
|
|
|
|
url_sonarr = get_sonarr_settings()[6]
|
|
|
|
apikey_sonarr = get_sonarr_settings()[4]
|
2018-06-27 02:47:58 +00:00
|
|
|
serie_default_enabled = get_general_settings()[15]
|
|
|
|
serie_default_language = get_general_settings()[16]
|
|
|
|
serie_default_hi = get_general_settings()[17]
|
2017-12-20 03:25:10 +00:00
|
|
|
|
2017-11-16 02:07:21 +00:00
|
|
|
if apikey_sonarr == None:
|
|
|
|
pass
|
|
|
|
else:
|
2018-01-12 00:42:35 +00:00
|
|
|
get_profile_list()
|
|
|
|
|
2017-11-16 02:07:21 +00:00
|
|
|
# Get shows data from Sonarr
|
|
|
|
url_sonarr_api_series = url_sonarr + "/api/series?apikey=" + apikey_sonarr
|
2018-07-11 02:31:58 +00:00
|
|
|
try:
|
2018-09-02 13:48:50 +00:00
|
|
|
r = requests.get(url_sonarr_api_series, timeout=15, verify=False)
|
2018-07-11 02:31:58 +00:00
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as errh:
|
|
|
|
logging.exception("Error trying to get series from Sonarr. Http error.")
|
|
|
|
except requests.exceptions.ConnectionError as errc:
|
|
|
|
logging.exception("Error trying to get series from Sonarr. Connection Error.")
|
|
|
|
except requests.exceptions.Timeout as errt:
|
|
|
|
logging.exception("Error trying to get series from Sonarr. Timeout Error.")
|
|
|
|
except requests.exceptions.RequestException as err:
|
|
|
|
logging.exception("Error trying to get series from Sonarr.")
|
|
|
|
else:
|
2018-09-25 02:22:54 +00:00
|
|
|
# Open database connection
|
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
|
|
|
c = db.cursor()
|
|
|
|
|
2018-07-11 02:31:58 +00:00
|
|
|
# Get current shows in DB
|
|
|
|
current_shows_db = c.execute('SELECT tvdbId FROM table_shows').fetchall()
|
2018-09-25 02:22:54 +00:00
|
|
|
|
|
|
|
# Close database connection
|
|
|
|
db.close()
|
|
|
|
|
2018-07-11 02:31:58 +00:00
|
|
|
current_shows_db_list = [x[0] for x in current_shows_db]
|
|
|
|
current_shows_sonarr = []
|
2018-09-25 02:22:54 +00:00
|
|
|
series_to_update = []
|
|
|
|
series_to_add = []
|
2018-07-11 02:31:58 +00:00
|
|
|
|
|
|
|
for show in r.json():
|
|
|
|
try:
|
|
|
|
overview = unicode(show['overview'])
|
|
|
|
except:
|
|
|
|
overview = ""
|
|
|
|
try:
|
|
|
|
poster_big = show['images'][2]['url'].split('?')[0]
|
|
|
|
poster = os.path.splitext(poster_big)[0] + '-250' + os.path.splitext(poster_big)[1]
|
|
|
|
except:
|
|
|
|
poster = ""
|
|
|
|
try:
|
|
|
|
fanart = show['images'][0]['url'].split('?')[0]
|
|
|
|
except:
|
|
|
|
fanart = ""
|
|
|
|
|
|
|
|
# Add shows in Sonarr to current shows list
|
|
|
|
current_shows_sonarr.append(show['tvdbId'])
|
|
|
|
|
2018-09-25 02:22:54 +00:00
|
|
|
if show['tvdbId'] in current_shows_db_list:
|
|
|
|
series_to_update.append((show["title"],show["path"],show["tvdbId"],show["id"],overview,poster,fanart,profile_id_to_language((show['qualityProfileId'] if sonarr_version == 2 else show['languageProfileId'])),show['sortTitle'],show["tvdbId"]))
|
|
|
|
else:
|
2018-08-23 19:58:15 +00:00
|
|
|
if serie_default_enabled is True:
|
2018-09-25 02:22:54 +00:00
|
|
|
series_to_add.append((show["title"], show["path"], show["tvdbId"], serie_default_language, serie_default_hi, show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle']))
|
2018-07-11 02:31:58 +00:00
|
|
|
else:
|
2018-09-25 02:22:54 +00:00
|
|
|
series_to_add.append((show["title"], show["path"], show["tvdbId"], show["tvdbId"], show["tvdbId"], show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle']))
|
|
|
|
|
|
|
|
# Update or insert series in DB
|
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
|
|
|
c = db.cursor()
|
|
|
|
|
|
|
|
updated_result = c.executemany('''UPDATE table_shows SET title = ?, path = ?, tvdbId = ?, sonarrSeriesId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ? , sortTitle = ? WHERE tvdbid = ?''', series_to_update)
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
if serie_default_enabled is True:
|
2018-10-01 00:48:36 +00:00
|
|
|
added_result = c.executemany('''INSERT OR IGNORE INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', series_to_add)
|
2018-09-25 02:22:54 +00:00
|
|
|
db.commit()
|
|
|
|
else:
|
2018-10-01 00:48:36 +00:00
|
|
|
added_result = c.executemany('''INSERT OR IGNORE INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?,?,?,(SELECT languages FROM table_shows WHERE tvdbId = ?),(SELECT `hearing_impaired` FROM table_shows WHERE tvdbId = ?), ?, ?, ?, ?, ?, ?)''', series_to_add)
|
2018-09-25 02:22:54 +00:00
|
|
|
db.commit()
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
for show in series_to_add:
|
|
|
|
list_missing_subtitles(show[5])
|
2018-07-11 02:31:58 +00:00
|
|
|
|
|
|
|
# Delete shows not in Sonarr anymore
|
|
|
|
deleted_items = []
|
|
|
|
for item in current_shows_db_list:
|
|
|
|
if item not in current_shows_sonarr:
|
|
|
|
deleted_items.append(tuple([item]))
|
2018-09-25 02:22:54 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
|
|
|
c = db.cursor()
|
2018-07-11 02:31:58 +00:00
|
|
|
c.executemany('DELETE FROM table_shows WHERE tvdbId = ?',deleted_items)
|
|
|
|
db.commit()
|
2018-09-25 02:22:54 +00:00
|
|
|
db.close()
|
2017-12-19 01:24:04 +00:00
|
|
|
|
|
|
|
def get_profile_list():
|
2018-08-23 19:58:15 +00:00
|
|
|
from get_settings import get_sonarr_settings
|
|
|
|
url_sonarr = get_sonarr_settings()[6]
|
|
|
|
# url_sonarr_short = get_sonarr_settings()[5]
|
|
|
|
apikey_sonarr = get_sonarr_settings()[4]
|
2017-12-20 03:25:10 +00:00
|
|
|
|
2017-12-19 01:24:04 +00:00
|
|
|
# Get profiles data from Sonarr
|
2018-07-11 02:31:58 +00:00
|
|
|
error = False
|
|
|
|
|
2017-12-19 01:24:04 +00:00
|
|
|
url_sonarr_api_series = url_sonarr + "/api/profile?apikey=" + apikey_sonarr
|
2018-07-11 02:31:58 +00:00
|
|
|
try:
|
2018-09-02 13:48:50 +00:00
|
|
|
profiles_json = requests.get(url_sonarr_api_series, timeout=15, verify=False)
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.ConnectionError as errc:
|
|
|
|
error = True
|
|
|
|
logging.exception("Error trying to get profiles from Sonarr. Connection Error.")
|
|
|
|
except requests.exceptions.Timeout as errt:
|
|
|
|
error = True
|
|
|
|
logging.exception("Error trying to get profiles from Sonarr. Timeout Error.")
|
|
|
|
except requests.exceptions.RequestException as err:
|
|
|
|
error = True
|
|
|
|
logging.exception("Error trying to get profiles from Sonarr.")
|
|
|
|
|
2018-01-12 00:42:35 +00:00
|
|
|
url_sonarr_api_series_v3 = url_sonarr + "/api/v3/languageprofile?apikey=" + apikey_sonarr
|
2018-07-11 02:31:58 +00:00
|
|
|
try:
|
2018-09-02 13:48:50 +00:00
|
|
|
profiles_json_v3 = requests.get(url_sonarr_api_series_v3, timeout=15, verify=False)
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.ConnectionError as errc:
|
|
|
|
error = True
|
|
|
|
logging.exception("Error trying to get profiles from Sonarr. Connection Error.")
|
|
|
|
except requests.exceptions.Timeout as errt:
|
|
|
|
error = True
|
|
|
|
logging.exception("Error trying to get profiles from Sonarr. Timeout Error.")
|
|
|
|
except requests.exceptions.RequestException as err:
|
|
|
|
error = True
|
|
|
|
logging.exception("Error trying to get profiles from Sonarr.")
|
|
|
|
|
2017-12-19 01:24:04 +00:00
|
|
|
global profiles_list
|
|
|
|
profiles_list = []
|
|
|
|
|
2018-07-11 02:31:58 +00:00
|
|
|
if error is False:
|
|
|
|
# Parsing data returned from Sonarr
|
|
|
|
global sonarr_version
|
|
|
|
if type(profiles_json_v3.json()) != list:
|
|
|
|
sonarr_version = 2
|
|
|
|
for profile in profiles_json.json():
|
|
|
|
profiles_list.append([profile['id'], profile['language'].capitalize()])
|
|
|
|
else:
|
|
|
|
sonarr_version = 3
|
|
|
|
for profile in profiles_json_v3.json():
|
|
|
|
profiles_list.append([profile['id'], profile['name'].capitalize()])
|
2017-12-19 01:24:04 +00:00
|
|
|
|
|
|
|
def profile_id_to_language(id):
|
|
|
|
for profile in profiles_list:
|
|
|
|
if id == profile[0]:
|
2018-01-12 00:42:35 +00:00
|
|
|
return profile[1]
|