pull/191/head
morpheus65535 7 years ago
parent c7866b3f11
commit 9b8c529f51

@ -4,6 +4,7 @@ import os
import sqlite3 import sqlite3
import ast import ast
import logging import logging
import operator
import subprocess import subprocess
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -28,21 +29,18 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName,
hi = True hi = True
else: else:
hi = False hi = False
if media_type == 'series': language_set = set()
type_of_score = 360 if language == 'pb':
minimum_score = float(get_general_settings()[8]) / 100 * type_of_score language_set.add(Language('por', 'BR'))
elif media_type == 'movie': else:
type_of_score = 120 language_set.add(Language(language))
minimum_score = float(get_general_settings()[22]) / 100 * type_of_score
use_scenename = get_general_settings()[9] use_scenename = get_general_settings()[9]
minimum_score = get_general_settings()[8]
minimum_score_movie = get_general_settings()[22]
use_postprocessing = get_general_settings()[10] use_postprocessing = get_general_settings()[10]
postprocessing_cmd = get_general_settings()[11] postprocessing_cmd = get_general_settings()[11]
if language == 'pob':
lang_obj = Language('por', 'BR')
else:
lang_obj = Language(language)
try: try:
if sceneName is None or use_scenename is False: if sceneName is None or use_scenename is False:
used_sceneName = False used_sceneName = False
@ -50,73 +48,102 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName,
else: else:
used_sceneName = True used_sceneName = True
video = Video.fromname(sceneName) video = Video.fromname(sceneName)
except:
logging.error("Error trying to get video information.")
else:
if media_type == "movie":
max_score = 120.0
elif media_type == "series":
max_score = 360.0
try:
with AsyncProviderPool(max_workers=None, providers=providers, provider_configs=providers_auth) as p:
subtitles = p.list_subtitles(video, language_set)
except Exception as e: except Exception as e:
logging.exception('Error trying to extract information from this filename: ' + path) logging.exception("Error trying to get subtitle list from provider")
return None
else: else:
try: subtitles_list = []
best_subtitles = download_best_subtitles([video], {lang_obj}, providers=providers, min_score=minimum_score, hearing_impaired=hi, provider_configs=providers_auth) #for s in subtitles:
except Exception as e: # {s: compute_score(s, video, hearing_impaired=hi)}
logging.exception('Error trying to get the best subtitles for this file: ' + path) sorted_subtitles = sorted([(s, compute_score(s, video, hearing_impaired=hi)) for s in subtitles], key=operator.itemgetter(1), reverse=True)
return None for s, preliminary_score in sorted_subtitles:
else: if media_type == "movie":
if (preliminary_score / max_score * 100) < int(minimum_score_movie):
continue
matched = set(s.get_matches(video))
if hi == s.hearing_impaired:
matched.add('hearing_impaired')
not_matched = set(score.movie_scores.keys()) - matched
required = set(['title'])
if any(elem in required for elem in not_matched):
continue
elif media_type == "series":
if (preliminary_score / max_score * 100) < int(minimum_score):
continue
matched = set(s.get_matches(video))
if hi == s.hearing_impaired:
matched.add('hearing_impaired')
not_matched = set(score.episode_scores.keys()) - matched
required = set(['series', 'season', 'episode'])
if any(elem in required for elem in not_matched):
continue
subtitles_list.append(s)
if len(subtitles_list) > 0:
best_subtitle = subtitles_list[0]
download_subtitles([best_subtitle], providers=providers, provider_configs=providers_auth)
try: try:
best_subtitle = best_subtitles[video][0] calculated_score = round(float(compute_score(best_subtitle, video, hearing_impaired=hi)) / max_score * 100, 2)
except: if used_sceneName == True:
logging.debug('No subtitles found for ' + path) video = scan_video(path)
single = get_general_settings()[7]
if single is True:
result = save_subtitles(video, [best_subtitle], single=True, encoding='utf-8')
else:
result = save_subtitles(video, [best_subtitle], encoding='utf-8')
except Exception as e:
logging.exception('Error saving subtitles file to disk.')
return None return None
else: else:
single = get_general_settings()[7] downloaded_provider = result[0].provider_name
try: downloaded_language = language_from_alpha3(result[0].language.alpha3)
score = round(float(compute_score(best_subtitle, video, hearing_impaired=hi)) / type_of_score * 100, 2) downloaded_language_code2 = alpha2_from_alpha3(result[0].language.alpha3)
if used_sceneName == True: downloaded_language_code3 = result[0].language.alpha3
video = scan_video(path) downloaded_path = get_subtitle_path(path, language=language_set)
if single is True: if used_sceneName == True:
result = save_subtitles(video, [best_subtitle], single=True, encoding='utf-8') message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(calculated_score) + "% using this scene name: " + sceneName
else:
result = save_subtitles(video, [best_subtitle], encoding='utf-8')
except:
logging.error('Error saving subtitles file to disk.')
return None
else: else:
downloaded_provider = result[0].provider_name message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(calculated_score) + "% using filename guessing."
downloaded_language = language_from_alpha3(result[0].language.alpha3)
downloaded_language_code2 = alpha2_from_alpha3(result[0].language.alpha3) if use_postprocessing is True:
downloaded_language_code3 = result[0].language.alpha3 command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3)
downloaded_path = get_subtitle_path(path, language=lang_obj) try:
if used_sceneName == True: if os.name == 'nt':
message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(score) + "% using this scene name: " + sceneName codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(score) + "% using filename guessing."
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 # wait for the process to terminate
out, err = process.communicate() out_codepage, err_codepage = codepage.communicate()
encoding = out_codepage.split(':')[-1].strip()
if os.name == 'nt': process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = out.decode(encoding) # wait for the process to terminate
out, err = process.communicate()
if os.name == 'nt':
out = out.decode(encoding)
except: except:
if out == "": if out == "":
logging.error('Post-processing result for file ' + path + ' : Nothing returned from command execution') logging.error('Post-processing result for file ' + path + ' : Nothing returned from command execution')
else:
logging.error('Post-processing result for file ' + path + ' : ' + out)
else: else:
if out == "": logging.error('Post-processing result for file ' + path + ' : ' + out)
logging.info('Post-processing result for file ' + path + ' : Nothing returned from command execution') else:
else: if out == "":
logging.info('Post-processing result for file ' + path + ' : ' + 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 return message
else:
return None
def manual_search(path, language, hi, providers, providers_auth, sceneName, media_type): def manual_search(path, language, hi, providers, providers_auth, sceneName, media_type):
if hi == "True": if hi == "True":
@ -129,11 +156,18 @@ def manual_search(path, language, hi, providers, providers_auth, sceneName, medi
language_set.add(Language('por', 'BR')) language_set.add(Language('por', 'BR'))
else: else:
language_set.add(Language(alpha3_from_alpha2(lang))) language_set.add(Language(alpha3_from_alpha2(lang)))
use_scenename = get_general_settings()[9]
use_postprocessing = get_general_settings()[10]
postprocessing_cmd = get_general_settings()[11]
try: try:
if sceneName != "None": if sceneName is None or use_scenename is False:
video = Video.fromname(sceneName) used_sceneName = False
else:
video = scan_video(path) video = scan_video(path)
else:
used_sceneName = True
video = Video.fromname(sceneName)
except: except:
logging.error("Error trying to get video information.") logging.error("Error trying to get video information.")
else: else:
@ -156,15 +190,21 @@ def manual_search(path, language, hi, providers, providers_auth, sceneName, medi
if hi == s.hearing_impaired: if hi == s.hearing_impaired:
matched.add('hearing_impaired') matched.add('hearing_impaired')
not_matched = set(score.movie_scores.keys()) - matched not_matched = set(score.movie_scores.keys()) - matched
if "title" in not_matched: required = set(['title'])
if any(elem in required for elem in not_matched):
continue continue
if used_sceneName:
not_matched.remove('hash')
elif media_type == "series": elif media_type == "series":
matched = set(s.get_matches(video)) matched = set(s.get_matches(video))
if hi == s.hearing_impaired: if hi == s.hearing_impaired:
matched.add('hearing_impaired') matched.add('hearing_impaired')
not_matched = set(score.episode_scores.keys()) - matched not_matched = set(score.episode_scores.keys()) - matched
if "series" in not_matched or "season" in not_matched or "episode" in not_matched: required = set(['series', 'season', 'episode'])
if any(elem in required for elem in not_matched):
continue continue
if used_sceneName:
not_matched.remove('hash')
subtitles_list.append(dict(score=round((compute_score(s, video, hearing_impaired=hi) / max_score * 100), 2), language=alpha2_from_alpha3(s.language.alpha3), hearing_impaired=str(s.hearing_impaired), provider=s.provider_name, subtitle=codecs.encode(pickle.dumps(s), "base64").decode(), url=s.page_link, matches=list(matched), dont_matches=list(not_matched))) subtitles_list.append(dict(score=round((compute_score(s, video, hearing_impaired=hi) / max_score * 100), 2), language=alpha2_from_alpha3(s.language.alpha3), hearing_impaired=str(s.hearing_impaired), provider=s.provider_name, subtitle=codecs.encode(pickle.dumps(s), "base64").decode(), url=s.page_link, matches=list(matched), dont_matches=list(not_matched)))
subtitles_dict = {} subtitles_dict = {}
subtitles_dict = sorted(subtitles_list, key=lambda x: x['score'], reverse=True) subtitles_dict = sorted(subtitles_list, key=lambda x: x['score'], reverse=True)

Loading…
Cancel
Save