mirror of
https://github.com/morpheus65535/bazarr.git
synced 2025-01-06 06:52:07 +08:00
Added series' episodes sync when required by Sonarr (ex.: episodes monitored status change)
This commit is contained in:
parent
d8e8d7afc2
commit
47bb77ac6f
2 changed files with 23 additions and 6 deletions
|
@ -19,12 +19,16 @@ def update_all_episodes():
|
|||
logging.info('BAZARR All existing episode subtitles indexed from disk.')
|
||||
|
||||
|
||||
def sync_episodes(send_event=True):
|
||||
def sync_episodes(series_id=None, send_event=True):
|
||||
logging.debug('BAZARR Starting episodes sync from Sonarr.')
|
||||
apikey_sonarr = settings.sonarr.apikey
|
||||
|
||||
# Get current episodes id in DB
|
||||
current_episodes_db = database.execute("SELECT sonarrEpisodeId, path, sonarrSeriesId FROM table_episodes")
|
||||
if series_id:
|
||||
current_episodes_db = database.execute("SELECT sonarrEpisodeId, path, sonarrSeriesId FROM table_episodes WHERE "
|
||||
"sonarrSeriesId = ?", (series_id,))
|
||||
else:
|
||||
current_episodes_db = database.execute("SELECT sonarrEpisodeId, path, sonarrSeriesId FROM table_episodes")
|
||||
|
||||
current_episodes_db_list = [x['sonarrEpisodeId'] for x in current_episodes_db]
|
||||
|
||||
|
@ -34,7 +38,7 @@ def sync_episodes(send_event=True):
|
|||
altered_episodes = []
|
||||
|
||||
# Get sonarrId for each series from database
|
||||
seriesIdList = get_series_from_sonarr_api(url=url_sonarr(), apikey_sonarr=apikey_sonarr)
|
||||
seriesIdList = get_series_from_sonarr_api(series_id=series_id, url=url_sonarr(), apikey_sonarr=apikey_sonarr)
|
||||
|
||||
series_count = len(seriesIdList)
|
||||
for i, seriesId in enumerate(seriesIdList, 1):
|
||||
|
@ -290,8 +294,11 @@ def episodeParser(episode):
|
|||
'file_size': episode['episodeFile']['size']}
|
||||
|
||||
|
||||
def get_series_from_sonarr_api(url, apikey_sonarr):
|
||||
url_sonarr_api_series = url + "/api/series?apikey=" + apikey_sonarr
|
||||
def get_series_from_sonarr_api(series_id, url, apikey_sonarr):
|
||||
if series_id:
|
||||
url_sonarr_api_series = url + "/api/series/{0}?apikey={1}".format(series_id, apikey_sonarr)
|
||||
else:
|
||||
url_sonarr_api_series = url + "/api/series?apikey={}".format(apikey_sonarr)
|
||||
try:
|
||||
r = requests.get(url_sonarr_api_series, timeout=60, verify=False, headers=headers)
|
||||
r.raise_for_status()
|
||||
|
@ -310,8 +317,12 @@ def get_series_from_sonarr_api(url, apikey_sonarr):
|
|||
logging.exception("BAZARR Error trying to get series from Sonarr.")
|
||||
return
|
||||
else:
|
||||
if series_id:
|
||||
series_json = list(r.json())
|
||||
else:
|
||||
series_json = r.json()
|
||||
series_list = []
|
||||
for series in r.json():
|
||||
for series in series_json:
|
||||
series_list.append({'sonarrSeriesId': series['id'], 'title': series['title']})
|
||||
return series_list
|
||||
|
||||
|
|
|
@ -132,11 +132,14 @@ class RadarrSignalrClient:
|
|||
|
||||
def dispatcher(data):
|
||||
topic = media_id = action = None
|
||||
episodesChanged = None
|
||||
if isinstance(data, dict):
|
||||
topic = data['name']
|
||||
try:
|
||||
media_id = data['body']['resource']['id']
|
||||
action = data['body']['action']
|
||||
if 'episodesChanged' in data['body']['resource']:
|
||||
episodesChanged = data['body']['resource']['episodesChanged']
|
||||
except KeyError:
|
||||
return
|
||||
elif isinstance(data, list):
|
||||
|
@ -149,6 +152,9 @@ def dispatcher(data):
|
|||
|
||||
if topic == 'series':
|
||||
update_one_series(series_id=media_id, action=action)
|
||||
if episodesChanged:
|
||||
# this will happen if an episode monitored status is changed.
|
||||
sync_episodes(series_id=media_id, send_event=True)
|
||||
elif topic == 'episode':
|
||||
sync_one_episode(episode_id=media_id)
|
||||
elif topic == 'movie':
|
||||
|
|
Loading…
Reference in a new issue