Esempio n. 1
0
def postprocessSeries(item):
    postprocess(item)
    # Parse audio language
    if 'audio_language' in item and item['audio_language'] is not None:
        item['audio_language'] = get_audio_profile_languages(series_id=item['sonarrSeriesId'])

    if 'alternateTitles' in item:
        if item['alternateTitles'] is None:
            item['alternativeTitles'] = []
        else:
            item['alternativeTitles'] = ast.literal_eval(item['alternateTitles'])
        del item["alternateTitles"]

    # Parse seriesType
    if 'seriesType' in item and item['seriesType'] is not None:
        item['seriesType'] = item['seriesType'].capitalize()

    if 'path' in item:
        item['path'] = path_mappings.path_replace(item['path'])

    # map poster and fanart to server proxy
    if 'poster' in item:
        poster = item['poster']
        item['poster'] = f"{base_url}/images/series{poster}" if poster else None

    if 'fanart' in item:
        fanart = item['fanart']
        item['fanart'] = f"{base_url}/images/series{fanart}" if fanart else None
Esempio n. 2
0
    def patch(self):
        sonarrSeriesId = request.args.get('seriesid')
        sonarrEpisodeId = request.args.get('episodeid')
        episodeInfo = TableEpisodes.select(TableEpisodes.path,
                                           TableEpisodes.scene_name,
                                           TableEpisodes.audio_language,
                                           TableShows.title) \
            .join(TableShows, on=(TableEpisodes.sonarrSeriesId == TableShows.sonarrSeriesId)) \
            .where(TableEpisodes.sonarrEpisodeId == sonarrEpisodeId)\
            .dicts()\
            .get_or_none()

        if not episodeInfo:
            return 'Episode not found', 500

        title = episodeInfo['title']
        episodePath = path_mappings.path_replace(episodeInfo['path'])
        sceneName = episodeInfo['scene_name'] or "None"

        language = request.form.get('language')
        hi = request.form.get('hi').capitalize()
        forced = request.form.get('forced').capitalize()

        audio_language_list = get_audio_profile_languages(episode_id=sonarrEpisodeId)
        if len(audio_language_list) > 0:
            audio_language = audio_language_list[0]['name']
        else:
            audio_language = None

        try:
            result = list(generate_subtitles(episodePath, [(language, hi, forced)], audio_language, sceneName,
                                             title, 'series', profile_id=get_profile_id(episode_id=sonarrEpisodeId)))
            if result:
                result = result[0]
                message = result[0]
                path = result[1]
                forced = result[5]
                if result[8]:
                    language_code = result[2] + ":hi"
                elif forced:
                    language_code = result[2] + ":forced"
                else:
                    language_code = result[2]
                provider = result[3]
                score = result[4]
                subs_id = result[6]
                subs_path = result[7]
                history_log(1, sonarrSeriesId, sonarrEpisodeId, message, path, language_code, provider, score, subs_id,
                            subs_path)
                send_notifications(sonarrSeriesId, sonarrEpisodeId, message)
                store_subtitles(path, episodePath)
            else:
                event_stream(type='episode', payload=sonarrEpisodeId)

        except OSError:
            pass

        return '', 204
Esempio n. 3
0
    def post(self):
        # Manual Download
        radarrId = request.args.get('radarrid')
        movieInfo = TableMovies.select(TableMovies.title,
                                       TableMovies.path,
                                       TableMovies.sceneName,
                                       TableMovies.audio_language) \
            .where(TableMovies.radarrId == radarrId) \
            .dicts() \
            .get()

        title = movieInfo['title']
        moviePath = path_mappings.path_replace_movie(movieInfo['path'])
        sceneName = movieInfo['sceneName']
        if sceneName is None: sceneName = "None"
        audio_language = movieInfo['audio_language']

        language = request.form.get('language')
        hi = request.form.get('hi').capitalize()
        forced = request.form.get('forced').capitalize()
        selected_provider = request.form.get('provider')
        subtitle = request.form.get('subtitle')

        providers_auth = get_providers_auth()

        audio_language_list = get_audio_profile_languages(movie_id=radarrId)
        if len(audio_language_list) > 0:
            audio_language = audio_language_list[0]['name']
        else:
            audio_language = 'None'

        try:
            result = manual_download_subtitle(moviePath, language, audio_language, hi, forced, subtitle,
                                              selected_provider, providers_auth, sceneName, title, 'movie',
                                              profile_id=get_profile_id(movie_id=radarrId))
            if result is not None:
                message = result[0]
                path = result[1]
                forced = result[5]
                if result[8]:
                    language_code = result[2] + ":hi"
                elif forced:
                    language_code = result[2] + ":forced"
                else:
                    language_code = result[2]
                provider = result[3]
                score = result[4]
                subs_id = result[6]
                subs_path = result[7]
                history_log_movie(2, radarrId, message, path, language_code, provider, score, subs_id, subs_path)
                if not settings.general.getboolean('dont_notify_manual_actions'):
                    send_notifications_movie(radarrId, message)
                store_subtitles_movie(path, moviePath)
        except OSError:
            pass

        return '', 204
