From c1649395207e463c2b80d0c6528e80e01c3543c5 Mon Sep 17 00:00:00 2001 From: morpheus65535 <5130500+morpheus65535@users.noreply.github.com> Date: Sun, 29 Jul 2018 20:42:24 -0400 Subject: [PATCH] Added support for Brazilian Portuguese requested in #109 --- bazarr.py | 9 +++-- get_languages.py | 91 ++++++++++++++++++++++++++++++++++++------ get_subtitle.py | 24 ++++++----- list_subtitles.py | 28 +++++++++---- views/episodes.tpl | 13 +++--- views/movie.tpl | 12 +++--- views/wantedmovies.tpl | 4 +- views/wantedseries.tpl | 4 +- 8 files changed, 135 insertions(+), 50 deletions(-) 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: -

{{language}}
+
{{language}}
%end

@@ -162,14 +162,14 @@ %if episode[4] is not None: % actual_languages = ast.literal_eval(episode[4]) + % actual_languages.sort() %else: % actual_languages = '[]' %end - %try: %for language in actual_languages: %if language[1] is not None: - + {{language[0]}} @@ -187,12 +187,13 @@ %try: %if episode[6] is not None: % missing_languages = ast.literal_eval(episode[6]) + % missing_languages.sort() %else: % missing_languages = None %end %if missing_languages is not None: - %for language in missing_languages: - + %for language in missing_languages: + {{language}} diff --git a/views/movie.tpl b/views/movie.tpl index 58a9a3bb4..6b93136f1 100644 --- a/views/movie.tpl +++ b/views/movie.tpl @@ -65,7 +65,7 @@ %import ast - %import pycountry + %from get_languages import * %from get_general_settings import * %single_language = get_general_settings()[7]
@@ -116,15 +116,16 @@ <% subtitles_files = ast.literal_eval(str(details[9])) + subtitles_files.sort() if subtitles_files is not None: for subtitles_file in subtitles_files: %> {{path_replace_movie(subtitles_file[1]) if subtitles_file[1] is not None else 'Video file subtitles track'}} -
{{pycountry.languages.lookup(str(subtitles_file[0])).name}}
+
{{language_from_alpha2(subtitles_file[0])}}
%if subtitles_file[1] is not None: - + %end @@ -143,7 +144,6 @@ <% missing_subs_languages = ast.literal_eval(str(details[11])) - missing_subs_languages_list = [] if missing_subs_languages is not None: %> @@ -156,8 +156,8 @@ <% for missing_subs_language in missing_subs_languages: %> - - {{pycountry.languages.lookup(str(missing_subs_language)).name}} + + {{language_from_alpha2(str(missing_subs_language))}} <% diff --git a/views/wantedmovies.tpl b/views/wantedmovies.tpl index 37d7e4fc4..2c610e5ac 100644 --- a/views/wantedmovies.tpl +++ b/views/wantedmovies.tpl @@ -41,7 +41,7 @@ %import ast - %import pycountry + %from get_languages import *
Loading...
@@ -72,7 +72,7 @@ %missing_languages = ast.literal_eval(row[1]) %if missing_languages is not None: %for language in missing_languages: - + {{language}} diff --git a/views/wantedseries.tpl b/views/wantedseries.tpl index 130f03efd..1b6617c71 100644 --- a/views/wantedseries.tpl +++ b/views/wantedseries.tpl @@ -41,7 +41,7 @@ %import ast - %import pycountry + %from get_languages import *
Loading...
@@ -79,7 +79,7 @@ %missing_languages = ast.literal_eval(row[3]) %if missing_languages is not None: %for language in missing_languages: - + {{language}}