2018-10-31 19:34:40 +00:00
|
|
|
# coding=utf-8
|
2018-10-04 18:16:49 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import logging
|
2021-05-28 17:23:09 +00:00
|
|
|
from gevent import sleep
|
2018-10-04 18:16:49 +00:00
|
|
|
|
2018-12-15 00:36:28 +00:00
|
|
|
from config import settings, url_sonarr
|
2018-10-04 18:16:49 +00:00
|
|
|
from list_subtitles import list_missing_subtitles
|
2021-05-08 14:25:29 +00:00
|
|
|
from get_rootfolder import check_sonarr_rootfolder
|
2021-05-26 20:47:14 +00:00
|
|
|
from database import TableShows
|
2019-09-05 23:33:34 +00:00
|
|
|
from utils import get_sonarr_version
|
2020-05-19 13:27:13 +00:00
|
|
|
from helper import path_mappings
|
2021-05-10 23:52:36 +00:00
|
|
|
from event_handler import event_stream, show_progress, hide_progress
|
2018-10-04 18:16:49 +00:00
|
|
|
|
2021-04-05 20:47:57 +00:00
|
|
|
headers = {"User-Agent": os.environ["SZ_USER_AGENT"]}
|
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2021-05-18 03:56:55 +00:00
|
|
|
def update_series(send_event=True):
|
2021-05-08 14:25:29 +00:00
|
|
|
check_sonarr_rootfolder()
|
2018-12-15 00:36:28 +00:00
|
|
|
apikey_sonarr = settings.sonarr.apikey
|
2020-01-31 00:02:59 +00:00
|
|
|
if apikey_sonarr is None:
|
|
|
|
return
|
|
|
|
|
2019-09-15 18:21:57 +00:00
|
|
|
sonarr_version = get_sonarr_version()
|
2018-12-27 19:19:59 +00:00
|
|
|
serie_default_enabled = settings.general.getboolean('serie_default_enabled')
|
2020-03-25 19:03:15 +00:00
|
|
|
|
|
|
|
if serie_default_enabled is True:
|
2021-01-19 04:49:51 +00:00
|
|
|
serie_default_profile = settings.general.serie_default_profile
|
|
|
|
if serie_default_profile == '':
|
|
|
|
serie_default_profile = None
|
2020-03-25 19:03:15 +00:00
|
|
|
else:
|
2021-01-19 04:49:51 +00:00
|
|
|
serie_default_profile = None
|
2020-03-25 19:03:15 +00:00
|
|
|
|
2020-01-31 00:02:59 +00:00
|
|
|
audio_profiles = get_profile_list()
|
2020-07-12 20:37:20 +00:00
|
|
|
tagsDict = get_tags()
|
2020-01-31 00:02:59 +00:00
|
|
|
|
|
|
|
# Get shows data from Sonarr
|
2021-05-08 14:39:00 +00:00
|
|
|
series = get_series_from_sonarr_api(url=url_sonarr(), apikey_sonarr=apikey_sonarr)
|
|
|
|
if not series:
|
2020-01-31 00:02:59 +00:00
|
|
|
return
|
2021-05-08 14:39:00 +00:00
|
|
|
else:
|
|
|
|
# Get current shows in DB
|
2021-05-26 20:47:14 +00:00
|
|
|
current_shows_db = TableShows.select(TableShows.sonarrSeriesId).dicts()
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
current_shows_db_list = [x['sonarrSeriesId'] for x in current_shows_db]
|
|
|
|
current_shows_sonarr = []
|
|
|
|
series_to_update = []
|
|
|
|
series_to_add = []
|
|
|
|
|
2021-05-09 04:15:29 +00:00
|
|
|
series_count = len(series)
|
|
|
|
for i, show in enumerate(series, 1):
|
2021-05-28 17:23:09 +00:00
|
|
|
sleep()
|
2021-05-18 19:41:29 +00:00
|
|
|
if send_event:
|
|
|
|
show_progress(id='series_progress',
|
|
|
|
header='Syncing series...',
|
|
|
|
name=show['title'],
|
|
|
|
value=i,
|
|
|
|
count=series_count)
|
2021-05-09 04:15:29 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
# Add shows in Sonarr to current shows list
|
|
|
|
current_shows_sonarr.append(show['id'])
|
|
|
|
|
|
|
|
if show['id'] in current_shows_db_list:
|
|
|
|
series_to_update.append(seriesParser(show, action='update', sonarr_version=sonarr_version,
|
|
|
|
tags_dict=tagsDict, serie_default_profile=serie_default_profile,
|
|
|
|
audio_profiles=audio_profiles))
|
|
|
|
else:
|
|
|
|
series_to_add.append(seriesParser(show, action='insert', sonarr_version=sonarr_version,
|
|
|
|
tags_dict=tagsDict, serie_default_profile=serie_default_profile,
|
|
|
|
audio_profiles=audio_profiles))
|
|
|
|
|
2021-05-18 19:41:29 +00:00
|
|
|
if send_event:
|
|
|
|
show_progress(id='series_progress',
|
|
|
|
header='Syncing series...',
|
|
|
|
name='Completed successfully',
|
|
|
|
value=series_count,
|
|
|
|
count=series_count)
|
2021-05-12 16:17:09 +00:00
|
|
|
|
2021-05-18 19:41:29 +00:00
|
|
|
hide_progress(id='series_progress')
|
2021-05-10 23:52:36 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
# Remove old series from DB
|
|
|
|
removed_series = list(set(current_shows_db_list) - set(current_shows_sonarr))
|
|
|
|
|
|
|
|
for series in removed_series:
|
2021-05-28 17:23:09 +00:00
|
|
|
sleep()
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShows.delete().where(TableShows.sonarrSeriesId == series).execute()
|
2021-05-18 03:56:55 +00:00
|
|
|
if send_event:
|
|
|
|
event_stream(type='series', action='delete', payload=series)
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
# Update existing series in DB
|
|
|
|
series_in_db_list = []
|
2021-05-26 20:47:14 +00:00
|
|
|
series_in_db = TableShows.select(TableShows.title,
|
|
|
|
TableShows.path,
|
|
|
|
TableShows.tvdbId,
|
|
|
|
TableShows.sonarrSeriesId,
|
|
|
|
TableShows.overview,
|
|
|
|
TableShows.poster,
|
|
|
|
TableShows.fanart,
|
|
|
|
TableShows.audio_language,
|
|
|
|
TableShows.sortTitle,
|
|
|
|
TableShows.year,
|
|
|
|
TableShows.alternateTitles,
|
|
|
|
TableShows.tags,
|
|
|
|
TableShows.seriesType,
|
|
|
|
TableShows.imdbId).dicts()
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
for item in series_in_db:
|
|
|
|
series_in_db_list.append(item)
|
|
|
|
|
|
|
|
series_to_update_list = [i for i in series_to_update if i not in series_in_db_list]
|
|
|
|
|
|
|
|
for updated_series in series_to_update_list:
|
2021-05-28 17:23:09 +00:00
|
|
|
sleep()
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShows.update(updated_series).where(TableShows.sonarrSeriesId ==
|
|
|
|
updated_series['sonarrSeriesId']).execute()
|
2021-05-18 03:56:55 +00:00
|
|
|
if send_event:
|
|
|
|
event_stream(type='series', payload=updated_series['sonarrSeriesId'])
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
# Insert new series in DB
|
|
|
|
for added_series in series_to_add:
|
2021-05-28 17:23:09 +00:00
|
|
|
sleep()
|
2021-05-26 20:47:14 +00:00
|
|
|
result = TableShows.insert(added_series).on_conflict(action='IGNORE').execute()
|
2021-05-08 14:39:00 +00:00
|
|
|
if result:
|
|
|
|
list_missing_subtitles(no=added_series['sonarrSeriesId'])
|
|
|
|
else:
|
|
|
|
logging.debug('BAZARR unable to insert this series into the database:',
|
|
|
|
path_mappings.path_replace(added_series['path']))
|
|
|
|
|
2021-05-18 03:56:55 +00:00
|
|
|
if send_event:
|
|
|
|
event_stream(type='series', action='update', payload=added_series['sonarrSeriesId'])
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
logging.debug('BAZARR All series synced from Sonarr into database.')
|
|
|
|
|
|
|
|
|
|
|
|
def update_one_series(series_id, action):
|
|
|
|
logging.debug('BAZARR syncing this specific series from RSonarr: {}'.format(series_id))
|
|
|
|
|
|
|
|
# Check if there's a row in database for this series ID
|
2021-05-26 20:47:14 +00:00
|
|
|
existing_series = TableShows.get_or_none(TableShows.sonarrSeriesId == series_id)
|
2020-01-31 00:02:59 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
sonarr_version = get_sonarr_version()
|
|
|
|
serie_default_enabled = settings.general.getboolean('serie_default_enabled')
|
2019-10-31 17:21:49 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
if serie_default_enabled is True:
|
|
|
|
serie_default_profile = settings.general.serie_default_profile
|
|
|
|
if serie_default_profile == '':
|
|
|
|
serie_default_profile = None
|
|
|
|
else:
|
|
|
|
serie_default_profile = None
|
2019-08-14 23:38:48 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
audio_profiles = get_profile_list()
|
|
|
|
tagsDict = get_tags()
|
2019-08-14 23:38:48 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
try:
|
|
|
|
# Get series data from sonarr api
|
|
|
|
series = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
series_data = get_series_from_sonarr_api(url=url_sonarr(), apikey_sonarr=settings.sonarr.apikey,
|
|
|
|
sonarr_series_id=series_id)
|
|
|
|
except requests.exceptions.HTTPError:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShows.delete().where(TableShows.sonarrSeriesId == series_id).execute()
|
2021-05-08 14:39:00 +00:00
|
|
|
event_stream(type='series', action='delete', payload=int(series_id))
|
|
|
|
return
|
|
|
|
|
|
|
|
if not series_data:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if action == 'updated' and existing_series:
|
|
|
|
series = seriesParser(series_data, action='update', sonarr_version=sonarr_version,
|
|
|
|
tags_dict=tagsDict, serie_default_profile=serie_default_profile,
|
|
|
|
audio_profiles=audio_profiles)
|
|
|
|
elif action == 'updated' and not existing_series:
|
|
|
|
series = seriesParser(series_data, action='insert', sonarr_version=sonarr_version,
|
|
|
|
tags_dict=tagsDict, serie_default_profile=serie_default_profile,
|
|
|
|
audio_profiles=audio_profiles)
|
|
|
|
except Exception:
|
|
|
|
logging.debug('BAZARR cannot parse series returned by SignalR feed.')
|
|
|
|
return
|
2019-08-14 23:38:48 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
# Remove series from DB
|
|
|
|
if action == 'deleted':
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShows.delete().where(TableShows.sonarrSeriesId == series_id).execute()
|
2021-05-08 14:39:00 +00:00
|
|
|
event_stream(type='series', action='delete', payload=int(series_id))
|
|
|
|
logging.debug('BAZARR deleted this series from the database:{}'.format(path_mappings.path_replace(
|
2021-05-26 20:47:14 +00:00
|
|
|
existing_series.path)))
|
2021-05-08 14:39:00 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Update existing series in DB
|
|
|
|
elif action == 'updated' and existing_series:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShows.update(series).where(TableShows.sonarrSeriesId == series['sonarrSeriesId']).execute()
|
2021-05-08 14:39:00 +00:00
|
|
|
event_stream(type='series', action='update', payload=int(series_id))
|
|
|
|
logging.debug('BAZARR updated this series into the database:{}'.format(path_mappings.path_replace(
|
|
|
|
series['path'])))
|
2020-01-13 03:03:19 +00:00
|
|
|
|
2020-01-31 00:02:59 +00:00
|
|
|
# Insert new series in DB
|
2021-05-08 14:39:00 +00:00
|
|
|
elif action == 'updated' and not existing_series:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShows.insert(series).on_conflict(action='IGNORE').execute()
|
2021-05-08 14:39:00 +00:00
|
|
|
event_stream(type='series', action='update', payload=int(series_id))
|
|
|
|
logging.debug('BAZARR inserted this series into the database:{}'.format(path_mappings.path_replace(
|
|
|
|
series['path'])))
|
2019-09-12 00:01:48 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2018-10-04 18:16:49 +00:00
|
|
|
def get_profile_list():
|
2018-12-15 00:36:28 +00:00
|
|
|
apikey_sonarr = settings.sonarr.apikey
|
2019-09-05 23:33:34 +00:00
|
|
|
sonarr_version = get_sonarr_version()
|
|
|
|
profiles_list = []
|
|
|
|
|
2020-01-31 00:02:59 +00:00
|
|
|
# Get profiles data from Sonarr
|
2019-09-05 23:33:34 +00:00
|
|
|
if sonarr_version.startswith('2'):
|
2019-11-06 01:49:08 +00:00
|
|
|
url_sonarr_api_series = url_sonarr() + "/api/profile?apikey=" + apikey_sonarr
|
2020-01-31 00:02:59 +00:00
|
|
|
else:
|
2019-11-06 01:49:08 +00:00
|
|
|
url_sonarr_api_series = url_sonarr() + "/api/v3/languageprofile?apikey=" + apikey_sonarr
|
2019-09-05 23:33:34 +00:00
|
|
|
|
2018-10-04 18:16:49 +00:00
|
|
|
try:
|
2021-04-05 20:47:57 +00:00
|
|
|
profiles_json = requests.get(url_sonarr_api_series, timeout=60, verify=False, headers=headers)
|
2020-01-31 00:02:59 +00:00
|
|
|
except requests.exceptions.ConnectionError:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get profiles from Sonarr. Connection Error.")
|
2020-01-31 00:02:59 +00:00
|
|
|
return None
|
|
|
|
except requests.exceptions.Timeout:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get profiles from Sonarr. Timeout Error.")
|
2020-01-31 00:02:59 +00:00
|
|
|
return None
|
|
|
|
except requests.exceptions.RequestException:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get profiles from Sonarr.")
|
2020-01-31 00:02:59 +00:00
|
|
|
return None
|
2018-10-04 18:16:49 +00:00
|
|
|
|
2020-01-31 00:02:59 +00:00
|
|
|
# Parsing data returned from Sonarr
|
|
|
|
if sonarr_version.startswith('2'):
|
|
|
|
for profile in profiles_json.json():
|
|
|
|
profiles_list.append([profile['id'], profile['language'].capitalize()])
|
|
|
|
else:
|
|
|
|
for profile in profiles_json.json():
|
|
|
|
profiles_list.append([profile['id'], profile['name'].capitalize()])
|
2019-09-05 23:33:34 +00:00
|
|
|
|
2020-01-31 00:02:59 +00:00
|
|
|
return profiles_list
|
2019-09-05 23:33:34 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2020-01-31 00:02:59 +00:00
|
|
|
def profile_id_to_language(id_, profiles):
|
2021-01-19 04:49:51 +00:00
|
|
|
profiles_to_return = []
|
2019-09-05 23:33:34 +00:00
|
|
|
for profile in profiles:
|
2020-01-31 00:02:59 +00:00
|
|
|
if id_ == profile[0]:
|
2021-01-19 04:49:51 +00:00
|
|
|
profiles_to_return.append(profile[1])
|
|
|
|
return profiles_to_return
|
2020-07-12 20:37:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_tags():
|
|
|
|
apikey_sonarr = settings.sonarr.apikey
|
|
|
|
tagsDict = []
|
|
|
|
|
|
|
|
# Get tags data from Sonarr
|
|
|
|
url_sonarr_api_series = url_sonarr() + "/api/tag?apikey=" + apikey_sonarr
|
|
|
|
|
|
|
|
try:
|
2021-04-05 20:47:57 +00:00
|
|
|
tagsDict = requests.get(url_sonarr_api_series, timeout=60, verify=False, headers=headers)
|
2020-07-12 20:37:20 +00:00
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logging.exception("BAZARR Error trying to get tags from Sonarr. Connection Error.")
|
|
|
|
return []
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
logging.exception("BAZARR Error trying to get tags from Sonarr. Timeout Error.")
|
|
|
|
return []
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
logging.exception("BAZARR Error trying to get tags from Sonarr.")
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return tagsDict.json()
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def seriesParser(show, action, sonarr_version, tags_dict, serie_default_profile, audio_profiles):
|
|
|
|
overview = show['overview'] if 'overview' in show else ''
|
|
|
|
poster = ''
|
|
|
|
fanart = ''
|
|
|
|
for image in show['images']:
|
|
|
|
if image['coverType'] == 'poster':
|
|
|
|
poster_big = image['url'].split('?')[0]
|
|
|
|
poster = os.path.splitext(poster_big)[0] + '-250' + os.path.splitext(poster_big)[1]
|
|
|
|
|
|
|
|
if image['coverType'] == 'fanart':
|
|
|
|
fanart = image['url'].split('?')[0]
|
|
|
|
|
|
|
|
alternate_titles = None
|
|
|
|
if show['alternateTitles'] is not None:
|
|
|
|
alternate_titles = str([item['title'] for item in show['alternateTitles']])
|
|
|
|
|
|
|
|
audio_language = []
|
|
|
|
if sonarr_version.startswith('2'):
|
|
|
|
audio_language = profile_id_to_language(show['qualityProfileId'], audio_profiles)
|
|
|
|
else:
|
|
|
|
audio_language = profile_id_to_language(show['languageProfileId'], audio_profiles)
|
|
|
|
|
|
|
|
tags = [d['label'] for d in tags_dict if d['id'] in show['tags']]
|
|
|
|
|
|
|
|
imdbId = show['imdbId'] if 'imdbId' in show else None
|
|
|
|
|
|
|
|
if action == 'update':
|
|
|
|
return {'title': show["title"],
|
|
|
|
'path': show["path"],
|
|
|
|
'tvdbId': int(show["tvdbId"]),
|
|
|
|
'sonarrSeriesId': int(show["id"]),
|
|
|
|
'overview': overview,
|
|
|
|
'poster': poster,
|
|
|
|
'fanart': fanart,
|
|
|
|
'audio_language': str(audio_language),
|
|
|
|
'sortTitle': show['sortTitle'],
|
|
|
|
'year': str(show['year']),
|
|
|
|
'alternateTitles': alternate_titles,
|
|
|
|
'tags': str(tags),
|
|
|
|
'seriesType': show['seriesType'],
|
|
|
|
'imdbId': imdbId}
|
|
|
|
else:
|
|
|
|
return {'title': show["title"],
|
|
|
|
'path': show["path"],
|
|
|
|
'tvdbId': show["tvdbId"],
|
|
|
|
'sonarrSeriesId': show["id"],
|
|
|
|
'overview': overview,
|
|
|
|
'poster': poster,
|
|
|
|
'fanart': fanart,
|
|
|
|
'audio_language': str(audio_language),
|
|
|
|
'sortTitle': show['sortTitle'],
|
|
|
|
'year': str(show['year']),
|
|
|
|
'alternateTitles': alternate_titles,
|
|
|
|
'tags': str(tags),
|
|
|
|
'seriesType': show['seriesType'],
|
|
|
|
'imdbId': imdbId,
|
|
|
|
'profileId': serie_default_profile}
|
|
|
|
|
|
|
|
|
|
|
|
def get_series_from_sonarr_api(url, apikey_sonarr, sonarr_series_id=None):
|
|
|
|
url_sonarr_api_series = url + "/api/series" + ("/{}".format(sonarr_series_id) if sonarr_series_id else "") + \
|
|
|
|
"?apikey=" + apikey_sonarr
|
|
|
|
try:
|
|
|
|
r = requests.get(url_sonarr_api_series, timeout=60, verify=False, headers=headers)
|
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as e:
|
|
|
|
if e.response.status_code:
|
|
|
|
raise requests.exceptions.HTTPError
|
|
|
|
logging.exception("BAZARR Error trying to get series from Sonarr. Http error.")
|
|
|
|
return
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logging.exception("BAZARR Error trying to get series from Sonarr. Connection Error.")
|
|
|
|
return
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
logging.exception("BAZARR Error trying to get series from Sonarr. Timeout Error.")
|
|
|
|
return
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
logging.exception("BAZARR Error trying to get series from Sonarr.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
return r.json()
|