Skip to content

Commit

Permalink
Revert "Merged the series and episodes sync process. Episodes are onl…
Browse files Browse the repository at this point in the history
…y synced if series sizeOnDisk reported by Sonarr changes."

This reverts commit 5aadcea
  • Loading branch information
morpheus65535 committed Apr 5, 2021
1 parent 47157f8 commit 97caf44
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 49 deletions.
3 changes: 2 additions & 1 deletion bazarr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
'full_update_day': '6',
'full_update_hour': '4',
'only_monitored': 'False',
'series_sync': '5',
'series_sync': '1',
'episodes_sync': '5',
'excluded_tags': '[]',
'excluded_series_types': '[]'
},
Expand Down
1 change: 0 additions & 1 deletion bazarr/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def db_upgrade():
['table_shows', 'seriesType', 'text', ''],
['table_shows', 'imdbId', 'text', ''],
['table_shows', 'profileId', 'integer'],
['table_shows', 'sizeOnDisk', 'integer'],
['table_episodes', 'format', 'text'],
['table_episodes', 'resolution', 'text'],
['table_episodes', 'video_codec', 'text'],
Expand Down
16 changes: 4 additions & 12 deletions bazarr/get_episodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,22 @@ def update_all_episodes():
logging.info('BAZARR All existing episode subtitles indexed from disk.')


def sync_episodes(series_id=None):
def sync_episodes():
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_list = [x['sonarrEpisodeId'] for x in current_episodes_db if x['sonarrSeriesId'] == series_id]
else:
current_episodes_db_list = [x['sonarrEpisodeId'] for x in current_episodes_db]
current_episodes_db_list = [x['sonarrEpisodeId'] for x in current_episodes_db]

current_episodes_sonarr = []
episodes_to_update = []
episodes_to_add = []
altered_episodes = []

if series_id and isinstance(series_id, int):
# Get sonarrId for each series from database
seriesIdList = database.execute("SELECT sonarrSeriesId, title FROM table_shows WHERE sonarrSeriesId = ?",
(series_id,))
else:
# Get sonarrId for each series from database
seriesIdList = database.execute("SELECT sonarrSeriesId, title FROM table_shows")
# Get sonarrId for each series from database
seriesIdList = database.execute("SELECT sonarrSeriesId, title FROM table_shows")

for i, seriesId in enumerate(seriesIdList):
# Get episodes data for a series from Sonarr
Expand Down
32 changes: 7 additions & 25 deletions bazarr/get_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from utils import get_sonarr_version
from helper import path_mappings
from event_handler import event_stream
from get_episodes import sync_episodes


def update_series():
Expand Down Expand Up @@ -57,7 +56,6 @@ def update_series():
current_shows_sonarr = []
series_to_update = []
series_to_add = []
episodes_to_sync = []

series_list_length = len(r.json())
for i, show in enumerate(r.json(), 1):
Expand Down Expand Up @@ -89,13 +87,6 @@ def update_series():
# Add shows in Sonarr to current shows list
current_shows_sonarr.append(show['id'])

# Get sizeOnDisk for show
sizeOnDisk = show['sizeOnDisk'] if 'sizeOnDisk' in show else 0
show_size_in_db = database.execute('SELECT sizeOnDisk FROM table_shows WHERE sonarrSeriesId = ?', (show['id'],))
if len(show_size_in_db):
if sizeOnDisk != show_size_in_db[0]['sizeOnDisk']:
episodes_to_sync.append(show['id'])

if show['id'] in current_shows_db_list:
series_to_update.append({'title': show["title"],
'path': show["path"],
Expand All @@ -110,8 +101,7 @@ def update_series():
'alternateTitles': alternate_titles,
'tags': str(tags),
'seriesType': show['seriesType'],
'imdbId': imdbId,
'sizeOnDisk': sizeOnDisk})
'imdbId': imdbId})
else:
series_to_add.append({'title': show["title"],
'path': show["path"],
Expand All @@ -127,22 +117,19 @@ def update_series():
'tags': str(tags),
'seriesType': show['seriesType'],
'imdbId': imdbId,
'profileId': serie_default_profile,
'sizeOnDisk': sizeOnDisk})
'profileId': serie_default_profile})

# Remove old series from DB
removed_series = list(set(current_shows_db_list) - set(current_shows_sonarr))

for series in removed_series:
database.execute("DELETE FROM table_shows WHERE sonarrSeriesId=?", (series,))
database.execute("DELETE FROM table_episodes WHERE sonarrSeriesId=?", (series,))
database.execute("DELETE FROM table_shows WHERE sonarrSeriesId=?",(series,))
event_stream(type='series', action='delete', series=series)

# Update existing series in DB
series_in_db_list = []
series_in_db = database.execute("SELECT title, path, tvdbId, sonarrSeriesId, overview, poster, fanart, "
"audio_language, sortTitle, year, alternateTitles, tags, seriesType, imdbId, "
"sizeOnDisk FROM table_shows")
"audio_language, sortTitle, year, alternateTitles, tags, seriesType, imdbId FROM table_shows")

for item in series_in_db:
series_in_db_list.append(item)
Expand All @@ -155,9 +142,6 @@ def update_series():
query.values + (updated_series['sonarrSeriesId'],))
event_stream(type='series', action='update', series=updated_series['sonarrSeriesId'])

if updated_series['sonarrSeriesId'] in episodes_to_sync:
sync_episodes(series_id=updated_series['sonarrSeriesId'])

