Skip to content

Commit

Permalink
Fixed some stuff and improved debug logging of upgradable subtitles d…
Browse files Browse the repository at this point in the history
…etection.
  • Loading branch information
morpheus65535 committed Nov 20, 2024
1 parent d2584d4 commit d0fca47
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
11 changes: 2 additions & 9 deletions bazarr/api/episodes/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,13 @@ def get(self):
} for x in database.execute(stmt).all()]

for item in episode_history:
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
# is this language still desired or should we simply skip this subtitles from upgrade logic?
still_desired = _language_still_desired(item['language'], item['profileId'])

item.update(postprocess(item))

# Mark upgradeable and get original_id
item.update({'original_id': upgradable_episodes_not_perfect.get(item['id'])})
language = item['language']['code2']
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
Expand Down
11 changes: 2 additions & 9 deletions bazarr/api/movies/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,13 @@ def get(self):
} for x in database.execute(stmt).all()]

for item in movie_history:
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
# is this language still desired or should we simply skip this subtitles from upgrade logic?
still_desired = _language_still_desired(item['language'], item['profileId'])

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']
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
Expand Down
33 changes: 32 additions & 1 deletion bazarr/subtitles/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from datetime import datetime, timedelta
from functools import reduce

from sqlalchemy import and_

from app.config import settings
Expand Down Expand Up @@ -269,10 +268,12 @@ def parse_language_string(language_string):
def get_upgradable_episode_subtitles():
if not settings.general.upgrade_subs:
# return an empty set of rows
logging.debug("Subtitles upgrade is disabled so we wont go further.")
return select(TableHistory.id) \
.where(TableHistory.id.is_(None)) \
.subquery()

logging.debug("Determining upgradable episode subtitles")
max_id_timestamp = select(TableHistory.video_path,
TableHistory.language,
func.max(TableHistory.timestamp).label('timestamp')) \
Expand All @@ -281,6 +282,8 @@ def get_upgradable_episode_subtitles():
.subquery()

minimum_timestamp, query_actions = get_queries_condition_parameters()
logging.debug(f"Minimum timestamp used for subtitles upgrade: {minimum_timestamp}")
logging.debug(f"Those actions are considered for subtitles upgrade: {query_actions}")

upgradable_episodes_conditions = [(TableHistory.action.in_(query_actions)),
(TableHistory.timestamp > minimum_timestamp),
Expand All @@ -301,42 +304,56 @@ def get_upgradable_episode_subtitles():
.where(reduce(operator.and_, upgradable_episodes_conditions))
.order_by(TableHistory.timestamp.desc())) \
.all()
logging.debug(f"{len(subtitles_to_upgrade)} subtitles are candidates and we've selected the latest timestamp for "
f"each of them.")

query_actions_without_upgrade = [x for x in query_actions if x != 3]
upgradable_episode_subtitles = {}
for subtitle_to_upgrade in subtitles_to_upgrade:
# check if we have the original subtitles id in database and use it instead of guessing
if subtitle_to_upgrade.upgradedFromId:
upgradable_episode_subtitles.update({subtitle_to_upgrade.id: subtitle_to_upgrade.upgradedFromId})
logging.debug(f"The original subtitles ID for TableHistory ID {subtitle_to_upgrade.id} stored in DB is: "
f"{subtitle_to_upgrade.upgradedFromId}")
continue

# if not, we have to try to guess the original subtitles id
logging.debug("We don't have the original subtitles ID for this subtitle so we'll have to guess it.")
potential_parents = database.execute(
select(TableHistory.id, TableHistory.action)
.where(TableHistory.video_path == subtitle_to_upgrade.video_path,
TableHistory.language == subtitle_to_upgrade.language,)
.order_by(TableHistory.timestamp.desc())
).all()

logging.debug(f"The potential original subtitles IDs for TableHistory ID {subtitle_to_upgrade.id} are: "
f"{[x.id for x in potential_parents]}")
confirmed_parent = None
for potential_parent in potential_parents:
if potential_parent.action in query_actions_without_upgrade:
confirmed_parent = potential_parent.id
logging.debug(f"This ID is the first one to match selected query actions so it's been selected as "
f"original subtitles ID: {potential_parent.id}")
break

