def download_best_subtitles(video_part_map,
                            min_score=0,
                            throttle_time=None,
                            providers=None):
    hearing_impaired = Prefs['subtitles.search.hearingImpaired']
    languages = set([Language.rebuild(l) for l in config.lang_list])
    if not languages:
        return

    use_videos = []
    missing_languages = set()
    for video, part in video_part_map.iteritems():
        if not video.ignore_all:
            p_missing_languages = get_missing_languages(video, part)
        else:
            p_missing_languages = languages

        if p_missing_languages:
            Log.Info(u"%s has missing languages: %s",
                     os.path.basename(video.name), p_missing_languages)
            refine_video(video, refiner_settings=config.refiner_settings)
            use_videos.append(video)
            missing_languages.update(p_missing_languages)

    # prepare blacklist
    blacklist = get_blacklist_from_part_map(video_part_map, languages)

    if use_videos and missing_languages:
        Log.Debug(
            "Download best subtitles using settings: min_score: %s, hearing_impaired: %s, languages: %s"
            % (min_score, hearing_impaired, missing_languages))

        return subliminal.download_best_subtitles(
            set(use_videos),
            missing_languages,
            min_score,
            hearing_impaired,
            providers=providers or config.providers,
            provider_configs=config.provider_settings,
            pool_class=config.provider_pool,
            compute_score=compute_score,
            throttle_time=throttle_time,
            blacklist=blacklist,
            throttle_callback=config.provider_throttle,
            pre_download_hook=pre_download_hook,
            post_download_hook=post_download_hook,
            language_hook=language_hook)
    Log.Debug("All languages for all requested videos exist. Doing nothing.")
Example #2
0
    def list_subtitles(self,
                       rating_key,
                       item_type,
                       part_id,
                       language,
                       skip_wrong_fps=True,
                       metadata=None,
                       scanned_parts=None,
                       air_date_cutoff=None):

        if not metadata:
            metadata = get_plex_metadata(rating_key, part_id, item_type)

        if not metadata:
            return

        providers = config.get_providers(
            media_type="series" if item_type == "episode" else "movies")
        if not scanned_parts:
            scanned_parts = scan_videos([metadata],
                                        ignore_all=True,
                                        providers=providers)
            if not scanned_parts:
                Log.Error(u"%s: Couldn't list available subtitles for %s",
                          self.name, rating_key)
                return

        video, plex_part = scanned_parts.items()[0]
        refine_video(video, refiner_settings=config.refiner_settings)

        if air_date_cutoff is not None and metadata["item"].year and \
            metadata["item"].year + air_date_cutoff < datetime.date.today().year:
            Log.Debug(
                "Skipping searching for subtitles: %s, it aired over %s year(s) ago.",
                rating_key, air_date_cutoff)
            return

        config.init_subliminal_patches()

        provider_settings = config.provider_settings
        if not skip_wrong_fps:
            provider_settings["opensubtitles"]["skip_wrong_fps"] = False

        if item_type == "episode":
            min_score = 240
            if video.is_special:
                min_score = 180
        else:
            min_score = 60

        languages = {Language.fromietf(language)}

        available_subs = list_all_subtitles(
            [video],
            languages,
            providers=providers,
            provider_configs=provider_settings,
            pool_class=config.provider_pool,
            throttle_callback=config.provider_throttle,
            language_hook=language_hook)

        use_hearing_impaired = Prefs['subtitles.search.hearingImpaired'] in (
            "prefer", "force HI")

        # sort subtitles by score
        unsorted_subtitles = []
        for s in available_subs[video]:
            Log.Debug(u"%s: Starting score computation for %s", self.name, s)
            try:
                matches = s.get_matches(video)
            except AttributeError:
                Log.Error(u"%s: Match computation failed for %s: %s",
                          self.name, s, traceback.format_exc())
                continue

            # skip wrong season/episodes
            if item_type == "episode":
                can_verify_series = True
                if not s.hash_verifiable and "hash" in matches:
                    can_verify_series = False

                if can_verify_series and not {"series", "season", "episode"
                                              }.issubset(matches):
                    if "series" not in matches:
                        s.wrong_series = True
                    else:
                        s.wrong_season_ep = True

            unsorted_subtitles.append(
                (s,
                 compute_score(matches,
                               s,
                               video,
                               hearing_impaired=use_hearing_impaired),
                 matches))
        scored_subtitles = sorted(unsorted_subtitles,
                                  key=operator.itemgetter(1),
                                  reverse=True)

        subtitles = []
        for subtitle, score, matches in scored_subtitles:
            # check score
            if score < min_score and not subtitle.wrong_series:
                Log.Info(u'%s: Score %d is below min_score (%d)', self.name,
                         score, min_score)
                continue
            subtitle.score = score
            subtitle.matches = matches
            subtitle.part_id = part_id
            subtitle.item_type = item_type
            subtitles.append(subtitle)
        return subtitles