diff --git a/bazarr.py b/bazarr.py index 4e25d056c..6f0059435 100644 --- a/bazarr.py +++ b/bazarr.py @@ -1,4 +1,4 @@ -bazarr_version = '0.5.6' +bazarr_version = '0.5.7' import gc gc.enable() @@ -69,7 +69,6 @@ from json import dumps import itertools import operator import requests -import pycountry import pretty from datetime import datetime, timedelta from PIL import Image @@ -100,6 +99,8 @@ c.execute("UPDATE table_settings_general SET configured = 0, updated = 0") conn.commit() c.close() +# Load languages in database +load_language_in_db() @route('/') def redirect_root(): @@ -1137,7 +1138,7 @@ def remove_subtitles(): try: os.remove(subtitlesPath) - result = pycountry.languages.lookup(language).name + " subtitles deleted from disk." + result = language_from_alpha3(language) + " subtitles deleted from disk." history_log(0, sonarrSeriesId, sonarrEpisodeId, result) except OSError: pass @@ -1154,7 +1155,7 @@ def remove_subtitles_movie(): try: os.remove(subtitlesPath) - result = pycountry.languages.lookup(language).name + " subtitles deleted from disk." + result = language_from_alpha3(language) + " subtitles deleted from disk." history_log_movie(0, radarrId, result) except OSError: pass diff --git a/get_languages.py b/get_languages.py index 6189cd67e..a6b2eb539 100644 --- a/get_languages.py +++ b/get_languages.py @@ -2,20 +2,85 @@ import sqlite3 import pycountry import os -# Get languages list in langs tuple -langs = [[lang.alpha_3,lang.alpha_2,lang.name] - for lang in pycountry.languages - if hasattr(lang, 'alpha_2')] +def load_language_in_db(): + # Get languages list in langs tuple + langs = [[lang.alpha_3,lang.alpha_2,lang.name] + for lang in pycountry.languages + if hasattr(lang, 'alpha_2')] -# Open database connection -db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) -c = db.cursor() + # Open database connection + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() -# Insert languages in database table -c.executemany('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', langs) + # Insert languages in database table + c.executemany('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', langs) + c.execute('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', ('pob','pb','Brazilian Portuguese')) -# Commit changes to database table -db.commit() + # Commit changes to database table + db.commit() -# Close database connection -db.close() + # Close database connection + db.close() + +def language_from_alpha2(lang): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT name FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def language_from_alpha3(lang): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT name FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha2_from_alpha3(lang): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code2 FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha2_from_language(lang): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code2 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha3_from_alpha2(lang): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code3 FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha3_from_language(lang): + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code3 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +if __name__ == '__main__': + load_language_in_db() \ No newline at end of file diff --git a/get_subtitle.py b/get_subtitle.py index 47f46d0df..6af3d98d1 100644 --- a/get_subtitle.py +++ b/get_subtitle.py @@ -5,7 +5,7 @@ import logging import subprocess from babelfish import * from subliminal import * -from pycountry import * +from get_languages import * from bs4 import UnicodeDammit from get_general_settings import * from list_subtitles import * @@ -24,6 +24,12 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, use_scenename = get_general_settings()[9] use_postprocessing = get_general_settings()[10] postprocessing_cmd = get_general_settings()[11] + + if language == 'pob': + lang_obj = Language('por', 'BR') + else: + lang_obj = Language(language) + try: if sceneName is None or use_scenename == "False": used_sceneName = False @@ -36,7 +42,7 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, return None else: try: - best_subtitles = download_best_subtitles([video], {Language(language)}, providers=providers, min_score=minimum_score, hearing_impaired=hi, provider_configs=providers_auth) + best_subtitles = download_best_subtitles([video], {lang_obj}, providers=providers, min_score=minimum_score, hearing_impaired=hi, provider_configs=providers_auth) except Exception as e: logging.exception('Error trying to get the best subtitles for this file: ' + path) return None @@ -61,9 +67,9 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, return None else: downloaded_provider = str(result[0][0]).strip('<>').split(' ')[0][:-8] - downloaded_language = pycountry.languages.lookup(str(str(result[0][0]).strip('<>').split(' ')[2].strip('[]'))).name - downloaded_language_code2 = pycountry.languages.lookup(downloaded_language).alpha_2 - downloaded_language_code3 = pycountry.languages.lookup(downloaded_language).alpha_3 + downloaded_language = language_from_alpha3(language) + downloaded_language_code2 = alpha2_from_alpha3(language) + downloaded_language_code3 = language downloaded_path = result[1] if used_sceneName == True: message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(score) + "% using this scene name obtained from Sonarr: " + sceneName @@ -126,7 +132,7 @@ def series_download_subtitles(no): for episode in episodes_details: for language in ast.literal_eval(episode[1]): - message = download_subtitle(path_replace(episode[0]), str(pycountry.languages.lookup(language).alpha_3), series_details[0], providers_list, providers_auth, episode[3], 'series') + message = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), series_details[0], providers_list, providers_auth, episode[3], 'series') if message is not None: store_subtitles(path_replace(episode[0])) history_log(1, no, episode[2], message) @@ -159,7 +165,7 @@ def movies_download_subtitles(no): providers_auth = None for language in ast.literal_eval(movie[1]): - message = download_subtitle(path_replace_movie(movie[0]), str(pycountry.languages.lookup(language).alpha_3), movie[4], providers_list, providers_auth, movie[3], 'movies') + message = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], providers_list, providers_auth, movie[3], 'movies') if message is not None: store_subtitles_movie(path_replace_movie(movie[0])) history_log_movie(1, no, message) @@ -193,7 +199,7 @@ def wanted_download_subtitles(path): for episode in episodes_details: for language in ast.literal_eval(episode[1]): - message = download_subtitle(path_replace(episode[0]), str(pycountry.languages.lookup(language).alpha_3), episode[4], providers_list, providers_auth, episode[5], 'series') + message = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), episode[4], providers_list, providers_auth, episode[5], 'series') if message is not None: store_subtitles(path_replace(episode[0])) list_missing_subtitles(episode[3]) @@ -227,7 +233,7 @@ def wanted_download_subtitles_movie(path): for movie in movies_details: for language in ast.literal_eval(movie[1]): - message = download_subtitle(path_replace_movie(movie[0]), str(pycountry.languages.lookup(language).alpha_3), movie[4], providers_list, providers_auth, movie[5], 'movies') + message = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], providers_list, providers_auth, movie[5], 'movies') if message is not None: store_subtitles_movie(path_replace_movie(movie[0])) list_missing_subtitles_movies(movie[3]) diff --git a/list_subtitles.py b/list_subtitles.py index 077766fe0..de2f372f6 100644 --- a/list_subtitles.py +++ b/list_subtitles.py @@ -3,7 +3,6 @@ import os import enzyme import babelfish from subliminal import * -import pycountry import sqlite3 import ast import langdetect @@ -11,6 +10,7 @@ from bs4 import UnicodeDammit from itertools import islice from get_general_settings import * +from get_languages import * gc.enable() @@ -25,20 +25,23 @@ def store_subtitles(file): for subtitle_track in mkv.subtitle_tracks: try: - actual_subtitles.append([str(pycountry.languages.lookup(subtitle_track.language).alpha_2),None]) + actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)),None]) except: pass except: pass + brazilian_portuguese = [".pt-br", ".pob", "pb"] try: subtitles = core.search_external_subtitles(file) except: pass else: for subtitle, language in subtitles.iteritems(): - if str(language) != 'und': + if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: + actual_subtitles.append([str("pb"), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) + elif str(language) != 'und': actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) else: with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: @@ -53,7 +56,7 @@ def store_subtitles(file): detected_language = langdetect.detect(text) if len(detected_language) > 0: actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - + conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) c_db = conn_db.cursor() @@ -76,16 +79,19 @@ def store_subtitles_movie(file): for subtitle_track in mkv.subtitle_tracks: try: - actual_subtitles.append([str(pycountry.languages.lookup(subtitle_track.language).alpha_2), 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(language) != 'und': + 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": @@ -139,7 +145,10 @@ def list_missing_subtitles(*no): missing_subtitles_global.append(tuple(['[]', episode_subtitles[0]])) else: for item in actual_subtitles: - actual_subtitles_list.append(item[0]) + if item[0] == "pt-BR": + actual_subtitles_list.append("pb") + else: + actual_subtitles_list.append(item[0]) missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]])) @@ -176,7 +185,10 @@ def list_missing_subtitles_movies(*no): missing_subtitles_global.append(tuple(['[]', movie_subtitles[0]])) else: for item in actual_subtitles: - actual_subtitles_list.append(item[0]) + if item[0] == "pt-BR": + actual_subtitles_list.append("pb") + else: + actual_subtitles_list.append(item[0]) missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) missing_subtitles_global.append(tuple([str(missing_subtitles), movie_subtitles[0]])) diff --git a/views/episodes.tpl b/views/episodes.tpl index 2733caf2d..007c753d6 100644 --- a/views/episodes.tpl +++ b/views/episodes.tpl @@ -74,7 +74,7 @@
%import ast - %import pycountry + %from get_languages import * %from get_general_settings import * %single_language = get_general_settings()[7] @@ -109,7 +109,7 @@%for language in subs_languages_list: -