Example #1
0
def wanted_download_subtitles_movie(path):
    conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                              timeout=30)
    c_db = conn_db.cursor()
    movies_details = c_db.execute(
        "SELECT path, missing_subtitles, radarrId, radarrId, hearing_impaired, sceneName, failedAttempts FROM table_movies WHERE path = ? AND missing_subtitles != '[]'",
        (path_replace_reverse_movie(path), )).fetchall()
    c_db.close()

    providers_list = get_providers()
    providers_auth = get_providers_auth()

    for movie in movies_details:
        attempt = movie[6]
        if type(attempt) == unicode:
            attempt = ast.literal_eval(attempt)
        for language in ast.literal_eval(movie[1]):
            if attempt is None:
                attempt = []
                attempt.append([language, time.time()])
            else:
                att = zip(*attempt)[0]
                if language not in att:
                    attempt.append([language, time.time()])

            conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                                      timeout=30)
            c_db = conn_db.cursor()
            c_db.execute(
                'UPDATE table_movies SET failedAttempts = ? WHERE radarrId = ?',
                (unicode(attempt), movie[2]))
            conn_db.commit()
            c_db.close()

            for i in range(len(attempt)):
                if attempt[i][0] == language:
                    if search_active(attempt[i][1]) is True:
                        message = download_subtitle(
                            path_replace_movie(movie[0]),
                            str(alpha3_from_alpha2(language)),
                            movie[4], providers_list, providers_auth,
                            str(movie[5]), 'movie')
                        if message is not None:
                            store_subtitles_movie(path_replace_movie(movie[0]))
                            list_missing_subtitles_movies(movie[3])
                            history_log_movie(1, movie[3], message)
                            send_notifications_movie(movie[3], message)
                    else:
                        logging.info('BAZARR Search is not active for movie ' +
                                     movie[0] + ' Language: ' + attempt[i][0])
Example #2
0
def movies_download_subtitles(no):
    conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
    c_db = conn_db.cursor()
    movie = c_db.execute("SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired FROM table_movies WHERE radarrId = ?", (no,)).fetchone()
    c_db.close()

    providers_list = get_providers()
    providers_auth = get_providers_auth()

    for language in ast.literal_eval(movie[1]):
        if language is not None:
            message = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], providers_list, providers_auth, str(movie[3]), 'movie')
            if message is not None:
                store_subtitles_movie(path_replace_movie(movie[0]))
                history_log_movie(1, no, message)
                send_notifications_movie(no, message)
    list_missing_subtitles_movies(no)
Example #3
0
def movies_full_scan_subtitles():
    conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                              timeout=30)
    c_db = conn_db.cursor()
    movies = c_db.execute("SELECT path FROM table_movies").fetchall()
    c_db.close()

    for movie in movies:
        store_subtitles_movie(path_replace_movie(movie[0]))

    gc.collect()
Example #4
0
def movies_scan_subtitles(no):
    conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                              timeout=30)
    c_db = conn_db.cursor()
    movies = c_db.execute("SELECT path FROM table_movies WHERE radarrId = ?",
                          (no, )).fetchall()
    c_db.close()

    for movie in movies:
        store_subtitles_movie(path_replace_movie(movie[0]))

    list_missing_subtitles_movies(no)