Esempio n. 4
0
def postprocessEpisode(item):
    postprocess(item)
    if 'audio_language' in item and item['audio_language'] is not None:
        item['audio_language'] = get_audio_profile_languages(episode_id=item['sonarrEpisodeId'])

    if 'subtitles' in item:
        if item['subtitles'] is None:
            raw_subtitles = []
        else:
            raw_subtitles = ast.literal_eval(item['subtitles'])
        subtitles = []

        for subs in raw_subtitles:
            subtitle = subs[0].split(':')
            sub = {"name": language_from_alpha2(subtitle[0]),
                   "code2": subtitle[0],
                   "code3": alpha3_from_alpha2(subtitle[0]),
                   "path": path_mappings.path_replace(subs[1]),
                   "forced": False,
                   "hi": False}
            if len(subtitle) > 1:
                sub["forced"] = True if subtitle[1] == 'forced' else False
                sub["hi"] = True if subtitle[1] == 'hi' else False

            subtitles.append(sub)

        item.update({"subtitles": subtitles})

    # Parse missing subtitles
    if 'missing_subtitles' in item:
        if item['missing_subtitles'] is None:
            item['missing_subtitles'] = []
        else:
            item['missing_subtitles'] = ast.literal_eval(item['missing_subtitles'])
        for i, subs in enumerate(item['missing_subtitles']):
            subtitle = subs.split(':')
            item['missing_subtitles'][i] = {"name": language_from_alpha2(subtitle[0]),
                                            "code2": subtitle[0],
                                            "code3": alpha3_from_alpha2(subtitle[0]),
                                            "forced": False,
                                            "hi": False}
            if len(subtitle) > 1:
                item['missing_subtitles'][i].update({
                    "forced": True if subtitle[1] == 'forced' else False,
                    "hi": True if subtitle[1] == 'hi' else False
                })

    if 'scene_name' in item:
        item["sceneName"] = item["scene_name"]
        del item["scene_name"]

    if 'path' in item and item['path']:
        # Provide mapped path
        item['path'] = path_mappings.path_replace(item['path'])
Esempio n. 5
0
    def patch(self):
        # Download
        radarrId = request.args.get('radarrid')

        movieInfo = TableMovies.select(TableMovies.title,
                                       TableMovies.path,
                                       TableMovies.sceneName,
                                       TableMovies.audio_language)\
            .where(TableMovies.radarrId == radarrId)\
            .dicts()\
            .get_or_none()

        if not movieInfo:
            return 'Movie not found', 500

        moviePath = path_mappings.path_replace_movie(movieInfo['path'])
        sceneName = movieInfo['sceneName'] or 'None'

        title = movieInfo['title']
        audio_language = movieInfo['audio_language']

        language = request.form.get('language')
        hi = request.form.get('hi').capitalize()
        forced = request.form.get('forced').capitalize()

        audio_language_list = get_audio_profile_languages(movie_id=radarrId)
        if len(audio_language_list) > 0:
            audio_language = audio_language_list[0]['name']
        else:
            audio_language = None

        try:
            result = list(
                generate_subtitles(
                    moviePath, [(language, hi, forced)],
                    audio_language,
                    sceneName,
                    title,
                    'movie',
                    profile_id=get_profile_id(movie_id=radarrId)))
            if result:
                result = result[0]
                message = result[0]
                path = result[1]
                forced = result[5]
                if result[8]:
                    language_code = result[2] + ":hi"
                elif forced:
                    language_code = result[2] + ":forced"
                else:
                    language_code = result[2]
                provider = result[3]
                score = result[4]
                subs_id = result[6]
                subs_path = result[7]
                history_log_movie(1, radarrId, message, path, language_code,
                                  provider, score, subs_id, subs_path)
                send_notifications_movie(radarrId, message)
                store_subtitles_movie(path, moviePath)
            else:
                event_stream(type='movie', payload=radarrId)
        except OSError:
            pass

        return '', 204
