Пример #1
0
def _init_pool(media_type, profile_id=None, providers=None):
    pool = provider_pool()
    return pool(
        providers=providers or get_providers(),
        provider_configs=get_providers_auth(),
        blacklist=get_blacklist(media_type),
        throttle_callback=provider_throttle,
        ban_list=get_ban_list(profile_id),
        language_hook=None,
    )
Пример #2
0
def manual_download_subtitle(path, language, hi, subtitle, provider,
                             providers_auth, sceneName, title, media_type):
    logging.debug('BAZARR Manually downloading subtitles for this file: ' +
                  path)

    subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64"))
    use_scenename = settings.general.getboolean('use_scenename')
    use_postprocessing = settings.general.getboolean('use_postprocessing')
    postprocessing_cmd = settings.general.postprocessing_cmd
    single = settings.general.getboolean('single_language')
    video = get_video(path,
                      title,
                      sceneName,
                      use_scenename,
                      providers={provider},
                      media_type=media_type)
    if video:
        min_score, max_score, scores = get_scores(video, media_type)
        try:
            if provider:
                download_subtitles([subtitle],
                                   providers={provider},
                                   provider_configs=providers_auth,
                                   pool_class=provider_pool(),
                                   throttle_callback=provider_throttle)
                logging.debug(
                    'BAZARR Subtitles file downloaded for this file:' + path)
            else:
                logging.info("BAZARR All providers are throttled")
                return None
        except Exception as e:
            logging.exception(
                'BAZARR Error downloading subtitles for this file ' + path)
            return None
        else:
            if not subtitle.is_valid():
                notifications.write(
                    msg='No valid subtitles file found for this file: ' + path,
                    queue='get_subtitle')
                logging.exception(
                    'BAZARR No valid subtitles file found for this file: ' +
                    path)
                return
            logging.debug('BAZARR Subtitles file downloaded for this file:' +
                          path)
            try:
                score = round(subtitle.score / max_score * 100, 2)
                fld = get_target_folder(path)
                chmod = int(settings.general.chmod,
                            8) if not sys.platform.startswith(
                                'win') and settings.general.getboolean(
                                    'chmod_enabled') else None
                saved_subtitles = save_subtitles(
                    video.original_path,
                    [subtitle],
                    single=single,
                    tags=None,  # fixme
                    directory=fld,
                    chmod=chmod,
                    # formats=("srt", "vtt")
                    path_decoder=force_unicode)

            except Exception as e:
                logging.exception(
                    'BAZARR Error saving subtitles file to disk for this file:'
                    + path)
                return
            else:
                if saved_subtitles:
                    for saved_subtitle in saved_subtitles:
                        downloaded_provider = saved_subtitle.provider_name
                        if saved_subtitle.language == 'pt-BR':
                            downloaded_language_code3 = 'pob'
                        else:
                            downloaded_language_code3 = subtitle.language.alpha3
                        downloaded_language = language_from_alpha3(
                            downloaded_language_code3)
                        downloaded_language_code2 = alpha2_from_alpha3(
                            downloaded_language_code3)
                        downloaded_path = saved_subtitle.storage_path
                        logging.debug('BAZARR Subtitles file saved to disk: ' +
                                      downloaded_path)
                        message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(
                            score) + "% using manual search."

                        if use_postprocessing is True:
                            command = pp_replace(postprocessing_cmd, path,
                                                 downloaded_path,
                                                 downloaded_language,
                                                 downloaded_language_code2,
                                                 downloaded_language_code3)
                            try:
                                if os.name == 'nt':
                                    codepage = subprocess.Popen(
                                        "chcp",
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                                    # wait for the process to terminate
                                    out_codepage, err_codepage = codepage.communicate(
                                    )
                                    encoding = out_codepage.split(
                                        ':')[-1].strip()

                                process = subprocess.Popen(
                                    command,
                                    shell=True,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
                                # wait for the process to terminate
                                out, err = process.communicate()

                                if os.name == 'nt':
                                    out = out.decode(encoding)

                            except:
                                if out == "":
                                    logging.error(
                                        'BAZARR Post-processing result for file '
                                        + path +
                                        ' : Nothing returned from command execution'
                                    )
                                else:
                                    logging.error(
                                        'BAZARR Post-processing result for file '
                                        + path + ' : ' + out)
                            else:
                                if out == "":
                                    logging.info(
                                        'BAZARR Post-processing result for file '
                                        + path +
                                        ' : Nothing returned from command execution'
                                    )
                                else:
                                    logging.info(
                                        'BAZARR Post-processing result for file '
                                        + path + ' : ' + out)

                        if media_type == 'series':
                            reversed_path = path_replace_reverse(path)
                        else:
                            reversed_path = path_replace_reverse_movie(path)

                        return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score
                else:
                    logging.error(
                        "BAZARR Tried to manually download a subtitles for file: "
                        + path +
                        " but we weren't able to do (probably throttled by " +
                        str(subtitle.provider_name) +
                        ". Please retry later or select a subtitles from another provider."
                    )
                    return None
    logging.debug('BAZARR Ended manually downloading subtitles for file: ' +
                  path)
Пример #3
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
Пример #4
0
def download_subtitle(path,
                      language,
                      hi,
                      providers,
                      providers_auth,
                      sceneName,
                      title,
                      media_type,
                      forced_minimum_score=None,
                      is_upgrade=False):
    # fixme: supply all missing languages, not only one, to hit providers only once who support multiple languages in
    #  one query

    logging.debug('BAZARR Searching subtitles for this file: ' + path)
    if hi == "True":
        hi = True
    else:
        hi = False
    language_set = set()

    if not isinstance(language, types.ListType):
        language = [language]

    for l in language:
        if l == 'pob':
            language_set.add(Language('por', 'BR'))
        else:
            language_set.add(Language(l))

    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
    single = settings.general.getboolean('single_language')

    # todo:
    """
    AsyncProviderPool:
    implement:
        blacklist=None,
        pre_download_hook=None,
        post_download_hook=None,
        language_hook=None
    """

    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))

        if providers:
            if forced_minimum_score:
                min_score = int(forced_minimum_score) + 1
            downloaded_subtitles = download_best_subtitles(
                {video},
                language_set,
                int(min_score),
                hi,
                providers=providers,
                provider_configs=providers_auth,
                pool_class=provider_pool(),
                compute_score=compute_score,
                throttle_time=None,  # fixme
                blacklist=None,  # fixme
                throttle_callback=provider_throttle,
                pre_download_hook=None,  # fixme
                post_download_hook=None,  # fixme
                language_hook=None)  # fixme
        else:
            downloaded_subtitles = None
            logging.info("BAZARR All providers are throttled")

        saved_any = False
        if downloaded_subtitles:
            for video, subtitles in downloaded_subtitles.iteritems():
                if not subtitles:
                    continue

                try:
                    fld = get_target_folder(path)
                    chmod = int(settings.general.chmod,
                                8) if not sys.platform.startswith(
                                    'win') and settings.general.getboolean(
                                        'chmod_enabled') else None
                    saved_subtitles = save_subtitles(
                        video.original_path,
                        subtitles,
                        single=single,
                        tags=None,  # fixme
                        directory=fld,
                        chmod=chmod,
                        # formats=("srt", "vtt")
                        path_decoder=force_unicode)
                except Exception as e:
                    logging.exception(
                        'BAZARR Error saving subtitles file to disk for this file:'
                        + path)
                    pass
                else:
                    saved_any = True
                    for subtitle in saved_subtitles:
                        downloaded_provider = subtitle.provider_name
                        if subtitle.language == 'pt-BR':
                            downloaded_language_code3 = 'pob'
                        else:
                            downloaded_language_code3 = subtitle.language.alpha3
                        downloaded_language = language_from_alpha3(
                            downloaded_language_code3)
                        downloaded_language_code2 = alpha2_from_alpha3(
                            downloaded_language_code3)
                        downloaded_path = subtitle.storage_path
                        logging.debug('BAZARR Subtitles file saved to disk: ' +
                                      downloaded_path)
                        if is_upgrade:
                            action = "upgraded"
                        else:
                            action = "downloaded"
                        if video.used_scene_name:
                            message = downloaded_language + " subtitles " + action + " from " + downloaded_provider + " with a score of " + unicode(
                                round(subtitle.score * 100 / max_score, 2)
                            ) + "% using this scene name: " + sceneName
                        else:
                            message = downloaded_language + " subtitles " + action + " from " + downloaded_provider + " with a score of " + unicode(
                                round(subtitle.score * 100 / max_score,
                                      2)) + "% using filename guessing."

                        if use_postprocessing is True:
                            command = pp_replace(postprocessing_cmd, path,
                                                 downloaded_path,
                                                 downloaded_language,
                                                 downloaded_language_code2,
                                                 downloaded_language_code3)
                            try:
                                if os.name == 'nt':
                                    codepage = subprocess.Popen(
                                        "chcp",
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                                    # wait for the process to terminate
                                    out_codepage, err_codepage = codepage.communicate(
                                    )
                                    encoding = out_codepage.split(
                                        ':')[-1].strip()

                                process = subprocess.Popen(
                                    command,
                                    shell=True,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
                                # wait for the process to terminate
                                out, err = process.communicate()

                                if os.name == 'nt':
                                    out = out.decode(encoding)

                            except:
                                if out == "":
                                    logging.error(
                                        'BAZARR Post-processing result for file '
                                        + path +
                                        ' : Nothing returned from command execution'
                                    )
                                else:
                                    logging.error(
                                        'BAZARR Post-processing result for file '
                                        + path + ' : ' + out)
                            else:
                                if out == "":
                                    logging.info(
                                        'BAZARR Post-processing result for file '
                                        + path +
                                        ' : Nothing returned from command execution'
                                    )
                                else:
                                    logging.info(
                                        'BAZARR Post-processing result for file '
                                        + path + ' : ' + out)

                        # fixme: support multiple languages at once
                        if media_type == 'series':
                            reversed_path = path_replace_reverse(path)
                        else:
                            reversed_path = path_replace_reverse_movie(path)

                        return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score

        if not saved_any:
            logging.debug('BAZARR No subtitles were found for this file: ' +
                          path)
            return None
    logging.debug('BAZARR Ended searching subtitles for file: ' + path)