1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-26 01:27:07 +00:00
bazarr/libs/subliminal_patch/score.py

133 lines
4.8 KiB
Python
Raw Normal View History

2018-10-31 16:08:29 +00:00
# coding=utf-8
2019-09-17 02:04:27 +00:00
from __future__ import absolute_import
2018-10-31 16:08:29 +00:00
import logging
from subliminal.video import Episode, Movie
from subliminal.score import get_scores
logger = logging.getLogger(__name__)
2021-06-10 20:00:42 +00:00
FPS_EQUALITY = ((23.976, 23.98, 24.0),)
2018-10-31 16:08:29 +00:00
def framerate_equal(source, check):
if source == check:
return True
source = float(source)
check = float(check)
if source == check:
return True
for equal_fps_tuple in FPS_EQUALITY:
if check in equal_fps_tuple and source in equal_fps_tuple:
return True
return False
2021-06-10 20:00:42 +00:00
def compute_score(matches, subtitle, video, hearing_impaired=None, score_obj=None):
2018-10-31 16:08:29 +00:00
"""Compute the score of the `subtitle` against the `video` with `hearing_impaired` preference.
patch:
2018-10-31 16:08:29 +00:00
- remove upper bounds of score
- re-add matches argument and remove get_matches from here
:func:`compute_score` uses the :meth:`Subtitle.get_matches <subliminal.subtitle.Subtitle.get_matches>` method and
applies the scores (either from :data:`episode_scores` or :data:`movie_scores`) after some processing.
:param subtitle: the subtitle to compute the score of.
:type subtitle: :class:`~subliminal.subtitle.Subtitle`
:param video: the video to compute the score against.
:type video: :class:`~subliminal.video.Video`
:param bool hearing_impaired: hearing impaired preference.
:return: score of the subtitle.
:rtype: int
"""
logger.info('%r: Computing score for video %r with %r', subtitle, video, dict(hearing_impaired=hearing_impaired))
if score_obj is not None:
scores = score_obj.scores
score_obj.check_custom_profiles(subtitle, matches)
else:
scores = get_scores(video)
2018-10-31 16:08:29 +00:00
is_episode = isinstance(video, Episode)
is_movie = isinstance(video, Movie)
2020-05-20 15:29:39 +00:00
episode_hash_valid_if = {"series", "season", "episode", "source"}
movie_hash_valid_if = {"video_codec", "source"}
2018-10-31 16:08:29 +00:00
orig_matches = matches.copy()
2018-10-31 16:08:29 +00:00
# on hash match, discard everything else
2021-06-10 20:00:42 +00:00
if subtitle.hash_verifiable and 'hash' in matches:
# hash is error-prone, try to fix that
hash_valid_if = episode_hash_valid_if if is_episode else movie_hash_valid_if
# don't validate hashes of specials, as season and episode tend to be wrong
if is_movie or not video.is_special:
if hash_valid_if <= set(matches):
# series, season and episode matched, hash is valid
logger.debug('%r: Using valid hash, as %s are correct (%r) and (%r)', subtitle, hash_valid_if, matches,
video)
matches &= {'hash'}
else:
# no match, invalidate hash
logger.debug('%r: Ignoring hash as other matches are wrong (missing: %r) and (%r)', subtitle,
hash_valid_if - matches, video)
matches -= {"hash"}
2018-10-31 16:08:29 +00:00
elif 'hash' in matches:
logger.debug('%r: Hash not verifiable for this provider. Keeping it', subtitle)
matches &= {'hash'}
# handle equivalent matches
eq_matches = set()
2018-10-31 16:08:29 +00:00
if is_episode:
2021-06-10 20:00:42 +00:00
_episode_checks(video, eq_matches, matches)
elif is_movie and 'imdb_id' in matches:
logger.debug('Adding imdb_id match equivalents')
eq_matches |= {'title', 'year'}
matches |= eq_matches
2018-10-31 16:08:29 +00:00
# handle hearing impaired
if hearing_impaired is not None and subtitle.hearing_impaired == hearing_impaired:
2018-10-31 16:08:29 +00:00
logger.debug('Matched hearing_impaired')
matches.add('hearing_impaired')
orig_matches.add('hearing_impaired')
2018-10-31 16:08:29 +00:00
# compute the score
score = sum((scores.get(match, 0) for match in matches))
logger.info('%r: Computed score %r with final matches %r', subtitle, score, matches)
score_without_hash = sum((scores.get(match, 0) for match in orig_matches | eq_matches if match != "hash"))
return score, score_without_hash
2021-06-10 20:00:42 +00:00
def _episode_checks(video, eq_matches, matches):
if "title" in matches:
logger.debug("Adding title match equivalent")
eq_matches.add("episode")
if "series_imdb_id" in matches:
logger.debug("Adding series_imdb_id match equivalent")
eq_matches |= {"series", "year"}
if "imdb_id" in matches:
logger.debug("Adding imdb_id match equivalents")
eq_matches |= {"series", "year", "season", "episode"}
if "tvdb_id" in matches:
logger.debug("Adding tvdb_id match equivalents")
eq_matches |= {"series", "year", "season", "episode", "title"}
if "series_tvdb_id" in matches:
logger.debug("Adding series_tvdb_id match equivalents")
eq_matches |= {"series", "year"}
# specials
if video.is_special and "title" in matches and "series" in matches and "year" in matches:
logger.debug("Adding special title match equivalent")
eq_matches |= {"season", "episode"}