# Insert new series in DB
for added_series in series_to_add:
query = dict_converter.convert(added_series)
Expand All @@ -166,15 +150,13 @@ def update_series():
query.question_marks + ''')''', query.values)
if result:
list_missing_subtitles(no=added_series['sonarrSeriesId'])
event_stream(type='series', action='insert', id=added_series['sonarrSeriesId'])

if added_series['sonarrSeriesId'] in episodes_to_sync:
sync_episodes(series_id=added_series['sonarrSeriesId'])
else:
logging.debug('BAZARR unable to insert this series into the database:',
path_mappings.path_replace(added_series['path']))

logging.debug('BAZARR All series synced from Sonarr into database.')
event_stream(type='series', action='insert', series=added_series['sonarrSeriesId'])

logging.debug('BAZARR All series synced from Sonarr into database.')


def get_profile_list():
Expand Down
3 changes: 0 additions & 3 deletions bazarr/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@
with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini')), 'w+') as handle:
settings.remove_option('general', 'throtteled_providers')
settings.remove_option('general', 'update_restart')
settings.remove_option('sonarr', 'episodes_sync')
if settings.sonarr.series_sync == '1':
settings.sonarr.series_sync = '5'
settings.write(handle)


Expand Down
11 changes: 8 additions & 3 deletions bazarr/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8

from get_episodes import update_all_episodes
from get_episodes import sync_episodes, update_all_episodes
from get_movies import update_movies, update_all_movies
from get_series import update_series
from config import settings
Expand Down Expand Up @@ -143,14 +143,18 @@ def __sonarr_update_task(self):
if settings.general.getboolean('use_sonarr'):
self.aps_scheduler.add_job(
update_series, IntervalTrigger(minutes=int(settings.sonarr.series_sync)), max_instances=1,
coalesce=True, misfire_grace_time=15, id='update_series', name='Sync with Sonarr',
coalesce=True, misfire_grace_time=15, id='update_series', name='Update Series list from Sonarr',
replace_existing=True)
self.aps_scheduler.add_job(
sync_episodes, IntervalTrigger(minutes=int(settings.sonarr.episodes_sync)), max_instances=1,
coalesce=True, misfire_grace_time=15, id='sync_episodes', name='Sync episodes with Sonarr',
replace_existing=True)

def __radarr_update_task(self):
if settings.general.getboolean('use_radarr'):
self.aps_scheduler.add_job(
update_movies, IntervalTrigger(minutes=int(settings.radarr.movies_sync)), max_instances=1,
coalesce=True, misfire_grace_time=15, id='update_movies', name='Sync with Radarr',
coalesce=True, misfire_grace_time=15, id='update_movies', name='Update Movie list from Radarr',
replace_existing=True)

def __cache_cleanup_task(self):
Expand Down Expand Up @@ -253,6 +257,7 @@ def __no_task(self):
if 'BAZARR_AUDIO_PROFILES_MIGRATION' in os.environ:
if settings.general.getboolean('use_sonarr'):
scheduler.aps_scheduler.modify_job('update_series', next_run_time=datetime.now())
scheduler.aps_scheduler.modify_job('sync_episodes', next_run_time=datetime.now())
if settings.general.getboolean('use_radarr'):
scheduler.aps_scheduler.modify_job('update_movies', next_run_time=datetime.now())
del os.environ['BAZARR_AUDIO_PROFILES_MIGRATION']
1 change: 1 addition & 0 deletions frontend/src/@types/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ namespace Settings {
full_update_hour: number;
only_monitored: boolean;
series_sync: number;
episodes_sync: number;
excluded_tags: string[];
excluded_series_types: SonarrSeriesType[];
}
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/Settings/Scheduler/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
dayOptions,
diskUpdateOptions,
episodesSyncOptions,
moviesSyncOptions,
seriesSyncOptions,
upgradeOptions,
Expand All @@ -27,13 +28,19 @@ const SettingsSchedulerView: FunctionComponent = () => {
return (
<SettingsProvider title="Scheduler - Bazarr (Settings)">
<Group header="Sonarr/Radarr Sync">
<Input name="Sync with Sonarr">
<Input name="Update Series List from Sonarr">
<Selector
options={seriesSyncOptions}
settingKey="settings-sonarr-series_sync"
></Selector>
</Input>
<Input name="Sync with Radarr">
<Input name="Update Episodes List from Sonarr">
<Selector
options={episodesSyncOptions}
settingKey="settings-sonarr-episodes_sync"
></Selector>
</Input>
<Input name="Update Movies List from Radarr">
<Selector
options={moviesSyncOptions}
settingKey="settings-radarr-movies_sync"
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/Settings/Scheduler/options.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
export const seriesSyncOptions: SelectorOption<number>[] = [
{ label: "1 Minute", value: 1 },
{ label: "5 Minutes", value: 5 },
{ label: "15 Minutes", value: 15 },
{ label: "1 Hour", value: 60 },
{ label: "3 Hours", value: 180 },
{ label: "6 Hours", value: 360 },
];

export const moviesSyncOptions: SelectorOption<number>[] = [
export const episodesSyncOptions: SelectorOption<number>[] = [
{ label: "5 Minutes", value: 5 },
{ label: "15 Minutes", value: 15 },
{ label: "1 Hour", value: 60 },
{ label: "3 Hours", value: 180 },
{ label: "6 Hours", value: 360 },
];

export const moviesSyncOptions = episodesSyncOptions;

export const diskUpdateOptions: SelectorOption<string>[] = [
{ label: "Manually", value: "Manually" },
{ label: "Daily", value: "Daily" },
Expand Down

0 comments on commit 97caf44

Please sign in to comment.