Ejemplo n.º 1
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
Ejemplo n.º 2
0
def manual_search(path, profile_id, providers, sceneName, title, media_type):
    logging.debug('BAZARR Manually searching subtitles for this file: ' + path)

    final_subtitles = []

    pool = _get_pool(media_type, profile_id)

    language_set, initial_language_set = _get_language_obj(
        profile_id=profile_id)
    also_forced = any([x.forced for x in initial_language_set])
    _set_forced_providers(also_forced=also_forced, pool=pool)

    if providers:
        video = get_video(force_unicode(path),
                          title,
                          sceneName,
                          providers=providers,
                          media_type=media_type)
    else:
        logging.info("BAZARR All providers are throttled")
        return None
    if video:
        handler = series_score if media_type == "series" else movie_score

        try:
            if providers:
                subtitles = list_all_subtitles([video], language_set, pool)

                if 'subscene' in providers:
                    s_pool = _init_pool("movie", profile_id, {"subscene"})

                    subscene_language_set = set()
                    for language in language_set:
                        if language.forced:
                            subscene_language_set.add(language)
                    if len(subscene_language_set):
                        s_pool.provider_configs['subscene'] = {}
                        s_pool.provider_configs['subscene'][
                            'only_foreign'] = True
                        subtitles_subscene = list_all_subtitles(
                            [video], subscene_language_set, s_pool)
                        s_pool.provider_configs['subscene'][
                            'only_foreign'] = False
                        subtitles[video] += subtitles_subscene[video]
            else:
                subtitles = []
                logging.info("BAZARR All providers are throttled")
                return None
        except Exception:
            logging.exception(
                "BAZARR Error trying to get Subtitle list from provider for this file: "
                + path)
        else:
            subtitles_list = []
            minimum_score = settings.general.minimum_score
            minimum_score_movie = settings.general.minimum_score_movie

            for s in subtitles[video]:
                try:
                    matches = s.get_matches(video)
                except AttributeError:
                    continue

                # skip wrong season/episodes
                if media_type == "series":
                    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):
                        logging.debug(
                            f"BAZARR Skipping {s}, because it doesn't match our series/episode"
                        )
                        continue

                initial_hi = None
                initial_hi_match = False
                for language in initial_language_set:
                    if s.language.basename == language.basename and \
                            s.language.forced == language.forced and \
                            s.language.hi == language.hi:
                        initial_hi = language.hi
                        initial_hi_match = True
                        break
                if not initial_hi_match:
                    initial_hi = None

                _, max_score, scores = _get_scores(media_type,
                                                   minimum_score_movie,
                                                   minimum_score)
                score, score_without_hash = compute_score(
                    matches,
                    s,
                    video,
                    hearing_impaired=initial_hi,
                    score_obj=handler)
                if 'hash' not in matches:
                    not_matched = scores - matches
                    s.score = score_without_hash
                else:
                    s.score = score
                    not_matched = set()

                if s.hearing_impaired == initial_hi:
                    matches.add('hearing_impaired')
                else:
                    not_matched.add('hearing_impaired')

                releases = []
                if hasattr(s, 'release_info'):
                    if s.release_info is not None:
                        for s_item in s.release_info.split(','):
                            if s_item.strip():
                                releases.append(s_item)

                if s.uploader and s.uploader.strip():
                    s_uploader = s.uploader.strip()
                else:
                    s_uploader = None

                subtitles_list.append(
                    dict(score=round((score / max_score * 100), 2),
                         orig_score=score,
                         score_without_hash=score_without_hash,
                         forced=str(s.language.forced),
                         language=str(s.language.basename),
                         hearing_impaired=str(s.hearing_impaired),
                         provider=s.provider_name,
                         subtitle=codecs.encode(
                             pickle.dumps(s.make_picklable()),
                             "base64").decode(),
                         url=s.page_link,
                         matches=list(matches),
                         dont_matches=list(not_matched),
                         release_info=releases,
                         uploader=s_uploader))

            final_subtitles = sorted(
                subtitles_list,
                key=lambda x: (x['orig_score'], x['score_without_hash']),
                reverse=True)
            logging.debug('BAZARR ' + str(len(final_subtitles)) +
                          " Subtitles have been found for this file: " + path)
            logging.debug('BAZARR Ended searching Subtitles for this file: ' +
                          path)

    subliminal.region.backend.sync()

    return final_subtitles