Esempio n. 6
0
def _wanted_episode(episode):
    audio_language_list = get_audio_profile_languages(episode_id=episode['sonarrEpisodeId'])
    if len(audio_language_list) > 0:
        audio_language = audio_language_list[0]['name']
    else:
        audio_language = 'None'

    languages = []
    for language in ast.literal_eval(episode['missing_subtitles']):

        # confirm if language is still missing or if cutoff have been reached
        confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \
            .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \
            .dicts() \
            .get_or_none()
        if not confirmed_missing_subs:
            continue

        if language not in ast.literal_eval(confirmed_missing_subs['missing_subtitles']):
            continue

        if is_search_active(desired_language=language, attempt_string=episode['failedAttempts']):
            TableEpisodes.update({TableEpisodes.failedAttempts:
                                  updateFailedAttempts(desired_language=language,
                                                       attempt_string=episode['failedAttempts'])}) \
                .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \
                .execute()

            hi_ = "True" if language.endswith(':hi') else "False"
            forced_ = "True" if language.endswith(':forced') else "False"
            languages.append((language.split(":")[0], hi_, forced_))

        else:
            logging.debug(
                f"BAZARR Search is throttled by adaptive search for this episode {episode['path']} and "
                f"language: {language}")

    for result in generate_subtitles(path_mappings.path_replace(episode['path']),
                                     languages,
                                     audio_language,
                                     str(episode['scene_name']),
                                     episode['title'],
                                     'series'):
        if result:
            message = result[0]
            path = result[1]
            forced = result[5]
            if result[8]:
                language_code = result[2] + ":hi"
            elif forced:
                language_code = result[2] + ":forced"
            else:
                language_code = result[2]
            provider = result[3]
            score = result[4]
            subs_id = result[6]
            subs_path = result[7]
            store_subtitles(episode['path'], path_mappings.path_replace(episode['path']))
            history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path,
                        language_code, provider, score, subs_id, subs_path)
            event_stream(type='series', action='update', payload=episode['sonarrSeriesId'])
            event_stream(type='episode-wanted', action='delete', payload=episode['sonarrEpisodeId'])
            send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message)
Esempio n. 7
0
def postprocessMovie(item):
    postprocess(item)
    # Parse audio language
    if 'audio_language' in item and item['audio_language'] is not None:
        item['audio_language'] = get_audio_profile_languages(movie_id=item['radarrId'])

    # Parse alternate titles
    if 'alternativeTitles' in item:
        if item['alternativeTitles'] is None:
            item['alternativeTitles'] = []
        else:
            item['alternativeTitles'] = ast.literal_eval(item['alternativeTitles'])

    # Parse failed attempts
    if 'failedAttempts' in item:
        if item['failedAttempts']:
            item['failedAttempts'] = ast.literal_eval(item['failedAttempts'])

    # Parse subtitles
    if 'subtitles' in item:
        if item['subtitles'] is None:
            item['subtitles'] = []
        else:
            item['subtitles'] = ast.literal_eval(item['subtitles'])
        for i, subs in enumerate(item['subtitles']):
            language = subs[0].split(':')
            item['subtitles'][i] = {"path": path_mappings.path_replace_movie(subs[1]),
                                    "name": language_from_alpha2(language[0]),
                                    "code2": language[0],
                                    "code3": alpha3_from_alpha2(language[0]),
                                    "forced": False,
                                    "hi": False}
            if len(language) > 1:
                item['subtitles'][i].update({
                    "forced": True if language[1] == 'forced' else False,
                    "hi": True if language[1] == 'hi' else False
                })

        if settings.general.getboolean('embedded_subs_show_desired'):
            desired_lang_list = get_desired_languages(item['profileId'])
            item['subtitles'] = [x for x in item['subtitles'] if x['code2'] in desired_lang_list or x['path']]

        item['subtitles'] = sorted(item['subtitles'], key=itemgetter('name', 'forced'))

    # Parse missing subtitles
    if 'missing_subtitles' in item:
        if item['missing_subtitles'] is None:
            item['missing_subtitles'] = []
        else:
            item['missing_subtitles'] = ast.literal_eval(item['missing_subtitles'])
        for i, subs in enumerate(item['missing_subtitles']):
            language = subs.split(':')
            item['missing_subtitles'][i] = {"name": language_from_alpha2(language[0]),
                                            "code2": language[0],
                                            "code3": alpha3_from_alpha2(language[0]),
                                            "forced": False,
                                            "hi": False}
            if len(language) > 1:
                item['missing_subtitles'][i].update({
                    "forced": True if language[1] == 'forced' else False,
                    "hi": True if language[1] == 'hi' else False
                })

    # Provide mapped path
    if 'path' in item:
        if item['path']:
            item['path'] = path_mappings.path_replace_movie(item['path'])

    if 'subtitles_path' in item:
        # Provide mapped subtitles path
        item['subtitles_path'] = path_mappings.path_replace_movie(item['subtitles_path'])

    # map poster and fanart to server proxy
    if 'poster' in item:
        poster = item['poster']
        item['poster'] = f"{base_url}/images/movies{poster}" if poster else None

    if 'fanart' in item:
        fanart = item['fanart']
        item['fanart'] = f"{base_url}/images/movies{fanart}" if fanart else None
