Major improvement to series synchronization

pull/152/head
morpheus65535 6 years ago
parent e6a44bcbd6
commit 6156a99ef9

@ -16,10 +16,6 @@ def update_series():
serie_default_language = get_general_settings()[16] serie_default_language = get_general_settings()[16]
serie_default_hi = get_general_settings()[17] serie_default_hi = get_general_settings()[17]
# Open database connection
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
if apikey_sonarr == None: if apikey_sonarr == None:
pass pass
else: else:
@ -39,10 +35,20 @@ def update_series():
except requests.exceptions.RequestException as err: except requests.exceptions.RequestException as err:
logging.exception("Error trying to get series from Sonarr.") logging.exception("Error trying to get series from Sonarr.")
else: else:
# Open database connection
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
# Get current shows in DB # Get current shows in DB
current_shows_db = c.execute('SELECT tvdbId FROM table_shows').fetchall() current_shows_db = c.execute('SELECT tvdbId FROM table_shows').fetchall()
# Close database connection
db.close()
current_shows_db_list = [x[0] for x in current_shows_db] current_shows_db_list = [x[0] for x in current_shows_db]
current_shows_sonarr = [] current_shows_sonarr = []
series_to_update = []
series_to_add = []
for show in r.json(): for show in r.json():
try: try:
@ -62,28 +68,42 @@ def update_series():
# Add shows in Sonarr to current shows list # Add shows in Sonarr to current shows list
current_shows_sonarr.append(show['tvdbId']) current_shows_sonarr.append(show['tvdbId'])
# Update or insert shows list in database table if show['tvdbId'] in current_shows_db_list:
try: 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:
if serie_default_enabled is True: if serie_default_enabled is True:
c.execute('''INSERT INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (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'])) 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']))
list_missing_subtitles(show["id"])
else: else:
c.execute('''INSERT 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 = ?), ?, ?, ?, ?, ?, ?)''', (show["title"], show["path"], show["tvdbId"], show["tvdbId"], show["tvdbId"], show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) 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']))
except:
c.execute('''UPDATE table_shows SET title = ?, path = ?, tvdbId = ?, sonarrSeriesId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ? , sortTitle = ? WHERE tvdbid = ?''', (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"])) # 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:
added_result = c.executemany('''INSERT INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', series_to_add)
db.commit()
else:
added_result = c.executemany('''INSERT 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)
db.commit()
db.close()
for show in series_to_add:
list_missing_subtitles(show[5])
# Delete shows not in Sonarr anymore # Delete shows not in Sonarr anymore
deleted_items = [] deleted_items = []
for item in current_shows_db_list: for item in current_shows_db_list:
if item not in current_shows_sonarr: if item not in current_shows_sonarr:
deleted_items.append(tuple([item])) deleted_items.append(tuple([item]))
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
c.executemany('DELETE FROM table_shows WHERE tvdbId = ?',deleted_items) c.executemany('DELETE FROM table_shows WHERE tvdbId = ?',deleted_items)
# Commit changes to database table
db.commit() db.commit()
db.close()
# Close database connection
db.close()
def get_profile_list(): def get_profile_list():
from get_settings import get_sonarr_settings from get_settings import get_sonarr_settings

Loading…
Cancel
Save