Skip to content

Commit

Permalink
scoring adjustments moved into compute_score() and some logging cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Nov 30, 2014
1 parent bc575ea commit 7fef3cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
24 changes: 15 additions & 9 deletions subliminal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def list_subtitles(videos, languages, providers=None, provider_configs=None):
except:
logger.exception('Unexpected error in provider %r', provider_entry_point.name)
continue
logger.info('Found %d subtitles', len(provider_subtitles))
logger.info('Found %d subtitle(s) on %s' % (
len(provider_subtitles),
provider_entry_point.name,
))
subtitles[provider_video].extend(provider_subtitles)
except ProviderNotAvailable:
logger.warning('Provider %r is not available, discarding it', provider_entry_point.name)
Expand Down Expand Up @@ -207,10 +210,10 @@ def download_best_subtitles(videos, languages, providers=None, provider_configs=
continue
Provider = provider_entry_point.load()
if not Provider.languages & languages - subtitle_languages:
logger.info('Skipping provider %r: no language to search for', provider_entry_point.name)
logger.debug('Skipping provider %r: no language to search for', provider_entry_point.name)
continue
if not [v for v in videos if Provider.check(v)]:
logger.info('Skipping provider %r: no video to search for', provider_entry_point.name)
logger.debug('Skipping provider %r: video type not hosted here.', provider_entry_point.name)
continue
provider = Provider(**provider_configs.get(provider_entry_point.name, {}))
try:
Expand Down Expand Up @@ -253,27 +256,30 @@ def download_best_subtitles(videos, languages, providers=None, provider_configs=
except:
logger.exception('Unexpected error in provider %r', provider_name)
continue
logger.info('Found %d subtitles', len(provider_subtitles))
logger.info('Found %d subtitle(s) on %s' % (
len(provider_subtitles),
provider_name,
))
subtitles.extend(provider_subtitles)

# find the best subtitles and download them
for subtitle, score in sorted([(s, s.compute_score(video)) for s in subtitles],
key=operator.itemgetter(1), reverse=True):
for subtitle, score in sorted([(s, s.compute_score(video, hi_score_adjust)) \
for s in subtitles], key=operator.itemgetter(1), reverse=True):

# filter
if subtitle.provider_name in discarded_providers:
logger.debug('Skipping subtitle from discarded provider %r', subtitle.provider_name)
continue

if hearing_impaired is not None:
if subtitle.hearing_impaired != hearing_impaired:
logger.debug('Skipping subtitle: hearing impaired != %r', hearing_impaired)
continue
elif subtitle.hearing_impaired and hi_score_adjust != 0:
# Priortization (adjust score)
score += hi_score_adjust

if score < min_score:
logger.debug('Skipping subtitle: score < %d', min_score)
continue

if subtitle.language in downloaded_languages:
logger.debug('Skipping subtitle: %r already downloaded', subtitle.language)
continue
Expand Down
13 changes: 11 additions & 2 deletions subliminal/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def compute_matches(self, video):
"""
raise NotImplementedError

def compute_score(self, video):
def compute_score(self, video, hi_score_adjust=0):
"""Compute the score of the subtitle against the `video`
There are equivalent matches so that a provider can match one element or its equivalent. This is
Expand All @@ -47,6 +47,7 @@ def compute_score(self, video):
:param video: the video to compute the score against
:type video: :class:`~subliminal.video.Video`
:param hi_score_adjust: adjust hearing impaired matched videos by this value
:return: score of the subtitle
:rtype: int
Expand All @@ -69,7 +70,15 @@ def compute_score(self, video):
matches -= set(['season', 'episode'])
# add other scores
score += sum([video.scores[match] for match in matches])
logger.info('Computed score %d with matches %r', score, initial_matches)

# Adjust scoring if hearing impaired subtitles are detected
if self.hearing_impaired and hi_score_adjust != 0:
logger.debug('Hearing impaired subtitle score adjusted ' + \
'by %d' % hi_score_adjust)
# Priortization (adjust score)
score += hi_score_adjust

logger.debug('Computed score %d with matches %r', score, initial_matches)
return score

def __repr__(self):
Expand Down

0 comments on commit 7fef3cf

Please sign in to comment.