if confirmed_parent not in upgradable_episode_subtitles.values():
logging.debug("We haven't defined this ID as original subtitles ID for any other ID so we'll add it to "
"upgradable episode subtitles.")
upgradable_episode_subtitles.update({subtitle_to_upgrade.id: confirmed_parent})

logging.debug(f"We've found {len(upgradable_episode_subtitles)} episode subtitles IDs to be upgradable")
return upgradable_episode_subtitles


def get_upgradable_movies_subtitles():
if not settings.general.upgrade_subs:
# return an empty set of rows
logging.debug("Subtitles upgrade is disabled so we wont go further.")
return select(TableHistoryMovie.id) \
.where(TableHistoryMovie.id.is_(None)) \
.subquery()

logging.debug("Determining upgradable movie subtitles")
max_id_timestamp = select(TableHistoryMovie.video_path,
TableHistoryMovie.language,
func.max(TableHistoryMovie.timestamp).label('timestamp')) \
Expand All @@ -345,6 +362,8 @@ def get_upgradable_movies_subtitles():
.subquery()

minimum_timestamp, query_actions = get_queries_condition_parameters()
logging.debug(f"Minimum timestamp used for subtitles upgrade: {minimum_timestamp}")
logging.debug(f"Those actions are considered for subtitles upgrade: {query_actions}")

upgradable_movies_conditions = [(TableHistoryMovie.action.in_(query_actions)),
(TableHistoryMovie.timestamp > minimum_timestamp),
Expand All @@ -364,32 +383,44 @@ def get_upgradable_movies_subtitles():
.where(reduce(operator.and_, upgradable_movies_conditions))
.order_by(TableHistoryMovie.timestamp.desc())) \
.all()
logging.debug(f"{len(subtitles_to_upgrade)} subtitles are candidates and we've selected the latest timestamp for "
f"each of them.")

query_actions_without_upgrade = [x for x in query_actions if x != 3]
upgradable_movie_subtitles = {}
for subtitle_to_upgrade in subtitles_to_upgrade:
# check if we have the original subtitles id in database and use it instead of guessing
if subtitle_to_upgrade.upgradedFromId:
upgradable_movie_subtitles.update({subtitle_to_upgrade.id: subtitle_to_upgrade.upgradedFromId})
logging.debug(f"The original subtitles ID for TableHistoryMovie ID {subtitle_to_upgrade.id} stored in DB "
f"is: {subtitle_to_upgrade.upgradedFromId}")
continue

# if not, we have to try to guess the original subtitles id
logging.debug("We don't have the original subtitles ID for this subtitle so we'll have to guess it.")
potential_parents = database.execute(
select(TableHistoryMovie.id, TableHistoryMovie.action)
.where(TableHistoryMovie.video_path == subtitle_to_upgrade.video_path,
TableHistoryMovie.language == subtitle_to_upgrade.language, )
.order_by(TableHistoryMovie.timestamp.desc())
).all()

logging.debug(f"The potential original subtitles IDs for TableHistoryMovie ID {subtitle_to_upgrade.id} are: "
f"{[x.id for x in potential_parents]}")
confirmed_parent = None
for potential_parent in potential_parents:
if potential_parent.action in query_actions_without_upgrade:
confirmed_parent = potential_parent.id
logging.debug(f"This ID is the first one to match selected query actions so it's been selected as "
f"original subtitles ID: {potential_parent.id}")
break

if confirmed_parent not in upgradable_movie_subtitles.values():
logging.debug("We haven't defined this ID as original subtitles ID for any other ID so we'll add it to "
"upgradable episode subtitles.")
upgradable_movie_subtitles.update({subtitle_to_upgrade.id: confirmed_parent})

logging.debug(f"We've found {len(upgradable_movie_subtitles)} movie subtitles IDs to be upgradable")
return upgradable_movie_subtitles


Expand Down

0 comments on commit d0fca47

Please sign in to comment.