Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored upgrade routine to bring logic out of db requests but into Python code. #2762

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions bazarr/api/episodes/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,10 @@ def get(self):
TableHistory.matched,
TableHistory.not_matched,
TableEpisodes.subtitles.label('external_subtitles'),
upgradable_episodes_not_perfect.c.id.label('upgradable'),
blacklisted_subtitles.c.subs_id.label('blacklisted')) \
.select_from(TableHistory) \
.join(TableShows, onclause=TableHistory.sonarrSeriesId == TableShows.sonarrSeriesId) \
.join(TableEpisodes, onclause=TableHistory.sonarrEpisodeId == TableEpisodes.sonarrEpisodeId) \
.join(upgradable_episodes_not_perfect, onclause=TableHistory.id == upgradable_episodes_not_perfect.c.id,
isouter=True) \
.join(blacklisted_subtitles, onclause=TableHistory.subs_id == blacklisted_subtitles.c.subs_id,
isouter=True) \
.where(reduce(operator.and_, query_conditions)) \
Expand All @@ -120,6 +117,7 @@ def get(self):
'sonarrSeriesId': x.sonarrSeriesId,
'path': x.path,
'language': x.language,
'profileId': x.profileId,
'score': x.score,
'tags': x.tags,
'action': x.action,
Expand All @@ -130,24 +128,36 @@ def get(self):
'matches': x.matched,
'dont_matches': x.not_matched,
'external_subtitles': [y[1] for y in ast.literal_eval(x.external_subtitles) if y[1]],
'upgradable': bool(x.upgradable) if _language_still_desired(x.language, x.profileId) else False,
'blacklisted': bool(x.blacklisted),
} for x in database.execute(stmt).all()]

for item in episode_history:
original_video_path = item['path']
original_subtitle_path = item['subtitles_path']
still_desired = True
if not _language_still_desired(item['language'], item['profileId']):
# this language isn't desired anymore so there's no point in upgrading this subtitles
still_desired = False
morpheus65535 marked this conversation as resolved.
Show resolved Hide resolved

item.update(postprocess(item))

# Mark not upgradable if score is perfect or if video/subtitles file doesn't exist anymore
# Mark upgradeable and get original_id
item.update({'original_id': upgradable_episodes_not_perfect.get(item['id'])})
language = item['language']['code2']
morpheus65535 marked this conversation as resolved.
Show resolved Hide resolved
if item['language']['hi']:
language += ':hi'
elif item['language']['forced']:
language += ':forced'
item.update({'upgradable': bool(item['original_id'])})

# Mark not upgradable if score or if video/subtitles file doesn't exist anymore
if item['upgradable']:
if original_subtitle_path not in item['external_subtitles'] or \
not item['video_path'] == original_video_path:
if (item['subtitles_path'] not in item['external_subtitles'] or item['video_path'] != item['path'] or
not still_desired):
item.update({"upgradable": False})

del item['path']
del item['video_path']
del item['external_subtitles']
del item['profileId']

if item['score']:
item['score'] = f"{round((int(item['score']) * 100 / 360), 2)}%"
Expand Down
26 changes: 18 additions & 8 deletions bazarr/api/movies/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,9 @@ def get(self):
TableHistoryMovie.not_matched,
TableMovies.profileId,
TableMovies.subtitles.label('external_subtitles'),
upgradable_movies_not_perfect.c.id.label('upgradable'),
blacklisted_subtitles.c.subs_id.label('blacklisted')) \
.select_from(TableHistoryMovie) \
.join(TableMovies) \
.join(upgradable_movies_not_perfect, onclause=TableHistoryMovie.id == upgradable_movies_not_perfect.c.id,
isouter=True) \
.join(blacklisted_subtitles, onclause=TableHistoryMovie.subs_id == blacklisted_subtitles.c.subs_id,
isouter=True) \
.where(reduce(operator.and_, query_conditions)) \
Expand All @@ -112,6 +109,7 @@ def get(self):
'monitored': x.monitored,
'path': x.path,
'language': x.language,
'profileId': x.profileId,
'tags': x.tags,
'score': x.score,
'subs_id': x.subs_id,
Expand All @@ -121,24 +119,36 @@ def get(self):
'matches': x.matched,
'dont_matches': x.not_matched,
'external_subtitles': [y[1] for y in ast.literal_eval(x.external_subtitles) if y[1]],
'upgradable': bool(x.upgradable) if _language_still_desired(x.language, x.profileId) else False,
'blacklisted': bool(x.blacklisted),
} for x in database.execute(stmt).all()]

for item in movie_history:
original_video_path = item['path']
original_subtitle_path = item['subtitles_path']
still_desired = True
if not _language_still_desired(item['language'], item['profileId']):
# this language isn't desired anymore so there's no point in upgrading this subtitles
still_desired = False

item.update(postprocess(item))

# Mark upgradeable and get original_id
item.update({'original_id': upgradable_movies_not_perfect.get(item['id'])})
language = item['language']['code2']
morpheus65535 marked this conversation as resolved.
Show resolved Hide resolved
if item['language']['hi']:
language += ':hi'
elif item['language']['forced']:
language += ':forced'
item.update({'upgradable': bool(item['original_id'])})

# Mark not upgradable if score or if video/subtitles file doesn't exist anymore
if item['upgradable']:
if original_subtitle_path not in item['external_subtitles'] or \
not item['video_path'] == original_video_path:
if (item['subtitles_path'] not in item['external_subtitles'] or item['video_path'] != item['path'] or
not still_desired):
item.update({"upgradable": False})

del item['path']
del item['video_path']
del item['external_subtitles']
del item['profileId']

if item['score']:
item['score'] = f"{round((int(item['score']) * 100 / 120), 2)}%"
Expand Down
11 changes: 11 additions & 0 deletions bazarr/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,21 @@ def postprocess(item):
if item.get('path'):
item['path'] = path_replace(item['path'])

if item.get('video_path'):
# Provide mapped video path for history
item['video_path'] = path_replace(item['video_path'])

if item.get('subtitles_path'):
# Provide mapped subtitles path
item['subtitles_path'] = path_replace(item['subtitles_path'])

if item.get('external_subtitles'):
# Provide mapped external subtitles paths for history
if isinstance(item['external_subtitles'], str):
item['external_subtitles'] = ast.literal_eval(item['external_subtitles'])
for i, subs in enumerate(item['external_subtitles']):
item['external_subtitles'][i] = path_replace(subs)

# map poster and fanart to server proxy
if item.get('poster') is not None:
poster = item['poster']
Expand Down
Loading