Added support for Brazilian Portuguese requested in #109

pull/119/merge
morpheus65535 7 years ago
parent 2db22f0271
commit c164939520

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

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

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

@ -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:
@ -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]]))

@ -74,7 +74,7 @@
</head>
<body>
%import ast
%import pycountry
%from get_languages import *
%from get_general_settings import *
%single_language = get_general_settings()[7]
<div style="display: none;"><img src="{{base_url}}image_proxy{{details[3]}}"></div>
@ -109,7 +109,7 @@
</p>
<p style='margin-top: 2em;'>
%for language in subs_languages_list:
<div class="ui tiny inverted label" style='background-color: #35c5f4;'>{{language}}</div>
<div class="ui tiny inverted label" style='background-color: #35c5f4;'>{{language}}</div>
%end
</p>
<div style='clear:both;'></div>
@ -162,14 +162,14 @@
<td>
%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:
<a data-episodePath="{{episode[1]}}" data-subtitlesPath="{{path_replace(language[1])}}" data-language="{{pycountry.languages.lookup(str(language[0])).alpha_3}}" data-sonarrSeriesId={{episode[5]}} data-sonarrEpisodeId={{episode[7]}} class="remove_subtitles ui tiny label">
<a data-episodePath="{{episode[1]}}" data-subtitlesPath="{{path_replace(language[1])}}" data-language="{{alpha3_from_alpha2(str(language[0]))}}" data-sonarrSeriesId={{episode[5]}} data-sonarrEpisodeId={{episode[7]}} class="remove_subtitles ui tiny label">
{{language[0]}}
<i class="delete icon"></i>
</a>
@ -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:
<a data-episodePath="{{episode[1]}}" data-scenename="{{episode[8]}}" data-language="{{pycountry.languages.lookup(str(language)).alpha_3}}" data-hi="{{details[4]}}" data-sonarrSeriesId={{episode[5]}} data-sonarrEpisodeId={{episode[7]}} class="get_subtitle ui tiny label">
%for language in missing_languages:
<a data-episodePath="{{episode[1]}}" data-scenename="{{episode[8]}}" data-language="{{alpha3_from_alpha2(str(language))}}" data-hi="{{details[4]}}" data-sonarrSeriesId={{episode[5]}} data-sonarrEpisodeId={{episode[7]}} class="get_subtitle ui tiny label">
{{language}}
<i style="margin-left:3px; margin-right:0px" class="search icon"></i>
</a>

@ -65,7 +65,7 @@
</head>
<body>
%import ast
%import pycountry
%from get_languages import *
%from get_general_settings import *
%single_language = get_general_settings()[7]
<div style="display: none;"><img src="{{base_url}}image_proxy_movies{{details[3]}}"></div>
@ -116,15 +116,16 @@
<tbody>
<%
subtitles_files = ast.literal_eval(str(details[9]))
subtitles_files.sort()
if subtitles_files is not None:
for subtitles_file in subtitles_files:
%>
<tr>
<td>{{path_replace_movie(subtitles_file[1]) if subtitles_file[1] is not None else 'Video file subtitles track'}}</td>
<td><div class="ui tiny inverted label" style='background-color: #777777;'>{{pycountry.languages.lookup(str(subtitles_file[0])).name}}</div></td>
<td><div class="ui tiny inverted label" style='background-color: #777777;'>{{language_from_alpha2(subtitles_file[0])}}</div></td>
<td>
%if subtitles_file[1] is not None:
<a class="remove_subtitles ui inverted basic compact icon" data-tooltip="Delete subtitles file from disk" data-inverted="" data-moviePath="{{details[8]}}" data-subtitlesPath="{{path_replace_movie(subtitles_file[1])}}" data-language="{{pycountry.languages.lookup(str(subtitles_file[0])).alpha_3}}" data-radarrId={{details[10]}}>
<a class="remove_subtitles ui inverted basic compact icon" data-tooltip="Delete subtitles file from disk" data-inverted="" data-moviePath="{{details[8]}}" data-subtitlesPath="{{path_replace_movie(subtitles_file[1])}}" data-language="{{alpha3_from_alpha2(subtitles_file[0])}}" data-radarrId={{details[10]}}>
<i class="ui black delete icon"></i>
</a>
%end
@ -143,7 +144,6 @@
</table>
<%
missing_subs_languages = ast.literal_eval(str(details[11]))
missing_subs_languages_list = []
if missing_subs_languages is not None:
%>
<table class="ui very basic single line selectable table">
@ -156,8 +156,8 @@
<%
for missing_subs_language in missing_subs_languages:
%>
<a class="get_subtitle ui small blue label" data-moviePath="{{details[8]}}" data-scenename="{{details[12]}}" data-language="{{pycountry.languages.lookup(str(missing_subs_language)).alpha_3}}" data-hi="{{details[4]}}" data-radarrId={{details[10]}}>
{{pycountry.languages.lookup(str(missing_subs_language)).name}}
<a class="get_subtitle ui small blue label" data-moviePath="{{details[8]}}" data-scenename="{{details[12]}}" data-language="{{alpha3_from_alpha2(str(missing_subs_language))}}" data-hi="{{details[4]}}" data-radarrId={{details[10]}}>
{{language_from_alpha2(str(missing_subs_language))}}
<i style="margin-left:3px; margin-right:0px" class="search icon"></i>
</a>
<%

@ -41,7 +41,7 @@
</head>
<body>
%import ast
%import pycountry
%from get_languages import *
<div id='loader' class="ui page dimmer">
<div class="ui indeterminate text loader">Loading...</div>
</div>
@ -72,7 +72,7 @@
%missing_languages = ast.literal_eval(row[1])
%if missing_languages is not None:
%for language in missing_languages:
<a data-moviePath="{{row[3]}}" data-sceneName="{{row[5]}}" data-language="{{pycountry.languages.lookup(str(language)).alpha_3}}" data-hi="{{row[4]}}" data-radarrId={{row[2]}} class="get_subtitle ui tiny label">
<a data-moviePath="{{row[3]}}" data-sceneName="{{row[5]}}" data-language="{{alpha3_from_alpha2(str(language))}}" data-hi="{{row[4]}}" data-radarrId={{row[2]}} class="get_subtitle ui tiny label">
{{language}}
<i style="margin-left:3px; margin-right:0px" class="search icon"></i>
</a>

@ -41,7 +41,7 @@
</head>
<body>
%import ast
%import pycountry
%from get_languages import *
<div id='loader' class="ui page dimmer">
<div class="ui indeterminate text loader">Loading...</div>
</div>
@ -79,7 +79,7 @@
%missing_languages = ast.literal_eval(row[3])
%if missing_languages is not None:
%for language in missing_languages:
<a data-episodePath="{{row[5]}}" data-sceneName="{{row[8]}}" data-language="{{pycountry.languages.lookup(str(language)).alpha_3}}" data-hi="{{row[6]}}" data-sonarrSeriesId={{row[4]}} data-sonarrEpisodeId={{row[7]}} class="get_subtitle ui tiny label">
<a data-episodePath="{{row[5]}}" data-sceneName="{{row[8]}}" data-language="{{alpha3_from_alpha2(str(language))}}" data-hi="{{row[6]}}" data-sonarrSeriesId={{row[4]}} data-sonarrEpisodeId={{row[7]}} class="get_subtitle ui tiny label">
{{language}}
<i style="margin-left:3px; margin-right:0px" class="search icon"></i>
</a>

Loading…
Cancel
Save