Esempio n. 8
0
def movies_download_subtitles(no):
    conditions = [(TableMovies.radarrId == no)]
    conditions += get_exclusion_clause('movie')
    movies = TableMovies.select(TableMovies.path,
                                TableMovies.missing_subtitles,
                                TableMovies.audio_language,
                                TableMovies.radarrId,
                                TableMovies.sceneName,
                                TableMovies.title,
                                TableMovies.tags,
                                TableMovies.monitored)\
        .where(reduce(operator.and_, conditions))\
        .dicts()
    if not len(movies):
        logging.debug(
            "BAZARR no movie with that radarrId can be found in database:",
            str(no))
        return
    else:
        movie = movies[0]

    if ast.literal_eval(movie['missing_subtitles']):
        count_movie = len(ast.literal_eval(movie['missing_subtitles']))
    else:
        count_movie = 0

    audio_language_list = get_audio_profile_languages(
        movie_id=movie['radarrId'])
    if len(audio_language_list) > 0:
        audio_language = audio_language_list[0]['name']
    else:
        audio_language = 'None'

    languages = []
    providers_list = None

    for i, language in enumerate(ast.literal_eval(movie['missing_subtitles'])):
        providers_list = get_providers()

        if language is not None:
            hi_ = "True" if language.endswith(':hi') else "False"
            forced_ = "True" if language.endswith(':forced') else "False"
            languages.append((language.split(":")[0], hi_, forced_))

        if providers_list:
            # confirm if language is still missing or if cutoff have been reached
            confirmed_missing_subs = TableMovies.select(TableMovies.missing_subtitles) \
                .where(TableMovies.radarrId == movie['radarrId']) \
                .dicts() \
                .get_or_none()
            if not confirmed_missing_subs:
                continue

            if language not in ast.literal_eval(
                    confirmed_missing_subs['missing_subtitles']):
                continue

            show_progress(id='movie_search_progress_{}'.format(no),
                          header='Searching missing subtitles...',
                          name=movie['title'],
                          value=i,
                          count=count_movie)

    if providers_list:
        for result in generate_subtitles(
                path_mappings.path_replace_movie(movie['path']), languages,
                audio_language, str(movie['sceneName']), movie['title'],
                'movie'):

            if result:
                message = result[0]
                path = result[1]
                forced = result[5]
                if result[8]:
                    language_code = result[2] + ":hi"
                elif forced:
                    language_code = result[2] + ":forced"
                else:
                    language_code = result[2]
                provider = result[3]
                score = result[4]
                subs_id = result[6]
                subs_path = result[7]
                store_subtitles_movie(
                    movie['path'],
                    path_mappings.path_replace_movie(movie['path']))
                history_log_movie(1, no, message, path, language_code,
                                  provider, score, subs_id, subs_path)
                send_notifications_movie(no, message)
    else:
        logging.info("BAZARR All providers are throttled")

    hide_progress(id='movie_search_progress_{}'.format(no))