Ejemplo n.º 3
0
    def list_subtitles(self,
                       rating_key,
                       item_type,
                       part_id,
                       language,
                       skip_wrong_fps=True,
                       metadata=None,
                       scanned_parts=None):

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

        if not metadata:
            return

        if not scanned_parts:
            scanned_parts = scan_videos(
                [metadata],
                kind="series" if item_type == "episode" else "movie",
                ignore_all=True)
            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]
        config.init_subliminal_patches()

        provider_settings = config.provider_settings.copy()
        if not skip_wrong_fps:
            provider_settings = config.provider_settings.copy()
            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

        available_subs = list_all_subtitles(scanned_parts,
                                            {Language.fromietf(language)},
                                            providers=config.providers,
                                            provider_configs=provider_settings,
                                            pool_class=config.provider_pool)

        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

            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:
                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
Ejemplo n.º 4
0
def manual_search(path, language, hi, providers, providers_auth, sceneName,
                  title, media_type):
    logging.debug('BAZARR Manually searching subtitles for this file: ' + path)

    final_subtitles = []

    if hi == "True":
        hi = True
    else:
        hi = False
    language_set = set()
    for lang in ast.literal_eval(language):
        lang = alpha3_from_alpha2(lang)
        if lang == 'pob':
            language_set.add(Language('por', 'BR'))
        else:
            language_set.add(Language(lang))

    use_scenename = settings.general.getboolean('use_scenename')
    minimum_score = settings.general.minimum_score
    minimum_score_movie = settings.general.minimum_score_movie
    use_postprocessing = settings.general.getboolean('use_postprocessing')
    postprocessing_cmd = settings.general.postprocessing_cmd

    video = get_video(path,
                      title,
                      sceneName,
                      use_scenename,
                      providers=providers,
                      media_type=media_type)
    if video:
        min_score, max_score, scores = get_scores(
            video,
            media_type,
            min_score_movie_perc=int(minimum_score_movie),
            min_score_series_perc=int(minimum_score))

        try:
            if providers:
                subtitles = list_subtitles([video],
                                           language_set,
                                           providers=providers,
                                           provider_configs=providers_auth,
                                           pool_class=provider_pool(),
                                           throttle_callback=provider_throttle,
                                           language_hook=None)  # fixme
            else:
                subtitles = []
                logging.info("BAZARR All providers are throttled")
        except Exception as e:
            logging.exception(
                "BAZARR Error trying to get subtitle list from provider for this file: "
                + path)
        else:
            subtitles_list = []

            for s in subtitles[video]:
                try:
                    matches = s.get_matches(video)
                except AttributeError:
                    continue

                # skip wrong season/episodes
                if media_type == "series":
                    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):
                        logging.debug(
                            u"BAZARR Skipping %s, because it doesn't match our series/episode",
                            s)
                        continue

                score = compute_score(matches, s, video, hearing_impaired=hi)
                not_matched = scores - matches
                s.score = score
                # if score < min_score:
                #     continue

                subtitles_list.append(
                    dict(score=round((score / max_score * 100), 2),
                         language=alpha2_from_alpha3(s.language.alpha3),
                         hearing_impaired=str(s.hearing_impaired),
                         provider=s.provider_name,
                         subtitle=codecs.encode(
                             pickle.dumps(s.make_picklable()),
                             "base64").decode(),
                         url=s.page_link,
                         matches=list(matches),
                         dont_matches=list(not_matched)))

            final_subtitles = sorted(subtitles_list,
                                     key=lambda x: x['score'],
                                     reverse=True)
            logging.debug('BAZARR ' + str(len(final_subtitles)) +
                          " subtitles have been found for this file: " + path)
            logging.debug('BAZARR Ended searching subtitles for this file: ' +
                          path)
    return final_subtitles