bazarr/bazarr/get_subtitle.py

1155 lines
59 KiB
Python
Raw Normal View History

# coding=utf-8
2017-09-17 00:11:47 +00:00
import os
import sys
2017-10-16 23:27:19 +00:00
import ast
2017-12-06 04:07:37 +00:00
import logging
2018-03-24 00:00:50 +00:00
import subprocess
import time
import cPickle as pickle
import codecs
import types
import re
2018-10-31 16:33:19 +00:00
import subliminal
2019-08-13 17:46:29 +00:00
import platform
2019-08-16 01:07:40 +00:00
import operator
from datetime import datetime, timedelta
from subzero.language import Language
from subzero.video import parse_video
from subliminal import region, score as subliminal_scores, \
list_subtitles, Episode, Movie
from subliminal_patch.core import SZAsyncProviderPool, download_best_subtitles, save_subtitles, download_subtitles, \
list_all_subtitles, get_subtitle_path
from subliminal_patch.score import compute_score
2019-03-20 21:20:10 +00:00
from subliminal.refiners.tvdb import series_re
2019-01-01 03:37:25 +00:00
from get_languages import language_from_alpha3, alpha2_from_alpha3, alpha3_from_alpha2, language_from_alpha2
2018-12-15 00:36:28 +00:00
from config import settings
from helper import path_replace, path_replace_movie, path_replace_reverse, \
path_replace_reverse_movie, pp_replace, get_target_folder, force_unicode
2018-08-15 20:51:46 +00:00
from list_subtitles import store_subtitles, list_missing_subtitles, store_subtitles_movie, list_missing_subtitles_movies
from utils import history_log, history_log_movie, get_binary
2018-04-24 14:48:52 +00:00
from notifier import send_notifications, send_notifications_movie
from get_providers import get_providers, get_providers_auth, provider_throttle, provider_pool
from get_args import args
from queueconfig import notifications
from pyprobe.pyprobe import VideoFileParser
2019-10-26 18:52:22 +00:00
from database import database, dict_mapper
2017-09-17 00:11:47 +00:00
from analytics import track_event
def get_video(path, title, sceneName, use_scenename, providers=None, media_type="movie"):
"""
Construct `Video` instance
:param path: path to video
:param title: series/movie title
:param sceneName: sceneName
:param use_scenename: use sceneName
:param providers: provider list for selective hashing
:param media_type: movie/series
:return: `Video` instance
"""
hints = {"title": title, "type": "movie" if media_type == "movie" else "episode"}
used_scene_name = False
original_path = path
original_name = os.path.basename(path)
hash_from = None
if sceneName != "None" and use_scenename:
# use the sceneName but keep the folder structure for better guessing
path = os.path.join(os.path.dirname(path), sceneName + os.path.splitext(path)[1])
used_scene_name = True
hash_from = original_path
2019-07-28 19:49:19 +00:00
try:
video = parse_video(path, hints=hints, providers=providers, dry_run=used_scene_name,
hash_from=hash_from)
video.used_scene_name = used_scene_name
video.original_name = original_name
video.original_path = original_path
2019-07-28 19:49:19 +00:00
refine_from_db(original_path, video)
refine_from_ffprobe(original_path, video)
2019-07-28 19:49:19 +00:00
2019-09-17 13:51:40 +00:00
logging.debug('BAZARR is using these video object properties: %s', vars(video))
2019-07-28 19:49:19 +00:00
return video
except Exception as e:
2019-07-28 19:49:19 +00:00
logging.exception("BAZARR Error trying to get video information for this file: " + path)
def get_scores(video, media_type, min_score_movie_perc=60 * 100 / 120.0, min_score_series_perc=240 * 100 / 360.0,
min_score_special_ep=180 * 100 / 360.0):
"""
Get score range for a video.
:param video: `Video` instance
:param media_type: movie/series
:param min_score_movie_perc: Percentage of max score for min score of movies
:param min_score_series_perc: Percentage of max score for min score of series
:param min_score_special_ep: Percentage of max score for min score of series special episode
:return: tuple(min_score, max_score, set(scores))
"""
max_score = 120.0
min_score = max_score * min_score_movie_perc / 100.0
scores = subliminal_scores.movie_scores.keys()
if media_type == "series":
max_score = 360.0
min_score = max_score * min_score_series_perc / 100.0
scores = subliminal_scores.episode_scores.keys()
if video.is_special:
min_score = max_score * min_score_special_ep / 100.0
2019-01-15 16:25:13 +00:00
return min_score, max_score, set(scores)
2019-06-11 18:45:48 +00:00
def download_subtitle(path, language, hi, forced, providers, providers_auth, sceneName, title, media_type,
forced_minimum_score=None, is_upgrade=False):
# fixme: supply all missing languages, not only one, to hit providers only once who support multiple languages in
# one query
2019-06-11 18:45:48 +00:00
if settings.general.getboolean('utf8_encode'):
os.environ["SZ_KEEP_ENCODING"] = ""
else:
2019-08-16 01:29:21 +00:00
os.environ["SZ_KEEP_ENCODING"] = "True"
2019-06-11 18:45:48 +00:00
logging.debug('BAZARR Searching subtitles for this file: ' + path)
if hi == "True":
2019-05-03 17:37:29 +00:00
hi = "force HI"
else:
2019-05-03 17:37:29 +00:00
hi = "force non-HI"
2018-10-09 20:33:18 +00:00
language_set = set()
2019-06-11 18:45:48 +00:00
if not isinstance(language, types.ListType):
language = [language]
2019-06-11 18:45:48 +00:00
if forced == "True":
2019-07-28 19:49:19 +00:00
providers_auth['podnapisi']['only_foreign'] = True ## fixme: This is also in get_providers_auth()
providers_auth['subscene']['only_foreign'] = True ## fixme: This is also in get_providers_auth()
providers_auth['opensubtitles']['only_foreign'] = True ## fixme: This is also in get_providers_auth()
else:
providers_auth['podnapisi']['only_foreign'] = False
providers_auth['subscene']['only_foreign'] = False
providers_auth['opensubtitles']['only_foreign'] = False
2019-06-11 18:45:48 +00:00
for l in language:
if l == 'pob':
lang_obj = Language('por', 'BR')
2019-08-16 01:55:21 +00:00
if forced == "True":
lang_obj = Language.rebuild(lang_obj, forced=True)
else:
lang_obj = Language(l)
2019-08-16 01:55:21 +00:00
if forced == "True":
lang_obj = Language.rebuild(lang_obj, forced=True)
language_set.add(lang_obj)
2019-01-15 16:25:13 +00:00
use_scenename = settings.general.getboolean('use_scenename')
2018-12-15 00:36:28 +00:00
minimum_score = settings.general.minimum_score
minimum_score_movie = settings.general.minimum_score_movie
use_postprocessing = settings.general.getboolean('use_postprocessing')
2018-12-15 00:36:28 +00:00
postprocessing_cmd = settings.general.postprocessing_cmd
single = settings.general.getboolean('single_language')
2019-01-15 16:25:13 +00:00
# todo:
"""
AsyncProviderPool:
implement:
blacklist=None,
pre_download_hook=None,
post_download_hook=None,
language_hook=None
"""
video = get_video(force_unicode(path), title, sceneName, use_scenename, providers=providers,
2019-07-28 19:49:19 +00:00
media_type=media_type)
if video:
min_score, max_score, scores = get_scores(video, media_type, min_score_movie_perc=int(minimum_score_movie),
min_score_series_perc=int(minimum_score))
2019-06-11 18:45:48 +00:00
if providers:
2019-03-16 19:30:06 +00:00
if forced_minimum_score:
min_score = int(forced_minimum_score) + 1
downloaded_subtitles = download_best_subtitles({video}, language_set, int(min_score), hi,
providers=providers,
provider_configs=providers_auth,
pool_class=provider_pool(),
compute_score=compute_score,
throttle_time=None, # fixme
blacklist=None, # fixme
throttle_callback=provider_throttle,
pre_download_hook=None, # fixme
post_download_hook=None, # fixme
language_hook=None) # fixme
else:
downloaded_subtitles = None
logging.info("BAZARR All providers are throttled")
2019-07-28 19:49:19 +00:00
return None
2019-01-15 16:25:13 +00:00
saved_any = False
if downloaded_subtitles:
for video, subtitles in downloaded_subtitles.iteritems():
if not subtitles:
continue
2019-01-15 16:25:13 +00:00
try:
fld = get_target_folder(path)
2019-06-11 18:45:48 +00:00
chmod = int(settings.general.chmod, 8) if not sys.platform.startswith(
'win') and settings.general.getboolean('chmod_enabled') else None
saved_subtitles = save_subtitles(video.original_path, subtitles, single=single,
tags=None, # fixme
directory=fld,
2019-02-27 20:55:06 +00:00
chmod=chmod,
# formats=("srt", "vtt")
path_decoder=force_unicode
)
except Exception as e:
2019-09-17 13:51:40 +00:00
logging.exception('BAZARR Error saving Subtitles file to disk for this file:' + path)
pass
else:
saved_any = True
for subtitle in saved_subtitles:
downloaded_provider = subtitle.provider_name
2019-03-20 00:16:19 +00:00
if subtitle.language == 'pt-BR':
downloaded_language_code3 = 'pob'
else:
2019-03-20 03:44:50 +00:00
downloaded_language_code3 = subtitle.language.alpha3
2019-03-20 00:16:19 +00:00
downloaded_language = language_from_alpha3(downloaded_language_code3)
downloaded_language_code2 = alpha2_from_alpha3(downloaded_language_code3)
downloaded_path = subtitle.storage_path
2019-05-02 00:26:48 +00:00
is_forced_string = " forced" if subtitle.language.forced else ""
logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path)
2019-03-20 10:33:11 +00:00
if is_upgrade:
action = "upgraded"
else:
action = "downloaded"
if video.used_scene_name:
2019-05-02 00:26:48 +00:00
message = downloaded_language + is_forced_string + " subtitles " + action + " from " + downloaded_provider + " with a score of " + unicode(
round(subtitle.score * 100 / max_score, 2)) + "% using this scene name: " + sceneName
else:
2019-05-02 00:26:48 +00:00
message = downloaded_language + is_forced_string + " subtitles " + action + " from " + downloaded_provider + " with a score of " + unicode(
round(subtitle.score * 100 / max_score, 2)) + "% using filename guessing."
2019-01-15 16:25:13 +00:00
if use_postprocessing is True:
command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language,
2019-06-11 18:45:48 +00:00
downloaded_language_code2, downloaded_language_code3,
subtitle.language.forced)
try:
if os.name == 'nt':
codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
2018-10-12 02:24:27 +00:00
# wait for the process to terminate
out_codepage, err_codepage = codepage.communicate()
encoding = out_codepage.split(':')[-1].strip()
2019-01-15 16:25:13 +00:00
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# wait for the process to terminate
out, err = process.communicate()
2019-01-15 16:25:13 +00:00
if os.name == 'nt':
out = out.decode(encoding)
2019-01-15 16:25:13 +00:00
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')
2018-10-12 02:24:27 +00:00
else:
logging.info('BAZARR Post-processing result for file ' + path + ' : ' + out)
2019-01-15 16:25:13 +00:00
# fixme: support multiple languages at once
if media_type == 'series':
reversed_path = path_replace_reverse(path)
else:
reversed_path = path_replace_reverse_movie(path)
track_event(category=downloaded_provider, action=action, label=downloaded_language)
2019-05-02 00:26:48 +00:00
return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score, subtitle.language.forced
2019-01-15 16:25:13 +00:00
if not saved_any:
2019-09-17 13:51:40 +00:00
logging.debug('BAZARR No Subtitles were found for this file: ' + path)
return None
2019-07-28 19:49:19 +00:00
subliminal.region.backend.sync()
2019-07-28 19:49:19 +00:00
2019-09-17 13:51:40 +00:00
logging.debug('BAZARR Ended searching Subtitles for file: ' + path)
2017-10-16 23:27:19 +00:00
def manual_search(path, language, hi, forced, providers, providers_auth, sceneName, title, media_type):
logging.debug('BAZARR Manually searching subtitles for this file: ' + path)
2019-06-11 18:45:48 +00:00
final_subtitles = []
initial_hi = True if hi == "True" else False
if hi == "True":
2019-05-03 17:37:29 +00:00
hi = "force HI"
else:
2019-05-03 17:37:29 +00:00
hi = "force non-HI"
language_set = set()
2019-06-11 18:45:48 +00:00
if forced == "True":
providers_auth['podnapisi']['only_foreign'] = True
providers_auth['subscene']['only_foreign'] = True
providers_auth['opensubtitles']['only_foreign'] = True
else:
providers_auth['podnapisi']['only_foreign'] = False
providers_auth['subscene']['only_foreign'] = False
providers_auth['opensubtitles']['only_foreign'] = False
2019-06-11 18:45:48 +00:00
for lang in ast.literal_eval(language):
2018-10-12 03:44:34 +00:00
lang = alpha3_from_alpha2(lang)
if lang == 'pob':
lang_obj = Language('por', 'BR')
if forced == "True":
lang_obj = Language.rebuild(lang_obj, forced=True)
else:
lang_obj = Language(lang)
if forced == "True":
lang_obj = Language.rebuild(lang_obj, forced=True)
language_set.add(lang_obj)
2019-01-15 16:25:13 +00:00
use_scenename = settings.general.getboolean('use_scenename')
minimum_score = settings.general.minimum_score
minimum_score_movie = settings.general.minimum_score_movie
use_postprocessing = settings.general.getboolean('use_postprocessing')
2018-12-15 00:36:28 +00:00
postprocessing_cmd = settings.general.postprocessing_cmd
2019-07-28 19:49:19 +00:00
if providers:
video = get_video(force_unicode(path), title, sceneName, use_scenename, providers=providers,
2019-07-28 19:49:19 +00:00
media_type=media_type)
else:
logging.info("BAZARR All providers are throttled")
return None
if video:
min_score, max_score, scores = get_scores(video, media_type, min_score_movie_perc=int(minimum_score_movie),
min_score_series_perc=int(minimum_score))
2019-01-15 16:25:13 +00:00
try:
if providers:
subtitles = list_all_subtitles([video], language_set,
providers=providers,
provider_configs=providers_auth,
throttle_callback=provider_throttle,
language_hook=None) # fixme
else:
subtitles = []
logging.info("BAZARR All providers are throttled")
2019-07-28 19:49:19 +00:00
return None
except Exception as e:
2019-09-17 13:51:40 +00:00
logging.exception("BAZARR Error trying to get Subtitle list from provider for this file: " + path)
else:
subtitles_list = []
2019-01-15 16:25:13 +00:00
for s in subtitles[video]:
try:
matches = s.get_matches(video)
except AttributeError:
continue
2019-01-15 16:25:13 +00:00
# skip wrong season/episodes
if media_type == "series":
can_verify_series = True
if not s.hash_verifiable and "hash" in matches:
can_verify_series = False
2019-01-15 16:25:13 +00:00
if can_verify_series and not {"series", "season", "episode"}.issubset(matches):
logging.debug(u"BAZARR Skipping %s, because it doesn't match our series/episode", s)
continue
if s.hearing_impaired == initial_hi:
matches.add('hearing_impaired')
score = compute_score(matches, s, video, hearing_impaired=initial_hi)
not_matched = scores - matches
s.score = score
releases = ['n/a']
if hasattr(s, 'release_info'):
if s.release_info is not None:
releases = s.release_info.split(',')
subtitles_list.append(
dict(score=round((score / max_score * 100), 2),
2019-04-04 11:11:53 +00:00
language=str(s.language), hearing_impaired=str(s.hearing_impaired),
provider=s.provider_name,
subtitle=codecs.encode(pickle.dumps(s.make_picklable()), "base64").decode(),
url=s.page_link, matches=list(matches), dont_matches=list(not_matched),
release_info=releases))
2019-01-15 16:25:13 +00:00
final_subtitles = sorted(subtitles_list, key=lambda x: x['score'], reverse=True)
2019-09-17 13:51:40 +00:00
logging.debug('BAZARR ' + str(len(final_subtitles)) + " Subtitles have been found for this file: " + path)
logging.debug('BAZARR Ended searching Subtitles for this file: ' + path)
2019-07-28 19:49:19 +00:00
subliminal.region.backend.sync()
2019-07-28 19:49:19 +00:00
return final_subtitles
2019-06-11 18:45:48 +00:00
def manual_download_subtitle(path, language, hi, forced, subtitle, provider, providers_auth, sceneName, title,
media_type):
2019-09-17 13:51:40 +00:00
logging.debug('BAZARR Manually downloading Subtitles for this file: ' + path)
2019-06-11 18:45:48 +00:00
if settings.general.getboolean('utf8_encode'):
os.environ["SZ_KEEP_ENCODING"] = ""
else:
2019-08-16 02:01:42 +00:00
os.environ["SZ_KEEP_ENCODING"] = "True"
2019-06-11 18:45:48 +00:00
subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64"))
use_scenename = settings.general.getboolean('use_scenename')
use_postprocessing = settings.general.getboolean('use_postprocessing')
2018-12-15 00:36:28 +00:00
postprocessing_cmd = settings.general.postprocessing_cmd
single = settings.general.getboolean('single_language')
video = get_video(force_unicode(path), title, sceneName, use_scenename, providers={provider},
2019-07-28 19:49:19 +00:00
media_type=media_type)
if video:
min_score, max_score, scores = get_scores(video, media_type)
try:
if provider:
download_subtitles([subtitle], providers={provider}, provider_configs=providers_auth,
pool_class=provider_pool(), throttle_callback=provider_throttle)
logging.debug('BAZARR Subtitles file downloaded for this file:' + path)
else:
logging.info("BAZARR All providers are throttled")
return None
except Exception as e:
2019-09-17 13:51:40 +00:00
logging.exception('BAZARR Error downloading Subtitles for this file ' + path)
return None
else:
if not subtitle.is_valid():
2019-09-17 13:51:40 +00:00
logging.exception('BAZARR No valid Subtitles file found for this file: ' + path)
return
logging.debug('BAZARR Subtitles file downloaded for this file:' + path)
try:
score = round(subtitle.score / max_score * 100, 2)
fld = get_target_folder(path)
2019-06-11 18:45:48 +00:00
chmod = int(settings.general.chmod, 8) if not sys.platform.startswith(
'win') and settings.general.getboolean('chmod_enabled') else None
saved_subtitles = save_subtitles(video.original_path, [subtitle], single=single,
tags=None, # fixme
directory=fld,
chmod=chmod,
# formats=("srt", "vtt")
path_decoder=force_unicode)
2019-01-15 16:25:13 +00:00
except Exception as e:
2019-09-17 13:51:40 +00:00
logging.exception('BAZARR Error saving Subtitles file to disk for this file:' + path)
return
else:
if saved_subtitles:
for saved_subtitle in saved_subtitles:
downloaded_provider = saved_subtitle.provider_name
2019-03-20 00:16:19 +00:00
if saved_subtitle.language == 'pt-BR':
downloaded_language_code3 = 'pob'
else:
2019-03-20 03:44:50 +00:00
downloaded_language_code3 = subtitle.language.alpha3
2019-03-20 00:16:19 +00:00
downloaded_language = language_from_alpha3(downloaded_language_code3)
downloaded_language_code2 = alpha2_from_alpha3(downloaded_language_code3)
downloaded_path = saved_subtitle.storage_path
logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path)
2019-05-02 00:26:48 +00:00
is_forced_string = " forced" if subtitle.language.forced else ""
2019-09-17 13:51:40 +00:00
message = downloaded_language + is_forced_string + " Subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(
score) + "% using manual search."
2019-01-15 16:25:13 +00:00
if use_postprocessing is True:
command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language,
2019-06-11 18:45:48 +00:00
downloaded_language_code2, downloaded_language_code3,
subtitle.language.forced)
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()
2019-01-15 16:25:13 +00:00
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# wait for the process to terminate
out, err = process.communicate()
2019-01-15 16:25:13 +00:00
if os.name == 'nt':
out = out.decode(encoding)
2019-01-15 16:25:13 +00:00
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)
2018-10-12 02:24:27 +00:00
else:
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)
2019-06-11 18:45:48 +00:00
if media_type == 'series':
reversed_path = path_replace_reverse(path)
else:
reversed_path = path_replace_reverse_movie(path)
2019-09-04 11:29:37 +00:00
track_event(category=downloaded_provider, action="manually_downloaded", label=downloaded_language)
2019-06-11 18:45:48 +00:00
2019-05-02 00:26:48 +00:00
return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score, subtitle.language.forced
2018-10-12 02:24:27 +00:00
else:
logging.error(
2019-09-17 13:51:40 +00:00
"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.")
2018-10-12 02:24:27 +00:00
return None
2019-07-28 19:49:19 +00:00
subliminal.region.backend.sync()
2019-07-28 19:49:19 +00:00
2019-09-17 13:51:40 +00:00
logging.debug('BAZARR Ended manually downloading Subtitles for file: ' + path)
def manual_upload_subtitle(path, language, forced, title, scene_name, media_type, subtitle):
logging.debug('BAZARR Manually uploading subtitles for this file: ' + path)
single = settings.general.getboolean('single_language')
chmod = int(settings.general.chmod, 8) if not sys.platform.startswith(
'win') and settings.general.getboolean('chmod_enabled') else None
_, ext = os.path.splitext(subtitle.filename)
language = alpha3_from_alpha2(language)
if language == 'pob':
lang_obj = Language('por', 'BR')
else:
lang_obj = Language(language)
if forced:
lang_obj = Language.rebuild(lang_obj, forced=True)
subtitle_path = get_subtitle_path(video_path=force_unicode(path),
language=None if single else lang_obj,
extension=ext,
forced_tag=forced)
subtitle_path = force_unicode(subtitle_path)
if os.path.exists(subtitle_path):
os.remove(subtitle_path)
subtitle.save(subtitle_path)
if chmod:
os.chmod(subtitle_path, chmod)
2019-09-17 13:51:40 +00:00
message = language_from_alpha3(language) + (" forced" if forced else "") + " Subtitles manually uploaded."
if media_type == 'series':
reversed_path = path_replace_reverse(path)
else:
reversed_path = path_replace_reverse_movie(path)
return message, reversed_path
2017-10-16 23:27:19 +00:00
def series_download_subtitles(no):
2019-01-06 17:15:43 +00:00
if settings.sonarr.getboolean('only_monitored'):
2019-10-25 02:35:04 +00:00
episodes_details_clause = " AND monitored='True'"
else:
episodes_details_clause = ''
2019-01-15 16:25:13 +00:00
2019-10-25 02:35:04 +00:00
episodes_details = database.execute("SELECT path, missing_subtitles, sonarrEpisodeId, scene_name "
"FROM table_episodes WHERE sonarrSeriesId=? and missing_subtitles!='[]'" +
episodes_details_clause, (no,))
series_details = database.execute("SELECT hearing_impaired, title, forced FROM table_shows WHERE sonarrSeriesId=?",
2019-10-27 01:16:59 +00:00
(no,), only_one=True)
2019-01-15 16:25:13 +00:00
2018-09-22 22:07:46 +00:00
providers_list = get_providers()
providers_auth = get_providers_auth()
2019-01-15 16:25:13 +00:00
2019-10-25 02:35:04 +00:00
count_episodes_details = len(episodes_details)
2019-06-11 18:45:48 +00:00
2019-04-04 22:33:49 +00:00
for i, episode in enumerate(episodes_details, 1):
2019-07-28 19:49:19 +00:00
if providers_list:
2019-10-25 02:35:04 +00:00
for language in ast.literal_eval(episode['missing_subtitles']):
2019-07-28 19:49:19 +00:00
if language is not None:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Searching for Series Subtitles...', queue='get_subtitle', item=i,
2019-07-28 19:49:19 +00:00
length=count_episodes_details)
2019-10-25 02:35:04 +00:00
result = download_subtitle(path_replace(episode['path']),
2019-08-16 01:07:40 +00:00
str(alpha3_from_alpha2(language.split(':'))),
2019-10-25 02:35:04 +00:00
series_details['hearing_impaired'],
2019-08-16 02:14:03 +00:00
"True" if len(language.split(':')) > 1 else "False",
2019-08-16 01:07:40 +00:00
providers_list,
providers_auth,
2019-10-25 02:35:04 +00:00
str(episode['scene_name']),
series_details['title'],
2019-07-28 19:49:19 +00:00
'series')
if result is not None:
message = result[0]
path = result[1]
forced = result[5]
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-08-16 01:07:40 +00:00
store_subtitles(path_replace(episode.path))
2019-10-25 02:35:04 +00:00
history_log(1, no, episode['sonarrEpisodeId'], message, path, language_code, provider, score)
send_notifications(no, episode['sonarrEpisodeId'], message)
2019-07-28 19:49:19 +00:00
else:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
break
2019-06-11 18:45:48 +00:00
2019-04-23 01:08:26 +00:00
if count_episodes_details:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Search Complete. Please Reload The Page.', type='success', duration='permanent',
2019-06-11 18:45:48 +00:00
button='refresh', queue='get_subtitle')
2018-04-24 14:48:52 +00:00
def episode_download_subtitles(no):
if settings.sonarr.getboolean('only_monitored'):
2019-10-25 02:35:04 +00:00
episodes_details_clause = " AND monitored='True'"
else:
episodes_details_clause = ''
2019-06-11 18:45:48 +00:00
2019-10-25 02:35:04 +00:00
episodes_details = database.execute("SELECT table_episodes.path, table_episodes.missing_subtitles, "
"table_episodes.sonarrEpisodeId, table_episodes.scene_name, "
"table_shows.hearing_impaired, table_shows.title, table_shows.sonarrSeriesId, "
"table_shows.forced FROM table_episodes LEFT JOIN table_shows on "
"table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId "
"WHERE sonarrEpisodeId=?" + episodes_details_clause, (no,))
2019-06-11 18:45:48 +00:00
providers_list = get_providers()
providers_auth = get_providers_auth()
2019-06-11 18:45:48 +00:00
for episode in episodes_details:
2019-07-28 19:49:19 +00:00
if providers_list:
2019-10-25 02:35:04 +00:00
for language in ast.literal_eval(episode['missing_subtitles']):
2019-07-28 19:49:19 +00:00
if language is not None:
notifications.write(msg='Searching for ' + str(
2019-10-25 02:35:04 +00:00
language_from_alpha2(language)) + ' Subtitles for this episode: ' +
path_replace(episode['path']), queue='get_subtitle')
result = download_subtitle(path_replace(episode['path']),
2019-08-15 20:36:53 +00:00
str(alpha3_from_alpha2(language.split(':')[0])),
2019-10-25 02:35:04 +00:00
episode['hearing_impaired'],
2019-08-16 01:55:21 +00:00
"True" if len(language.split(':')) > 1 else "False",
2019-08-15 20:36:53 +00:00
providers_list,
providers_auth,
2019-10-25 02:35:04 +00:00
str(episode['scene_name']),
episode['title'],
2019-08-15 20:36:53 +00:00
'series')
2019-07-28 19:49:19 +00:00
if result is not None:
message = result[0]
path = result[1]
forced = result[5]
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-10-25 02:35:04 +00:00
store_subtitles(path_replace(episode['path']))
history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path, language_code, provider, score)
send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message)
2019-07-28 19:49:19 +00:00
else:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
break
2018-04-24 14:48:52 +00:00
def movies_download_subtitles(no):
2019-10-25 02:35:04 +00:00
if settings.radarr.getboolean('only_monitored'):
movie_details_clause = " AND monitored='True'"
else:
movie_details_clause = ''
movie = database.execute("SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired, title, forced "
2019-10-27 01:16:59 +00:00
"FROM table_movies WHERE radarrId=?" + movie_details_clause, (no,), only_one=True)
2019-06-11 18:45:48 +00:00
2018-09-22 22:07:46 +00:00
providers_list = get_providers()
providers_auth = get_providers_auth()
2019-01-15 16:25:13 +00:00
2019-10-25 02:35:04 +00:00
count_movie = len(ast.literal_eval(movie['missing_subtitles']))
2019-06-11 18:45:48 +00:00
2019-10-25 02:35:04 +00:00
for i, language in enumerate(ast.literal_eval(movie['missing_subtitles']), 1):
2019-07-28 19:49:19 +00:00
if providers_list:
if language is not None:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Searching for Movie Subtitles', queue='get_subtitle', item=i,
2019-07-28 19:49:19 +00:00
length=count_movie)
2019-10-25 02:35:04 +00:00
result = download_subtitle(path_replace_movie(movie['path']),
2019-08-15 20:36:53 +00:00
str(alpha3_from_alpha2(language.split(':')[0])),
2019-10-25 02:35:04 +00:00
movie['hearing_impaired'],
2019-08-16 01:55:21 +00:00
"True" if len(language.split(':')) > 1 else "False",
2019-08-15 20:36:53 +00:00
providers_list,
providers_auth,
2019-10-25 02:35:04 +00:00
str(movie['sceneName']),
movie['title'],
2019-08-15 20:36:53 +00:00
'movie')
2019-07-28 19:49:19 +00:00
if result is not None:
message = result[0]
path = result[1]
forced = result[5]
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-10-25 02:35:04 +00:00
store_subtitles_movie(path_replace_movie(movie['path']))
2019-07-28 19:49:19 +00:00
history_log_movie(1, no, message, path, language_code, provider, score)
send_notifications_movie(no, message)
else:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
break
2019-06-11 18:45:48 +00:00
2019-04-23 01:08:26 +00:00
if count_movie:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Search Complete. Please Reload The Page.', type='success', duration='permanent',
2019-06-11 18:45:48 +00:00
button='refresh', queue='get_subtitle')
2018-04-24 14:48:52 +00:00
2019-04-04 22:33:49 +00:00
def wanted_download_subtitles(path, l, count_episodes):
2019-10-25 02:35:04 +00:00
episodes_details = database.execute("SELECT table_episodes.path, table_episodes.missing_subtitles, "
"table_episodes.sonarrEpisodeId, table_episodes.sonarrSeriesId, "
"table_shows.hearing_impaired, table_episodes.scene_name,"
"table_episodes.failedAttempts, table_shows.title, table_shows.forced "
"FROM table_episodes LEFT JOIN table_shows on "
"table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId "
"WHERE table_episodes.path=? and table_episodes.missing_subtitles!='[]'",
(path_replace_reverse(path),))
2019-01-15 16:25:13 +00:00
2018-09-22 22:07:46 +00:00
providers_list = get_providers()
providers_auth = get_providers_auth()
2019-01-15 16:25:13 +00:00
2017-11-16 19:09:40 +00:00
for episode in episodes_details:
2019-10-25 02:35:04 +00:00
attempt = episode['failed_attempts']
if type(attempt) == unicode:
attempt = ast.literal_eval(attempt)
2019-10-25 02:35:04 +00:00
for language in ast.literal_eval(episode['missing_subtitles']):
if attempt is None:
attempt = []
attempt.append([language, time.time()])
else:
att = zip(*attempt)[0]
if language not in att:
attempt.append([language, time.time()])
2019-08-17 14:24:55 +00:00
2019-10-25 02:35:04 +00:00
database.execute("UPDATE table_episodes SET failedAttempts=? WHERE sonarrEpisodeId=?",
(unicode(attempt), episode['sonarrEpisdoeId']))
2019-01-15 16:25:13 +00:00
for i in range(len(attempt)):
if attempt[i][0] == language:
if search_active(attempt[i][1]):
2019-09-17 13:51:40 +00:00
notifications.write(msg='Searching for Series Subtitles...', queue='get_subtitle', item=l,
2019-06-11 18:45:48 +00:00
length=count_episodes)
2019-10-25 02:35:04 +00:00
result = download_subtitle(path_replace(episode['path']),
2019-08-15 20:36:53 +00:00
str(alpha3_from_alpha2(language.split(':')[0])),
2019-10-25 02:35:04 +00:00
episode['hearing_impaired'],
2019-08-16 01:55:21 +00:00
"True" if len(language.split(':')) > 1 else "False",
2019-08-15 20:36:53 +00:00
providers_list,
providers_auth,
2019-10-25 02:35:04 +00:00
str(episode['scene_name']),
episode['title'],
2019-08-15 20:36:53 +00:00
'series')
if result is not None:
message = result[0]
path = result[1]
2019-05-02 00:26:48 +00:00
forced = result[5]
2019-06-16 19:24:47 +00:00
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-10-25 02:35:04 +00:00
store_subtitles(path_replace(episode['path']))
history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path, language_code, provider, score)
send_notifications(episode['sonarrSeriesId'], episode['sonarrSeriesId'], message)
else:
logging.debug(
2019-10-25 02:35:04 +00:00
'BAZARR Search is not active for episode ' + episode['path'] + ' Language: ' + attempt[i][0])
2017-11-16 17:04:20 +00:00
2019-04-04 22:33:49 +00:00
def wanted_download_subtitles_movie(path, l, count_movies):
2019-10-25 02:35:04 +00:00
movies_details = database.execute("SELECT path, missing_subtitles, radarrId, hearing_impaired, sceneName, "
"failedAttempts, title, forced FROM table_movies WHERE path = ? "
"AND missing_subtitles != '[]'", (path_replace_reverse_movie(path),))
2019-01-15 16:25:13 +00:00
2018-09-22 22:07:46 +00:00
providers_list = get_providers()
providers_auth = get_providers_auth()
2019-01-15 16:25:13 +00:00
for movie in movies_details:
2019-10-25 02:35:04 +00:00
attempt = movie['failed_attempts']
if type(attempt) == unicode:
attempt = ast.literal_eval(attempt)
2019-10-25 02:35:04 +00:00
for language in ast.literal_eval(movie['missing_subtitles']):
if attempt is None:
attempt = []
attempt.append([language, time.time()])
else:
att = zip(*attempt)[0]
if language not in att:
attempt.append([language, time.time()])
2019-01-15 16:25:13 +00:00
2019-10-25 02:35:04 +00:00
database.execute("UPDATE table_movies SET failedAttempts=? WHERE radarrId=?",
(unicode(attempt), movie['radarrId']))
2019-01-15 16:25:13 +00:00
for i in range(len(attempt)):
if attempt[i][0] == language:
if search_active(attempt[i][1]) is True:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Searching for Movie Subtitles...', queue='get_subtitle', item=l,
2019-06-11 18:45:48 +00:00
length=count_movies)
2019-10-25 02:35:04 +00:00
result = download_subtitle(path_replace_movie(movie['path']),
2019-08-15 20:36:53 +00:00
str(alpha3_from_alpha2(language.split(':')[0])),
2019-10-25 02:35:04 +00:00
movie['hearing_impaired'],
2019-08-16 01:55:21 +00:00
"True" if len(language.split(':')) > 1 else "False",
2019-08-15 20:36:53 +00:00
providers_list,
providers_auth,
2019-10-25 02:35:04 +00:00
str(movie['sceneName']),
movie['title'],
2019-08-15 20:36:53 +00:00
'movie')
if result is not None:
message = result[0]
path = result[1]
2019-05-02 00:26:48 +00:00
forced = result[5]
2019-06-16 23:29:28 +00:00
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-10-25 02:35:04 +00:00
store_subtitles_movie(path_replace_movie(movie['path']))
history_log_movie(1, movie['radarrId'], message, path, language_code, provider, score)
send_notifications_movie(movie['radarrId'], message)
else:
logging.info(
2019-10-25 02:35:04 +00:00
'BAZARR Search is not active for this Movie ' + movie['path'] + ' Language: ' + attempt[i][0])
2017-10-23 03:00:11 +00:00
def wanted_search_missing_subtitles():
if settings.general.getboolean('use_sonarr'):
2019-08-17 14:24:55 +00:00
if settings.sonarr.getboolean('only_monitored'):
2019-10-25 02:35:04 +00:00
monitored_only_query_string_sonarr = ' AND monitored = "True"'
else:
monitored_only_query_string_sonarr = ""
2019-08-17 14:24:55 +00:00
2019-10-25 02:35:04 +00:00
episodes = database.execute("SELECT path FROM table_episodes WHERE missing_subtitles != '[]'" +
monitored_only_query_string_sonarr)
2019-10-26 18:52:22 +00:00
# path_replace
dict_mapper.path_replace(episodes)
2019-08-17 14:24:55 +00:00
2019-10-25 02:35:04 +00:00
count_episodes = len(episodes)
2019-07-28 19:49:19 +00:00
for i, episode in enumerate(episodes, 1):
providers = get_providers()
if providers:
2019-10-25 02:35:04 +00:00
wanted_download_subtitles(episode['path'], i, count_episodes)
2019-07-28 19:49:19 +00:00
else:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
return
2019-01-15 16:25:13 +00:00
if settings.general.getboolean('use_radarr'):
2019-08-17 14:24:55 +00:00
if settings.radarr.getboolean('only_monitored'):
2019-10-25 02:35:04 +00:00
monitored_only_query_string_radarr = ' AND monitored = "True"'
else:
monitored_only_query_string_radarr = ""
movies = database.execute("SELECT path FROM table_movies WHERE missing_subtitles != '[]'" +
monitored_only_query_string_radarr)
2019-10-26 18:52:22 +00:00
# path_replace
dict_mapper.path_replace_movie(movies)
2019-10-25 02:35:04 +00:00
count_movies = len(movies)
2019-07-28 19:49:19 +00:00
for i, movie in enumerate(movies, 1):
providers = get_providers()
if providers:
2019-10-25 02:35:04 +00:00
wanted_download_subtitles_movie(movie['path'], i, count_movies)
2019-07-28 19:49:19 +00:00
else:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
return
2019-01-15 16:25:13 +00:00
2019-09-17 13:51:40 +00:00
logging.info('BAZARR Finished searching for missing Subtitles. Check History for more information.')
2019-06-11 18:45:48 +00:00
notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent',
button='refresh', queue='get_subtitle')
def search_active(timestamp):
2019-01-06 17:15:43 +00:00
if settings.general.getboolean('adaptive_searching'):
search_deadline = timedelta(weeks=3)
search_delta = timedelta(weeks=1)
aa = datetime.fromtimestamp(float(timestamp))
attempt_datetime = datetime.strptime(str(aa).split(".")[0], '%Y-%m-%d %H:%M:%S')
attempt_search_deadline = attempt_datetime + search_deadline
today = datetime.today()
attempt_age_in_days = (today.date() - attempt_search_deadline.date()).days
if today.date() <= attempt_search_deadline.date():
return True
elif attempt_age_in_days % search_delta.days == 0:
return True
else:
return False
else:
return True
def refine_from_db(path, video):
if isinstance(video, Episode):
2019-10-25 02:35:04 +00:00
data = database.execute("SELECT table_shows.title as seriesTitle, table_episodes.season, table_episodes.episode, "
"table_episodes.title as episodeTitle, table_shows.year, table_shows.tvdbId, "
"table_shows.alternateTitles, table_episodes.format, table_episodes.resolution, "
"table_episodes.video_codec, table_episodes.audio_codec, table_episodes.path "
"FROM table_episodes INNER JOIN table_shows on "
"table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId "
2019-10-27 01:16:59 +00:00
"WHERE table_episodes.path = ?", (unicode(path_replace_reverse(path)),), only_one=True)
2019-08-17 14:24:55 +00:00
2019-02-08 03:34:54 +00:00
if data:
2019-10-25 02:35:04 +00:00
video.series, year, country = series_re.match(data['seriesTitle']).groups()
video.season = int(data['season'])
video.episode = int(data['episode'])
video.title = data['episodeTitle']
2019-10-26 18:52:22 +00:00
if data['year']:
2019-10-25 02:35:04 +00:00
if int(data['year']) > 0: video.year = int(data['year'])
2019-10-26 18:52:22 +00:00
video.series_tvdb_id = int(data['tvdbId'])
video.alternative_series = ast.literal_eval(data['alternateTitles'])
2019-02-06 11:51:19 +00:00
if not video.format:
2019-10-25 02:35:04 +00:00
video.format = str(data['format'])
2019-02-06 03:49:58 +00:00
if not video.resolution:
2019-10-25 02:35:04 +00:00
video.resolution = str(data['resolution'])
2019-02-06 03:49:58 +00:00
if not video.video_codec:
2019-10-25 02:35:04 +00:00
if data['video_codec']: video.video_codec = data['video_codec']
2019-02-06 03:49:58 +00:00
if not video.audio_codec:
2019-10-25 02:35:04 +00:00
if data['audio_codec']: video.audio_codec = data['audio_codec']
elif isinstance(video, Movie):
2019-10-25 02:35:04 +00:00
data = database.execute("SELECT title, year, alternativeTitles, format, resolution, video_codec, audio_codec, "
2019-10-27 01:16:59 +00:00
"imdbId FROM table_movies WHERE path = ?",
(unicode(path_replace_reverse_movie(path)),), only_one=True)
2019-08-17 14:24:55 +00:00
2019-02-08 03:34:54 +00:00
if data:
2019-10-25 02:35:04 +00:00
video.title = re.sub(r'(\(\d\d\d\d\))', '', data['title'])
2019-10-26 18:52:22 +00:00
if data['year']:
2019-10-25 02:35:04 +00:00
if int(data['year']) > 0: video.year = int(data['year'])
if data['imdb_id']: video.imdb_id = data['imdb_id']
video.alternative_titles = ast.literal_eval(data['alternative_titles'])
2019-02-06 11:51:19 +00:00
if not video.format:
2019-10-25 02:35:04 +00:00
if data['format']: video.format = data['format']
2019-02-06 03:49:58 +00:00
if not video.resolution:
2019-10-25 02:35:04 +00:00
if data['resolution']: video.resolution = data['resolution']
2019-02-06 03:49:58 +00:00
if not video.video_codec:
2019-10-25 02:35:04 +00:00
if data['video_codec']: video.video_codec = data['video_codec']
2019-02-06 03:49:58 +00:00
if not video.audio_codec:
2019-10-25 02:35:04 +00:00
if data['audio_codec']: video.audio_codec = data['audio_codec']
2019-06-11 18:45:48 +00:00
return video
def refine_from_ffprobe(path, video):
exe = get_binary('ffprobe')
if not exe:
logging.debug('BAZARR FFprobe not found!')
return
else:
logging.debug('BAZARR FFprobe used is %s', exe)
2019-07-28 19:49:19 +00:00
parser = VideoFileParser(ffprobe=exe, includeMissing=True, rawMode=False)
data = parser.parseFfprobe(path)
logging.debug('FFprobe found: %s', data)
if 'videos' not in data:
logging.debug('BAZARR FFprobe was unable to find video tracks in the file!')
else:
if 'resolution' in data['videos'][0]:
if not video.resolution:
if data['videos'][0]['resolution'][0] >= 3200:
video.resolution = "2160p"
elif data['videos'][0]['resolution'][0] >= 1800:
video.resolution = "1080p"
elif data['videos'][0]['resolution'][0] >= 1200:
video.resolution = "720p"
elif data['videos'][0]['resolution'][0] >= 0:
video.resolution = "480p"
if 'codec' in data['videos'][0]:
if not video.video_codec:
video.video_codec = data['videos'][0]['codec']
2019-08-26 12:21:36 +00:00
if 'framerate' in data['videos'][0]:
if not video.fps:
video.fps = data['videos'][0]['framerate']
if 'audios' not in data:
logging.debug('BAZARR FFprobe was unable to find audio tracks in the file!')
else:
if 'codec' in data['audios'][0]:
if not video.audio_codec:
video.audio_codec = data['audios'][0]['codec'].upper()
def upgrade_subtitles():
days_to_upgrade_subs = settings.general.days_to_upgrade_subs
2019-03-17 14:29:38 +00:00
minimum_timestamp = ((datetime.now() - timedelta(days=int(days_to_upgrade_subs))) -
datetime(1970, 1, 1)).total_seconds()
2019-08-30 00:16:11 +00:00
if settings.sonarr.getboolean('only_monitored'):
2019-10-25 02:35:04 +00:00
series_monitored_only_query_string = ' AND table_episodes.monitored = "True"'
else:
series_monitored_only_query_string = ""
2019-08-30 00:16:11 +00:00
if settings.radarr.getboolean('only_monitored'):
2019-10-25 02:35:04 +00:00
movies_monitored_only_query_string = ' AND table_movies.monitored = "True"'
else:
movies_monitored_only_query_string = ""
2019-08-30 00:16:11 +00:00
2019-10-25 02:35:04 +00:00
if settings.general.getboolean('upgrade_manual'):
query_actions = [1, 2, 3]
else:
query_actions = [1, 3]
2019-08-30 00:16:11 +00:00
if settings.general.getboolean('use_sonarr'):
2019-10-25 02:35:04 +00:00
upgradable_episodes = database.execute("SELECT table_history.video_path, table_history.language, "
"table_history.score, table_shows.hearing_impaired, "
"table_episodes.scene_name, table_episodes.title,"
"table_episodes.sonarrSeriesId, table_episodes.sonarrEpisodeId,"
"MAX(table_history.timestamp), table_shows.languages, table_shows.forced "
"FROM table_history INNER JOIN table_shows on "
"table_shows.sonarrSeriesId = table_history.sonarrSeriesId INNER JOIN "
"table_episodes on table_episodes.sonarrEpisodeId = "
"table_history.sonarrEpisodeId WHERE action IN "
"(" + ','.join(map(str, query_actions)) + ") AND timestamp > ? AND "
"score is not null" + series_monitored_only_query_string +
"GROUP BY table_history.video_path, table_history.language",
(minimum_timestamp,))
2019-08-19 22:13:29 +00:00
upgradable_episodes_not_perfect = []
2019-10-25 02:35:04 +00:00
for upgradable_episode in upgradable_episodes:
2019-08-19 22:13:29 +00:00
if upgradable_episode['timestamp'] > minimum_timestamp:
try:
int(upgradable_episode['score'])
except ValueError:
pass
else:
if int(upgradable_episode['score']) < 360:
upgradable_episodes_not_perfect.append(upgradable_episode)
episodes_to_upgrade = []
for episode in upgradable_episodes_not_perfect:
if os.path.exists(path_replace(episode['video_path'])) and int(episode['score']) < 357:
episodes_to_upgrade.append(episode)
2019-10-07 23:36:20 +00:00
count_episode_to_upgrade = len(episodes_to_upgrade)
if settings.general.getboolean('use_radarr'):
2019-10-25 02:35:04 +00:00
upgradable_movies = database.execute("SELECT table_history_movie.video_path, table_history_movie.language, "
"table_history_movie.score, table_movies.hearing_impaired, "
"table_movies.sceneName, table_movies.title, table_movies.radarrId, "
"MAX(table_history_movie.timestamp), table_movies.languages, "
"table_movies.forced FROM table_history_movie INNER JOIN "
"table_movies on table_movies.radarrId = table_history_movie.radarrId "
"WHERE action IN (" + ','.join(map(str, query_actions)) +
") AND timestamp > ? AND score is not null" +
movies_monitored_only_query_string +
" GROUP BY table_history_movie.video_path, table_history_movie.language",
(minimum_timestamp,))
2019-08-19 22:13:29 +00:00
upgradable_movies_not_perfect = []
2019-10-25 02:35:04 +00:00
for upgradable_movie in upgradable_movies:
2019-08-19 22:13:29 +00:00
if upgradable_movie['timestamp'] > minimum_timestamp:
try:
int(upgradable_movie['score'])
except ValueError:
pass
else:
if int(upgradable_movie['score']) < 360:
upgradable_movies_not_perfect.append(upgradable_movie)
movies_to_upgrade = []
for movie in upgradable_movies_not_perfect:
if os.path.exists(path_replace_movie(movie['video_path'])) and int(movie['score']) < 117:
movies_to_upgrade.append(movie)
2019-10-07 23:36:20 +00:00
count_movie_to_upgrade = len(movies_to_upgrade)
2019-06-11 18:45:48 +00:00
2019-03-16 19:30:06 +00:00
providers_list = get_providers()
providers_auth = get_providers_auth()
2019-10-07 23:36:20 +00:00
if settings.general.getboolean('use_sonarr'):
for i, episode in enumerate(episodes_to_upgrade, 1):
2019-07-28 19:49:19 +00:00
providers = get_providers()
if not providers:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
return
2019-10-15 15:03:47 +00:00
if episode['languages']:
2019-08-19 22:13:29 +00:00
desired_languages = ast.literal_eval(str(episode['languages']))
if episode['forced'] == "True":
2019-05-10 13:24:11 +00:00
forced_languages = [l + ":forced" for l in desired_languages]
2019-08-19 22:13:29 +00:00
elif episode['forced'] == "Both":
2019-05-10 13:24:11 +00:00
forced_languages = [l + ":forced" for l in desired_languages] + desired_languages
else:
forced_languages = desired_languages
2019-06-11 18:45:48 +00:00
2019-08-19 22:13:29 +00:00
if episode['language'] in forced_languages:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Upgrading Series Subtitles...',
queue='upgrade_subtitle', item=i, length=count_episode_to_upgrade)
2019-06-11 18:45:48 +00:00
2019-08-19 22:13:29 +00:00
if episode['language'].endswith('forced'):
language = episode['language'].split(':')[0]
2019-05-10 13:24:11 +00:00
is_forced = "True"
else:
2019-08-19 22:13:29 +00:00
language = episode['language']
2019-05-10 13:24:11 +00:00
is_forced = "False"
2019-06-11 18:45:48 +00:00
2019-08-19 22:13:29 +00:00
result = download_subtitle(path_replace(episode['video_path']),
str(alpha3_from_alpha2(language)),
episode['hearing_impaired'],
is_forced,
providers_list,
providers_auth,
str(episode['scene_name']),
episode['title'],
'series',
forced_minimum_score=int(episode['score']),
2019-05-10 13:24:11 +00:00
is_upgrade=True)
if result is not None:
message = result[0]
path = result[1]
2019-05-02 00:26:48 +00:00
forced = result[5]
2019-06-16 19:24:47 +00:00
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-08-19 22:13:29 +00:00
store_subtitles(path_replace(episode['video_path']))
2019-10-25 02:35:04 +00:00
history_log(3, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path, language_code, provider, score)
send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message)
2019-06-11 18:45:48 +00:00
if settings.general.getboolean('use_radarr'):
for i, movie in enumerate(movies_to_upgrade, 1):
2019-07-28 19:49:19 +00:00
providers = get_providers()
if not providers:
notifications.write(msg='BAZARR All providers are throttled', queue='get_subtitle', duration='long')
logging.info("BAZARR All providers are throttled")
return
2019-10-15 15:03:47 +00:00
if movie['languages']:
2019-08-19 22:13:29 +00:00
desired_languages = ast.literal_eval(str(movie['languages']))
if movie['forced'] == "True":
2019-05-10 13:24:11 +00:00
forced_languages = [l + ":forced" for l in desired_languages]
2019-08-19 22:13:29 +00:00
elif movie['forced'] == "Both":
2019-05-10 13:24:11 +00:00
forced_languages = [l + ":forced" for l in desired_languages] + desired_languages
else:
forced_languages = desired_languages
2019-06-11 18:45:48 +00:00
2019-08-19 22:13:29 +00:00
if movie['language'] in forced_languages:
2019-09-17 13:51:40 +00:00
notifications.write(msg='Upgrading Movie Subtitles...',
2019-06-11 18:45:48 +00:00
queue='upgrade_subtitle', item=i, length=count_movie_to_upgrade)
2019-08-19 22:13:29 +00:00
if movie['language'].endswith('forced'):
language = movie['language'].split(':')[0]
2019-05-10 13:24:11 +00:00
is_forced = "True"
else:
2019-08-19 22:13:29 +00:00
language = movie['language']
2019-05-10 13:24:11 +00:00
is_forced = "False"
2019-06-11 18:45:48 +00:00
2019-08-19 22:13:29 +00:00
result = download_subtitle(path_replace_movie(movie['video_path']),
str(alpha3_from_alpha2(language)),
movie['hearing_impaired'],
is_forced,
providers_list,
providers_auth,
2019-10-25 02:35:04 +00:00
str(movie['sceneName']),
2019-08-19 22:13:29 +00:00
movie['title'],
'movie',
forced_minimum_score=int(movie['score']),
is_upgrade=True)
if result is not None:
message = result[0]
path = result[1]
2019-05-02 00:26:48 +00:00
forced = result[5]
2019-06-16 23:29:28 +00:00
language_code = result[2] + ":forced" if forced else result[2]
provider = result[3]
score = result[4]
2019-08-19 22:13:29 +00:00
store_subtitles_movie(path_replace_movie(movie['video_path']))
2019-10-25 02:35:04 +00:00
history_log_movie(3, movie['radarrId'], message, path, language_code, provider, score)
send_notifications_movie(movie['radarrId'], message)