Esempio n. 9
0
    def post(self):
        # Manual Download
        sonarrSeriesId = request.args.get('seriesid')
        sonarrEpisodeId = request.args.get('episodeid')
        episodeInfo = TableEpisodes.select(TableEpisodes.path,
                                           TableEpisodes.scene_name,
                                           TableShows.title) \
            .join(TableShows, on=(TableEpisodes.sonarrSeriesId == TableShows.sonarrSeriesId)) \
            .where(TableEpisodes.sonarrEpisodeId == sonarrEpisodeId) \
            .dicts() \
            .get_or_none()

        if not episodeInfo:
            return 'Episode not found', 500

        title = episodeInfo['title']
        episodePath = path_mappings.path_replace(episodeInfo['path'])
        sceneName = episodeInfo['scene_name'] or "None"

        hi = request.form.get('hi').capitalize()
        forced = request.form.get('forced').capitalize()
        selected_provider = request.form.get('provider')
        subtitle = request.form.get('subtitle')

        audio_language_list = get_audio_profile_languages(
            episode_id=sonarrEpisodeId)
        if len(audio_language_list) > 0:
            audio_language = audio_language_list[0]['name']
        else:
            audio_language = 'None'

        try:
            result = manual_download_subtitle(
                episodePath,
                audio_language,
                hi,
                forced,
                subtitle,
                selected_provider,
                sceneName,
                title,
                'series',
                profile_id=get_profile_id(episode_id=sonarrEpisodeId))
            if result is not None:
                message = result[0]
                path = result[1]
                forced = result[5]
                if result[8]:
                    language_code = result[2] + ":hi"
                elif forced:
                    language_code = result[2] + ":forced"
                else:
                    language_code = result[2]
                provider = result[3]
                score = result[4]
                subs_id = result[6]
                subs_path = result[7]
                history_log(2, sonarrSeriesId, sonarrEpisodeId, message, path,
                            language_code, provider, score, subs_id, subs_path)
                if not settings.general.getboolean(
                        'dont_notify_manual_actions'):
                    send_notifications(sonarrSeriesId, sonarrEpisodeId,
                                       message)
                store_subtitles(path, episodePath)
            return result, 201
        except OSError:
            pass

        return '', 204
Esempio n. 10
0
def series_download_subtitles(no):
    conditions = [(TableEpisodes.sonarrSeriesId == no),
                  (TableEpisodes.missing_subtitles != '[]')]
    conditions += get_exclusion_clause('series')
    episodes_details = TableEpisodes.select(TableEpisodes.path,
                                            TableEpisodes.missing_subtitles,
                                            TableEpisodes.monitored,
                                            TableEpisodes.sonarrEpisodeId,
                                            TableEpisodes.scene_name,
                                            TableShows.tags,
                                            TableShows.seriesType,
                                            TableEpisodes.audio_language,
                                            TableShows.title,
                                            TableEpisodes.season,
                                            TableEpisodes.episode,
                                            TableEpisodes.title.alias('episodeTitle')) \
        .join(TableShows, on=(TableEpisodes.sonarrSeriesId == TableShows.sonarrSeriesId)) \
        .where(reduce(operator.and_, conditions)) \
        .dicts()
    if not episodes_details:
        logging.debug(
            "BAZARR no episode for that sonarrSeriesId have been found in database or they have all been "
            "ignored because of monitored status, series type or series tags: {}"
            .format(no))
        return

    count_episodes_details = len(episodes_details)

    for i, episode in enumerate(episodes_details):
        providers_list = get_providers()

        if providers_list:
            show_progress(id='series_search_progress_{}'.format(no),
                          header='Searching missing subtitles...',
                          name='{0} - S{1:02d}E{2:02d} - {3}'.format(
                              episode['title'], episode['season'],
                              episode['episode'], episode['episodeTitle']),
                          value=i,
                          count=count_episodes_details)

            audio_language_list = get_audio_profile_languages(
                episode_id=episode['sonarrEpisodeId'])
            if len(audio_language_list) > 0:
                audio_language = audio_language_list[0]['name']
            else:
                audio_language = 'None'

            languages = []
            for language in ast.literal_eval(episode['missing_subtitles']):
                # confirm if language is still missing or if cutoff have been reached
                confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \
                    .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \
                    .dicts() \
                    .get_or_none()
                if not confirmed_missing_subs:
                    continue

                if language not in ast.literal_eval(
                        confirmed_missing_subs['missing_subtitles']):
                    continue

                if language is not None:
                    hi_ = "True" if language.endswith(':hi') else "False"
                    forced_ = "True" if language.endswith(
                        ':forced') else "False"
                    languages.append((language.split(":")[0], hi_, forced_))

            if not languages:
                continue

            for result in generate_subtitles(
                    path_mappings.path_replace(episode['path']), languages,
                    audio_language, str(episode['scene_name']),
                    episode['title'], 'series'):
                if result:
                    message = result[0]
                    path = result[1]
                    forced = result[5]
                    if result[8]:
                        language_code = result[2] + ":hi"
                    elif forced:
                        language_code = result[2] + ":forced"
                    else:
                        language_code = result[2]
                    provider = result[3]
                    score = result[4]
                    subs_id = result[6]
                    subs_path = result[7]
                    store_subtitles(
                        episode['path'],
                        path_mappings.path_replace(episode['path']))
                    history_log(1, no, episode['sonarrEpisodeId'], message,
                                path, language_code, provider, score, subs_id,
                                subs_path)
                    send_notifications(no, episode['sonarrEpisodeId'], message)
        else:
            logging.info("BAZARR All providers are throttled")
            break

    hide_progress(id='series_search_progress_{}'.format(no))