Example #5
0
def update_movies():
    logging.debug('BAZARR Starting movie sync from Radarr.')
    from get_settings import get_radarr_settings
    url_radarr = get_radarr_settings()[6]
    apikey_radarr = get_radarr_settings()[4]
    movie_default_enabled = get_general_settings()[18]
    movie_default_language = get_general_settings()[19]
    movie_default_hi = get_general_settings()[20]

    if apikey_radarr == None:
        pass
    else:
        get_profile_list()

        # Get movies data from radarr
        url_radarr_api_movies = url_radarr + "/api/movie?apikey=" + apikey_radarr
        try:
            r = requests.get(url_radarr_api_movies, timeout=15, verify=False)
            r.raise_for_status()
        except requests.exceptions.HTTPError as errh:
            logging.exception(
                "BAZARR Error trying to get movies from Radarr. Http error.")
        except requests.exceptions.ConnectionError as errc:
            logging.exception(
                "BAZARR Error trying to get movies from Radarr. Connection Error."
            )
        except requests.exceptions.Timeout as errt:
            logging.exception(
                "BAZARR Error trying to get movies from Radarr. Timeout Error."
            )
        except requests.exceptions.RequestException as err:
            logging.exception("BAZARR Error trying to get movies from Radarr.")
        else:
            # Get current movies in DB
            db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                                 timeout=30)
            c = db.cursor()
            current_movies_db = c.execute(
                'SELECT tmdbId FROM table_movies').fetchall()
            db.close()

            current_movies_db_list = [x[0] for x in current_movies_db]
            current_movies_radarr = []
            movies_to_update = []
            movies_to_add = []

            for movie in r.json():
                if movie['hasFile'] is True:
                    if 'movieFile' in movie:
                        if movie["path"] != None and movie['movieFile'][
                                'relativePath'] != None:
                            try:
                                overview = unicode(movie['overview'])
                            except:
                                overview = ""
                            try:
                                poster_big = movie['images'][0]['url']
                                poster = os.path.splitext(
                                    poster_big)[0] + '-500' + os.path.splitext(
                                        poster_big)[1]
                            except:
                                poster = ""
                            try:
                                fanart = movie['images'][1]['url']
                            except:
                                fanart = ""

                            if 'sceneName' in movie['movieFile']:
                                sceneName = movie['movieFile']['sceneName']
                            else:
                                sceneName = None

                            # Add movies in radarr to current movies list
                            current_movies_radarr.append(
                                unicode(movie['tmdbId']))

                            # Detect file separator
                            if movie['path'][0] == "/":
                                separator = "/"
                            else:
                                separator = "\\"

                            if unicode(
                                    movie['tmdbId']) in current_movies_db_list:
                                movies_to_update.append(
                                    (movie["title"],
                                     movie["path"] + separator +
                                     movie['movieFile']['relativePath'],
                                     movie["tmdbId"], movie["id"], overview,
                                     poster, fanart,
                                     profile_id_to_language(
                                         movie['qualityProfileId']), sceneName,
                                     unicode(bool(movie['monitored'])),
                                     movie['sortTitle'], movie["tmdbId"]))
                            else:
                                if movie_default_enabled is True:
                                    movies_to_add.append(
                                        (movie["title"],
                                         movie["path"] + separator +
                                         movie['movieFile']['relativePath'],
                                         movie["tmdbId"],
                                         movie_default_language, '[]',
                                         movie_default_hi, movie["id"],
                                         overview, poster, fanart,
                                         profile_id_to_language(
                                             movie['qualityProfileId']),
                                         sceneName,
                                         unicode(bool(movie['monitored'])),
                                         movie['sortTitle']))
                                else:
                                    movies_to_add.append(
                                        (movie["title"],
                                         movie["path"] + separator +
                                         movie['movieFile']['relativePath'],
                                         movie["tmdbId"], movie["tmdbId"],
                                         movie["tmdbId"], movie["id"],
                                         overview, poster, fanart,
                                         profile_id_to_language(
                                             movie['qualityProfileId']),
                                         sceneName,
                                         unicode(bool(movie['monitored'])),
                                         movie['sortTitle']))
                        else:
                            logging.error(
                                'BAZARR Radarr returned a movie without a file path: '
                                + movie["path"] + separator +
                                movie['movieFile']['relativePath'])

            # Update or insert movies in DB
            db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                                 timeout=30)
            c = db.cursor()

            updated_result = c.executemany(
                '''UPDATE table_movies SET title = ?, path = ?, tmdbId = ?, radarrId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ?, sceneName = ?, monitored = ?, sortTitle= ? WHERE tmdbid = ?''',
                movies_to_update)
            db.commit()

            if movie_default_enabled is True:
                added_result = c.executemany(
                    '''INSERT OR IGNORE INTO table_movies(title, path, tmdbId, languages, subtitles,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName, monitored, sortTitle) VALUES (?,?,?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
                    movies_to_add)
                db.commit()
            else:
                added_result = c.executemany(
                    '''INSERT OR IGNORE INTO table_movies(title, path, tmdbId, languages, subtitles,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName, monitored, sortTitle) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?), '[]',(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?, ?, ?, ?)''',
                    movies_to_add)
                db.commit()
            db.close()

            removed_movies = list(
                set(current_movies_db_list) - set(current_movies_radarr))

            db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                                 timeout=30)
            c = db.cursor()
            for removed_movie in removed_movies:
                c.execute('DELETE FROM table_movies WHERE tmdbId = ?',
                          (removed_movie, ))
            db.commit()
            db.close()

            for added_movie in movies_to_add:
                store_subtitles_movie(path_replace_movie(added_movie[1]))

            for updated_movie in movies_to_update:
                store_subtitles_movie(path_replace_movie(updated_movie[1]))

    logging.debug('BAZARR All movies synced from Radarr into database.')

    list_missing_subtitles_movies()
    logging.debug('BAZARR All movie missing subtitles updated in database.')
Example #6
0
def store_subtitles_movie(file):
    logging.debug('BAZARR started subtitles indexing for this file: ' + file)
    actual_subtitles = []
    if os.path.exists(file):
        if os.path.splitext(file)[1] == '.mkv':
            logging.debug("BAZARR is trying to index embedded subtitles.")
            try:
                with open(file, 'rb') as f:
                    mkv = enzyme.MKV(f)

                for subtitle_track in mkv.subtitle_tracks:
                    try:
                        if alpha2_from_alpha3(subtitle_track.language) != None:
                            lang = str(
                                alpha2_from_alpha3(subtitle_track.language))
                            logging.debug(
                                "BAZARR embedded subtitles detected: " + lang)
                            actual_subtitles.append([lang, None])
                    except:
                        logging.debug(
                            "BAZARR unable to index this unrecognized language: "
                            + subtitle_track.language)
                        pass
            except Exception as e:
                logging.exception(
                    "BAZARR error when trying to analyze this mkv file: " +
                    file)
                pass
        else:
            logging.debug("BAZARR This file isn't an .mkv file.")

        brazilian_portuguese = [".pt-br", ".pob", "pb"]
        try:
            subtitles = core.search_external_subtitles(file)
        except Exception as e:
            logging.exception("BAZARR unable to index external subtitles.")
            pass
        else:
            for subtitle, language in subtitles.iteritems():
                if str(os.path.splitext(subtitle)[0]).lower().endswith(
                        tuple(brazilian_portuguese)) is True:
                    logging.debug("BAZARR external subtitles detected: " +
                                  "pb")
                    actual_subtitles.append([
                        str("pb"),
                        path_replace_reverse_movie(
                            os.path.join(os.path.dirname(file), subtitle))
                    ])
                elif str(language) != 'und':
                    logging.debug("BAZARR external subtitles detected: " +
                                  str(language))
                    actual_subtitles.append([
                        str(language),
                        path_replace_reverse_movie(
                            os.path.join(os.path.dirname(file), subtitle))
                    ])
                else:
                    if os.path.splitext(subtitle)[1] != ".sub":
                        logging.debug(
                            "BAZARR falling back to file content analysis to detect language."
                        )
                        with open(
                                path_replace_movie(
                                    os.path.join(os.path.dirname(file),
                                                 subtitle)), 'r') as f:
                            text = list(islice(f, 100))
                            text = ' '.join(text)
                            encoding = UnicodeDammit(text)
                            try:
                                text = text.decode(encoding.original_encoding)
                                detected_language = langdetect.detect(text)
                            except Exception as e:
                                logging.exception(
                                    'BAZARR Error trying to detect language for this subtitles file: '
                                    + path_replace(
                                        os.path.join(os.path.dirname(file),
                                                     subtitle)) +
                                    ' You should try to delete this subtitles file manually and ask Bazarr to download it again.'
                                )
                            else:
                                if len(detected_language) > 0:
                                    logging.debug(
                                        "BAZARR external subtitles detected and analysis guessed this language: "
                                        + str(detected_language))
                                    actual_subtitles.append([
                                        str(detected_language),
                                        path_replace_reverse_movie(
                                            os.path.join(
                                                os.path.dirname(file),
                                                subtitle))
                                    ])

        conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                                  timeout=30)
        c_db = conn_db.cursor()
        logging.debug("BAZARR storing those languages to DB: " +
                      str(actual_subtitles))
        c_db.execute("UPDATE table_movies SET subtitles = ? WHERE path = ?",
                     (str(actual_subtitles), path_replace_reverse_movie(file)))
        conn_db.commit()

        c_db.close()
    else:
        logging.debug(
            "BAZARR this file doesn't seems to exist or isn't accessible.")

    logging.debug('BAZARR ended subtitles indexing for this file: ' + file)

    return actual_subtitles
Example #7
0
def store_subtitles_movie(file):
    # languages = []
    actual_subtitles = []
    if os.path.exists(file):
        if os.path.splitext(file)[1] == '.mkv':
            try:
                with open(file, 'rb') as f:
                    mkv = enzyme.MKV(f)

                for subtitle_track in mkv.subtitle_tracks:
                    try:
                        if alpha2_from_alpha3(subtitle_track.language) != None:
                            actual_subtitles.append([
                                str(alpha2_from_alpha3(
                                    subtitle_track.language)), None
                            ])
                    except:
                        pass
            except:
                pass

        subtitles = core.search_external_subtitles(file)
        brazilian_portuguese = [".pt-br", ".pob", "pb"]

        for subtitle, language in subtitles.iteritems():
            if str(os.path.splitext(subtitle)[0]).lower().endswith(
                    tuple(brazilian_portuguese)) is True:
                actual_subtitles.append([
                    str("pb"),
                    path_replace_reverse_movie(
                        os.path.join(os.path.dirname(file), subtitle))
                ])
            elif str(language) != 'und':
                actual_subtitles.append([
                    str(language),
                    path_replace_reverse_movie(
                        os.path.join(os.path.dirname(file), subtitle))
                ])
            else:
                if os.path.splitext(subtitle)[1] != ".sub":
                    with open(
                            path_replace_movie(
                                os.path.join(os.path.dirname(file), subtitle)),
                            'r') as f:
                        text = list(islice(f, 100))
                        text = ' '.join(text)
                        encoding = UnicodeDammit(text)
                        try:
                            text = text.decode(encoding.original_encoding)
                        except Exception as e:
                            logging.exception(
                                'Error trying to detect character encoding for this subtitles file: '
                                + path_replace_movie(
                                    os.path.join(os.path.dirname(file),
                                                 subtitle)) +
                                ' You should try to delete this subtitles file manually and ask Bazarr to download it again.'
                            )
                        else:
                            detected_language = langdetect.detect(text)
                            if len(detected_language) > 0:
                                actual_subtitles.append([
                                    str(detected_language),
                                    path_replace_reverse_movie(
                                        os.path.join(os.path.dirname(file),
                                                     subtitle))
                                ])

        conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'),
                                  timeout=30)
        c_db = conn_db.cursor()

        c_db.execute("UPDATE table_movies SET subtitles = ? WHERE path = ?",
                     (str(actual_subtitles), path_replace_reverse_movie(file)))
        conn_db.commit()

        c_db.close()

    return actual_subtitles