get_subtitle: implement manual subtitle download

pull/292/head
panni 6 years ago
parent d9627ae99c
commit 1f485d0aa7

@ -17,8 +17,8 @@ from datetime import datetime, timedelta
from subzero.language import Language
from subzero.video import parse_video
from subliminal import region, Video, score as subliminal_scores, \
list_subtitles, download_subtitles
from subliminal_patch.core import SZAsyncProviderPool, download_best_subtitles, save_subtitles
list_subtitles
from subliminal_patch.core import SZAsyncProviderPool, download_best_subtitles, save_subtitles, download_subtitles
from subliminal_patch.score import compute_score
from subliminal.subtitle import get_subtitle_path
from get_languages import language_from_alpha3, alpha2_from_alpha3, alpha3_from_alpha2
@ -343,6 +343,7 @@ def manual_search(path, language, hi, providers, providers_auth, sceneName, titl
not_matched = scores - matches
score = compute_score(matches, s, video, hearing_impaired=hi)
s.score = score
if score < min_score:
continue
@ -359,101 +360,82 @@ def manual_search(path, language, hi, providers, providers_auth, sceneName, titl
return final_subtitles
def manual_download_subtitle(path, language, hi, subtitle, provider, providers_auth, sceneName, media_type):
def manual_download_subtitle(path, language, hi, subtitle, provider, providers_auth, sceneName, title, media_type):
logging.debug('BAZARR Manually downloading subtitles for this file: ' + path)
if hi == "True":
hi = True
else:
hi = False
subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64"))
if media_type == 'series':
type_of_score = 360
elif media_type == 'movie':
type_of_score = 120
use_scenename = get_general_settings()[9]
use_postprocessing = get_general_settings()[10]
postprocessing_cmd = get_general_settings()[11]
single = get_general_settings()[7]
language = alpha3_from_alpha2(language)
if language == 'pob':
lang_obj = Language('por', 'BR')
else:
lang_obj = Language(language)
try:
if sceneName is None or not use_scenename:
used_sceneName = False
video = parse_video(path, None, providers={provider})
else:
used_sceneName = True
video = Video.fromname(sceneName)
except Exception as e:
logging.exception("BAZARR Error trying to get video information for this file: " + path)
pass
else:
video = get_video(path, title, sceneName, use_scenename, providers={provider}, media_type=media_type)
if video:
min_score, max_score, scores = get_scores(video, media_type)
try:
download_subtitles([subtitle], providers=provider, provider_configs=providers_auth)
download_subtitles([subtitle], providers={provider}, provider_configs=providers_auth,
pool_class=SZAsyncProviderPool,
throttle_callback=None) # fixme
logging.debug('BAZARR Subtitles file downloaded for this file:' + path)
except Exception as e:
logging.exception('BAZARR Error downloading subtitles for this file ' + path)
return None
else:
single = get_general_settings()[7]
try:
score = round(float(compute_score(subtitle, video, hearing_impaired=hi)) / type_of_score * 100, 2)
if used_sceneName == True:
video = parse_video(path, None, providers={provider})
if single is True:
result = save_subtitles(video, [subtitle], single=True, encoding='utf-8')
else:
result = save_subtitles(video, [subtitle], encoding='utf-8')
score = round(subtitle.score / max_score * 100, 2)
saved_subtitles = save_subtitles(video.name, [subtitle], single=single)
except Exception as e:
logging.exception('BAZARR Error saving subtitles file to disk for this file:' + path)
return None
return
else:
if len(result) > 0:
downloaded_provider = result[0].provider_name
downloaded_language = language_from_alpha3(result[0].language.alpha3)
downloaded_language_code2 = alpha2_from_alpha3(result[0].language.alpha3)
downloaded_language_code3 = result[0].language.alpha3
downloaded_path = get_subtitle_path(path, downloaded_language_code2)
logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path)
message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(
score) + "% using manual search."
if use_postprocessing is 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()
if saved_subtitles:
for saved_subtitle in saved_subtitles:
downloaded_provider = saved_subtitle.provider_name
downloaded_language = language_from_alpha3(saved_subtitle.language.alpha3)
downloaded_language_code2 = alpha2_from_alpha3(saved_subtitle.language.alpha3)
downloaded_language_code3 = saved_subtitle.language.alpha3
downloaded_path = saved_subtitle.storage_path
logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path)
message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(
score) + "% using manual search."
if use_postprocessing is 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()
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)
if os.name == 'nt':
out = out.decode(encoding)
except:
if out == "":
logging.error(
'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution')
else:
logging.error('BAZARR Post-processing result for file ' + path + ' : ' + out)
else:
if out == "":
logging.info(
'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution')
except:
if out == "":
logging.error(
'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution')
else:
logging.error('BAZARR Post-processing result for file ' + path + ' : ' + out)
else:
logging.info('BAZARR Post-processing result for file ' + path + ' : ' + out)
if out == "":
logging.info(
'BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution')
else:
logging.info('BAZARR Post-processing result for file ' + path + ' : ' + out)
return message
return message
else:
logging.error(
'BAZARR Tried to manually download a subtitles for file: ' + path + " but we weren't able to do (probably throttled by ' + str(subtitle.provider_name) + '. Please retry later or select a subtitles from another provider.")

@ -1675,13 +1675,14 @@ def manual_get_subtitle():
subtitle = request.forms.get('subtitle')
sonarrSeriesId = request.forms.get('sonarrSeriesId')
sonarrEpisodeId = request.forms.get('sonarrEpisodeId')
title = request.forms.get('title')
providers_list = get_providers()
providers_auth = get_providers_auth()
try:
result = manual_download_subtitle(episodePath, language, hi, subtitle, selected_provider, providers_auth,
sceneName, 'series')
sceneName, title, 'series')
if result is not None:
history_log(1, sonarrSeriesId, sonarrEpisodeId, result)
send_notifications(sonarrSeriesId, sonarrEpisodeId, result)
@ -1753,13 +1754,14 @@ def manual_get_subtitle_movie():
selected_provider = request.forms.get('provider')
subtitle = request.forms.get('subtitle')
radarrId = request.forms.get('radarrId')
title = request.forms.get('title')
providers_list = get_providers()
providers_auth = get_providers_auth()
try:
result = manual_download_subtitle(moviePath, language, hi, subtitle, selected_provider, providers_auth,
sceneName, 'movie')
sceneName, title, 'movie')
if result is not None:
history_log_movie(1, radarrId, result)
send_notifications_movie(radarrId, result)

@ -558,7 +558,8 @@
language: $(button).attr("data-language"),
hi: hi,
sonarrSeriesId: sonarrSeriesId,
sonarrEpisodeId: sonarrEpisodeId
sonarrEpisodeId: sonarrEpisodeId,
title: '{{!details[0].replace("'", "\\'")}}'
};
$('#loader_text').text("Downloading subtitle to disk...");

@ -500,7 +500,8 @@
sceneName: sceneName,
language: $(button).attr("data-language"),
hi: hi,
radarrId: radarrId
radarrId: radarrId,
title: '{{!details[0].replace("'", "\\'")}}'
};
$('#loader_text').text("Downloading subtitle to disk...");

Loading…
Cancel
Save