diff --git a/bazarr.py b/bazarr.py index c3a3b347d..bb02e32a3 100644 --- a/bazarr.py +++ b/bazarr.py @@ -400,10 +400,16 @@ def save_settings(): else: settings_general_scenename = 'True' settings_general_minimum_score = request.forms.get('settings_general_minimum_score') + settings_general_use_postprocessing = request.forms.get('settings_general_use_postprocessing') + if settings_general_use_postprocessing is None: + settings_general_use_postprocessing = 'False' + else: + settings_general_use_postprocessing = 'True' + settings_general_postprocessing_cmd = request.forms.get('settings_general_postprocessing_cmd') before = c.execute("SELECT ip, port, base_url FROM table_settings_general").fetchone() after = (unicode(settings_general_ip), int(settings_general_port), unicode(settings_general_baseurl)) - c.execute("UPDATE table_settings_general SET ip = ?, port = ?, base_url = ?, path_mapping = ?, log_level = ?, branch=?, auto_update=?, single_language=?, minimum_score=?, use_scenename=?", (unicode(settings_general_ip), int(settings_general_port), unicode(settings_general_baseurl), unicode(settings_general_pathmapping), unicode(settings_general_loglevel), unicode(settings_general_branch), unicode(settings_general_automatic), unicode(settings_general_single_language), unicode(settings_general_minimum_score), unicode(settings_general_scenename) )) + c.execute("UPDATE table_settings_general SET ip = ?, port = ?, base_url = ?, path_mapping = ?, log_level = ?, branch=?, auto_update=?, single_language=?, minimum_score=?, use_scenename=?, use_postprocessing=?, postprocessing_cmd=?", (unicode(settings_general_ip), int(settings_general_port), unicode(settings_general_baseurl), unicode(settings_general_pathmapping), unicode(settings_general_loglevel), unicode(settings_general_branch), unicode(settings_general_automatic), unicode(settings_general_single_language), unicode(settings_general_minimum_score), unicode(settings_general_scenename), unicode(settings_general_use_postprocessing), unicode(settings_general_postprocessing_cmd) )) conn.commit() if after != before: configured() diff --git a/get_general_settings.py b/get_general_settings.py index 3e3e5e456..37fab629f 100644 --- a/get_general_settings.py +++ b/get_general_settings.py @@ -29,8 +29,10 @@ def get_general_settings(): single_language = general_settings[9] minimum_score = general_settings[10] use_scenename = general_settings[11] + use_postprocessing = general_settings[12] + postprocessing_cmd = general_settings[13] - return [ip, port, base_url, path_mappings, log_level, branch, automatic, single_language, minimum_score, use_scenename] + return [ip, port, base_url, path_mappings, log_level, branch, automatic, single_language, minimum_score, use_scenename, use_postprocessing, postprocessing_cmd] def path_replace(path): for path_mapping in path_mappings: @@ -54,6 +56,14 @@ def path_replace_reverse(path): break return path +def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3): + pp_command = pp_command.replace('{{episode}}', episode) + pp_command = pp_command.replace('{{subtitles}}', subtitles) + pp_command = pp_command.replace('{{language}}', language) + pp_command = pp_command.replace('{{language_code2}}', language_code2) + pp_command = pp_command.replace('{{language_code3}}', language_code3) + return pp_command + result = get_general_settings() ip = result[0] port = result[1] @@ -65,3 +75,5 @@ automatic = result[6] single_language = result[7] minimum_score = result[8] use_scenename = result[9] +use_processing = result[10] +postprocessing_cmd = result[11] \ No newline at end of file diff --git a/get_subtitle.py b/get_subtitle.py index b34a0f06a..66539b1ee 100644 --- a/get_subtitle.py +++ b/get_subtitle.py @@ -2,9 +2,11 @@ import os import sqlite3 import ast import logging +import subprocess from babelfish import * from subliminal import * from pycountry import * +from bs4 import UnicodeDammit from get_general_settings import * from list_subtitles import * from utils import * @@ -16,6 +18,8 @@ region.configure('dogpile.cache.memory') def download_subtitle(path, language, hi, providers, providers_auth, sceneName): minimum_score = float(get_general_settings()[8]) / 100 * 360 use_scenename = get_general_settings()[9] + use_postprocessing = get_general_settings()[10] + postprocessing_cmd = get_general_settings()[11] try: if sceneName is None or use_scenename == "False": used_sceneName = False @@ -52,13 +56,43 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName): logging.error('Error saving subtitles file to disk.') return None else: - downloaded_provider = str(result[0]).strip('<>').split(' ')[0][:-8] - downloaded_language = pycountry.languages.lookup(str(str(result[0]).strip('<>').split(' ')[2].strip('[]'))).name + 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_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 else: message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(score) + "% using filename guessing." + if use_postprocessing == "True": + command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3) + try: + if os.name == 'nt': + codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # wait for the process to terminate + out_codepage, err_codepage = codepage.communicate() + encoding = out_codepage.split(':')[-1].strip() + + process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # wait for the process to terminate + out, err = process.communicate() + + if os.name == 'nt': + out = out.decode(encoding) + + except: + if out == "": + logging.error('Post-processing result for file ' + path + ' : Nothing returned from command execution') + else: + logging.error('Post-processing result for file ' + path + ' : ' + out) + else: + if out == "": + logging.info('Post-processing result for file ' + path + ' : Nothing returned from command execution') + else: + logging.info('Post-processing result for file ' + path + ' : ' + out) + return message def series_download_subtitles(no): diff --git a/update_db.py b/update_db.py index f1c0cd883..aa2e5d7d4 100644 --- a/update_db.py +++ b/update_db.py @@ -72,6 +72,18 @@ if os.path.exists(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) else: c.execute('UPDATE table_settings_general SET use_scenename="True"') + try: + c.execute('alter table table_settings_general add column "use_postprocessing" "text"') + except: + pass + else: + c.execute('UPDATE table_settings_general SET use_postprocessing="False"') + + try: + c.execute('alter table table_settings_general add column "postprocessing_cmd" "text"') + except: + pass + # Commit change to db db.commit() diff --git a/views/settings.tpl b/views/settings.tpl index 24faf786b..0e553bb36 100644 --- a/views/settings.tpl +++ b/views/settings.tpl @@ -231,6 +231,48 @@ +