2018-10-31 19:34:40 +00:00
# coding=utf-8
2018-08-16 02:01:49 +00:00
2017-09-17 00:11:47 +00:00
import os
2019-01-24 20:39:23 +00:00
import sys
2017-10-16 23:27:19 +00:00
import sqlite3
import ast
2017-12-06 04:07:37 +00:00
import logging
2018-10-09 20:33:18 +00:00
import operator
2018-03-24 00:00:50 +00:00
import subprocess
2018-08-27 00:53:02 +00:00
import time
2018-10-31 19:34:40 +00:00
import cPickle as pickle
import codecs
2018-11-29 13:03:44 +00:00
import types
2019-02-05 11:57:45 +00:00
import re
2018-10-31 16:33:19 +00:00
import subliminal
import subliminal_patch
2018-08-27 00:53:02 +00:00
from datetime import datetime , timedelta
2018-10-31 17:10:04 +00:00
from subzero . language import Language
2019-02-05 03:57:15 +00:00
from subzero . video import parse_video
2018-11-29 14:01:22 +00:00
from subliminal import region , score as subliminal_scores , \
2019-02-05 03:57:15 +00:00
list_subtitles , Episode , Movie
2018-11-29 13:40:31 +00:00
from subliminal_patch . core import SZAsyncProviderPool , download_best_subtitles , save_subtitles , download_subtitles
2018-10-31 17:10:04 +00:00
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 , \
2019-01-24 20:39:23 +00:00
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
2018-04-24 14:48:52 +00:00
from notifier import send_notifications , send_notifications_movie
2019-01-24 14:00:03 +00:00
from get_providers import get_providers , get_providers_auth , provider_throttle , provider_pool
2018-10-31 19:34:40 +00:00
from get_args import args
2019-02-21 04:30:25 +00:00
from queueconfig import notifications
2017-09-17 00:11:47 +00:00
# configure the cache
2018-10-31 17:10:04 +00:00
# fixme: do this inside a setup routine
2018-01-16 04:30:41 +00:00
region . configure ( ' dogpile.cache.memory ' )
2017-09-17 00:11:47 +00:00
2018-10-31 17:10:04 +00:00
2018-11-29 13:03:44 +00:00
def get_video ( path , title , sceneName , use_scenename , providers = None , media_type = " movie " ) :
2018-11-29 13:47:56 +00:00
"""
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
"""
2018-11-29 13:03:44 +00:00
hints = { " title " : title , " type " : " movie " if media_type == " movie " else " episode " }
dont_use_actual_file = False
2019-01-16 14:34:33 +00:00
original_path = path
2019-01-15 12:35:58 +00:00
original_name = os . path . basename ( path )
2018-11-29 13:03:44 +00:00
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 ] )
dont_use_actual_file = True
2019-02-03 12:17:20 +00:00
if providers :
2019-02-03 04:28:58 +00:00
try :
2019-01-24 14:00:03 +00:00
video = parse_video ( path , hints = hints , providers = providers , dry_run = dont_use_actual_file )
video . used_scene_name = dont_use_actual_file
video . original_name = original_name
video . original_path = original_path
2019-02-12 08:51:31 +00:00
refine_from_db ( original_path , video )
2019-03-15 20:04:28 +00:00
logging . debug ( ' BAZARR is using those video object properties: %s ' , vars ( video ) )
2019-01-24 14:00:03 +00:00
return video
2019-02-12 08:51:31 +00:00
2019-02-03 12:17:20 +00:00
except :
logging . exception ( " BAZARR Error trying to get video information for this file: " + path )
else :
logging . info ( " BAZARR All providers are throttled " )
return None
2018-11-29 13:03:44 +00:00
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 ) :
2018-11-29 13:47:56 +00:00
"""
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 ) )
"""
2018-11-29 13:21:48 +00:00
max_score = 120.0
2018-11-29 13:03:44 +00:00
min_score = max_score * min_score_movie_perc / 100.0
scores = subliminal_scores . movie_scores . keys ( )
if media_type == " series " :
2018-11-29 13:21:48 +00:00
max_score = 360.0
2018-11-29 13:03:44 +00:00
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
2018-11-29 13:03:44 +00:00
return min_score , max_score , set ( scores )
2019-03-20 10:33:11 +00:00
def download_subtitle ( path , language , hi , providers , providers_auth , sceneName , title , media_type , forced_minimum_score = None , is_upgrade = False ) :
2018-11-29 13:03:44 +00:00
# fixme: supply all missing languages, not only one, to hit providers only once who support multiple languages in
# one query
2019-02-05 03:57:15 +00:00
2018-10-19 03:33:01 +00:00
logging . debug ( ' BAZARR Searching subtitles for this file: ' + path )
2018-09-10 21:10:33 +00:00
if hi == " True " :
hi = True
else :
hi = False
2018-10-09 20:33:18 +00:00
language_set = set ( )
2019-03-20 00:16:19 +00:00
2018-11-29 13:11:10 +00:00
if not isinstance ( language , types . ListType ) :
2018-11-29 13:59:18 +00:00
language = [ language ]
2019-01-15 16:25:13 +00:00
2018-11-29 13:11:10 +00:00
for l in language :
if l == ' pob ' :
language_set . add ( Language ( ' por ' , ' BR ' ) )
else :
language_set . add ( Language ( l ) )
2019-01-15 16:25:13 +00:00
2018-12-27 19:19:59 +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
2018-12-27 19:19:59 +00:00
use_postprocessing = settings . general . getboolean ( ' use_postprocessing ' )
2018-12-15 00:36:28 +00:00
postprocessing_cmd = settings . general . postprocessing_cmd
2019-01-02 19:19:31 +00:00
single = settings . general . getboolean ( ' single_language ' )
2019-01-15 16:25:13 +00:00
2018-10-31 17:10:04 +00:00
# todo:
"""
AsyncProviderPool :
implement :
2019-01-01 20:15:12 +00:00
blacklist = None ,
pre_download_hook = None ,
post_download_hook = None ,
2018-10-31 17:10:04 +00:00
language_hook = None
"""
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +00:00
video = get_video ( path , title , sceneName , use_scenename , providers = providers , 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-01-24 14:00:03 +00:00
if providers :
2019-03-16 19:30:06 +00:00
if forced_minimum_score :
min_score = int ( forced_minimum_score ) + 1
2019-01-24 14:00:03 +00:00
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-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +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
2018-11-29 13:03:44 +00:00
try :
2019-01-24 20:39:23 +00:00
fld = get_target_folder ( path )
2019-03-26 19:49:03 +00:00
chmod = int ( settings . general . chmod , 8 ) if not sys . platform . startswith ( ' win ' ) and settings . general . getboolean ( ' chmod_enabled ' ) else None
2019-01-16 14:34:33 +00:00
saved_subtitles = save_subtitles ( video . original_path , subtitles , single = single ,
2018-11-29 13:03:44 +00:00
tags = None , # fixme
2019-01-24 20:39:23 +00:00
directory = fld ,
2019-02-27 20:55:06 +00:00
chmod = chmod ,
2018-11-29 13:03:44 +00:00
# formats=("srt", "vtt")
path_decoder = force_unicode
)
except Exception as e :
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 )
2018-11-29 13:03:44 +00:00
downloaded_path = subtitle . storage_path
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 "
2018-11-29 13:03:44 +00:00
if video . used_scene_name :
2019-03-20 10:33:11 +00:00
message = downloaded_language + " subtitles " + action + " from " + downloaded_provider + " with a score of " + unicode (
2018-11-30 13:35:28 +00:00
round ( subtitle . score * 100 / max_score , 2 ) ) + " % u sing this scene name: " + sceneName
2018-10-23 03:08:44 +00:00
else :
2019-03-20 10:33:11 +00:00
message = downloaded_language + " subtitles " + action + " from " + downloaded_provider + " with a score of " + unicode (
2018-11-30 13:35:28 +00:00
round ( subtitle . score * 100 / max_score , 2 ) ) + " % u sing filename guessing. "
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +00:00
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 )
2018-10-12 02:24:27 +00:00
# wait for the process to terminate
2018-11-29 13:03:44 +00:00
out_codepage , err_codepage = codepage . communicate ( )
encoding = out_codepage . split ( ' : ' ) [ - 1 ] . strip ( )
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +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
2018-11-29 13:03:44 +00:00
if os . name == ' nt ' :
out = out . decode ( encoding )
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +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 :
2018-11-29 13:03:44 +00:00
logging . info ( ' BAZARR Post-processing result for file ' + path + ' : ' + out )
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +00:00
# fixme: support multiple languages at once
2019-03-11 21:44:49 +00:00
if media_type == ' series ' :
reversed_path = path_replace_reverse ( path )
else :
reversed_path = path_replace_reverse_movie ( path )
return message , reversed_path , downloaded_language_code2 , downloaded_provider , subtitle . score
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +00:00
if not saved_any :
logging . debug ( ' BAZARR No subtitles were found for this file: ' + path )
return None
2018-10-19 03:33:01 +00:00
logging . debug ( ' BAZARR Ended searching subtitles for file: ' + path )
2017-10-16 23:27:19 +00:00
2018-10-31 19:34:40 +00:00
2018-11-29 11:20:39 +00:00
def manual_search ( path , language , hi , providers , providers_auth , sceneName , title , media_type ) :
2018-10-19 03:33:01 +00:00
logging . debug ( ' BAZARR Manually searching subtitles for this file: ' + path )
2019-02-05 03:57:15 +00:00
2018-11-29 11:02:15 +00:00
final_subtitles = [ ]
2019-01-15 16:25:13 +00:00
2018-09-10 21:10:33 +00:00
if hi == " True " :
hi = True
else :
hi = False
language_set = set ( )
for lang in ast . literal_eval ( language ) :
2018-10-12 03:44:34 +00:00
lang = alpha3_from_alpha2 ( lang )
if lang == ' pob ' :
2018-09-10 21:10:33 +00:00
language_set . add ( Language ( ' por ' , ' BR ' ) )
else :
2018-10-12 03:44:34 +00:00
language_set . add ( Language ( lang ) )
2019-01-15 16:25:13 +00:00
2018-12-27 19:19:59 +00:00
use_scenename = settings . general . getboolean ( ' use_scenename ' )
2019-01-02 19:19:31 +00:00
minimum_score = settings . general . minimum_score
minimum_score_movie = settings . general . minimum_score_movie
2018-12-27 19:19:59 +00:00
use_postprocessing = settings . general . getboolean ( ' use_postprocessing ' )
2018-12-15 00:36:28 +00:00
postprocessing_cmd = settings . general . postprocessing_cmd
2019-01-15 16:25:13 +00:00
2018-11-29 13:03:44 +00:00
video = get_video ( path , title , sceneName , use_scenename , providers = providers , 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-01-15 16:25:13 +00:00
2018-09-10 21:10:33 +00:00
try :
2019-01-24 14:00:03 +00:00
if providers :
subtitles = list_subtitles ( [ video ] , language_set ,
providers = providers ,
provider_configs = providers_auth ,
pool_class = provider_pool ( ) ,
throttle_callback = provider_throttle ,
language_hook = None ) # fixme
else :
subtitles = [ ]
logging . info ( " BAZARR All providers are throttled " )
2018-09-10 21:10:33 +00:00
except Exception as e :
2018-10-19 03:33:01 +00:00
logging . exception ( " BAZARR Error trying to get subtitle list from provider for this file: " + path )
2018-09-10 21:10:33 +00:00
else :
subtitles_list = [ ]
2019-01-15 16:25:13 +00:00
2018-11-29 11:02:15 +00:00
for s in subtitles [ video ] :
try :
matches = s . get_matches ( video )
except AttributeError :
continue
2019-01-15 16:25:13 +00:00
2018-11-29 11:02:15 +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
2018-11-29 11:02:15 +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 )
2018-09-10 21:10:33 +00:00
continue
2018-11-28 11:36:44 +00:00
2018-11-29 11:02:15 +00:00
score = compute_score ( matches , s , video , hearing_impaired = hi )
2019-01-15 12:26:26 +00:00
not_matched = scores - matches
2018-11-29 13:40:31 +00:00
s . score = score
2019-02-13 19:18:25 +00:00
# if score < min_score:
# continue
2019-01-15 16:25:13 +00:00
2018-10-31 19:34:40 +00:00
subtitles_list . append (
2018-11-29 11:02:15 +00:00
dict ( score = round ( ( score / max_score * 100 ) , 2 ) ,
2018-10-31 19:34:40 +00:00
language = alpha2_from_alpha3 ( s . language . alpha3 ) , hearing_impaired = str ( s . hearing_impaired ) ,
2018-11-29 11:02:15 +00:00
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 ) ) )
2019-01-15 16:25:13 +00:00
2018-11-29 11:02:15 +00:00
final_subtitles = sorted ( subtitles_list , key = lambda x : x [ ' score ' ] , reverse = True )
logging . debug ( ' BAZARR ' + str ( len ( final_subtitles ) ) + " subtitles have been found for this file: " + path )
2018-10-19 03:33:01 +00:00
logging . debug ( ' BAZARR Ended searching subtitles for this file: ' + path )
2018-11-29 11:02:15 +00:00
return final_subtitles
2018-10-31 19:34:40 +00:00
2018-09-10 21:10:33 +00:00
2018-11-29 13:40:31 +00:00
def manual_download_subtitle ( path , language , hi , subtitle , provider , providers_auth , sceneName , title , media_type ) :
2018-10-19 03:33:01 +00:00
logging . debug ( ' BAZARR Manually downloading subtitles for this file: ' + path )
2019-01-15 16:25:13 +00:00
2018-09-10 21:10:33 +00:00
subtitle = pickle . loads ( codecs . decode ( subtitle . encode ( ) , " base64 " ) )
2018-12-27 19:19:59 +00:00
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
2019-01-02 19:19:31 +00:00
single = settings . general . getboolean ( ' single_language ' )
2018-11-29 13:40:31 +00:00
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 )
2018-09-10 21:10:33 +00:00
try :
2019-01-24 14:00:03 +00:00
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
2018-09-10 21:10:33 +00:00
except Exception as e :
2018-10-19 03:33:01 +00:00
logging . exception ( ' BAZARR Error downloading subtitles for this file ' + path )
2018-09-10 21:10:33 +00:00
return None
else :
2019-01-15 12:20:39 +00:00
if not subtitle . is_valid ( ) :
2019-02-24 03:58:13 +00:00
notifications . write ( msg = ' No valid subtitles file found for this file: ' + path , queue = ' get_subtitle ' )
2019-01-27 14:17:23 +00:00
logging . exception ( ' BAZARR No valid subtitles file found for this file: ' + path )
2019-01-15 12:20:39 +00:00
return
2019-01-27 14:17:23 +00:00
logging . debug ( ' BAZARR Subtitles file downloaded for this file: ' + path )
2018-09-10 21:10:33 +00:00
try :
2018-11-29 13:40:31 +00:00
score = round ( subtitle . score / max_score * 100 , 2 )
2019-03-02 17:10:55 +00:00
fld = get_target_folder ( path )
2019-03-26 19:49:03 +00:00
chmod = int ( settings . general . chmod , 8 ) if not sys . platform . startswith ( ' win ' ) and settings . general . getboolean ( ' chmod_enabled ' ) else None
2019-01-16 14:34:33 +00:00
saved_subtitles = save_subtitles ( video . original_path , [ subtitle ] , single = single ,
2019-03-02 17:10:55 +00:00
tags = None , # fixme
directory = fld ,
chmod = chmod ,
# formats=("srt", "vtt")
2018-11-29 13:47:56 +00:00
path_decoder = force_unicode )
2019-01-15 16:25:13 +00:00
2018-09-10 21:10:33 +00:00
except Exception as e :
2018-10-19 03:33:01 +00:00
logging . exception ( ' BAZARR Error saving subtitles file to disk for this file: ' + path )
2018-11-29 13:40:31 +00:00
return
2018-09-10 21:10:33 +00:00
else :
2018-11-29 13:40:31 +00:00
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 )
2018-11-29 13:40:31 +00:00
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 ) + " % u sing manual search. "
2019-01-15 16:25:13 +00:00
2018-11-29 13:40:31 +00:00
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 ( )
2019-01-15 16:25:13 +00:00
2018-11-29 13:40:31 +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
2018-11-29 13:40:31 +00:00
if os . name == ' nt ' :
out = out . decode ( encoding )
2019-01-15 16:25:13 +00:00
2018-11-29 13:40:31 +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 :
2018-11-29 13:40:31 +00:00
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-03-17 18:57:23 +00:00
if media_type == ' series ' :
reversed_path = path_replace_reverse ( path )
else :
reversed_path = path_replace_reverse_movie ( path )
return message , reversed_path , downloaded_language_code2 , downloaded_provider , subtitle . score
2018-10-12 02:24:27 +00:00
else :
2018-10-31 19:34:40 +00:00
logging . error (
2019-01-24 14:00:03 +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
2018-10-19 03:33:01 +00:00
logging . debug ( ' BAZARR Ended manually downloading subtitles for file: ' + path )
2018-09-10 21:10:33 +00:00
2018-10-31 19:34:40 +00:00
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 ' ) :
2018-08-09 03:43:13 +00:00
monitored_only_query_string = ' AND monitored = " True " '
else :
monitored_only_query_string = " "
2019-01-15 16:25:13 +00:00
2018-10-31 19:34:40 +00:00
conn_db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2017-10-16 23:27:19 +00:00
c_db = conn_db . cursor ( )
2018-10-31 19:34:40 +00:00
episodes_details = c_db . execute (
' SELECT path, missing_subtitles, sonarrEpisodeId, scene_name FROM table_episodes WHERE sonarrSeriesId = ? AND missing_subtitles != " [] " ' + monitored_only_query_string ,
( no , ) ) . fetchall ( )
2019-01-15 16:25:13 +00:00
series_details = c_db . execute ( " SELECT hearing_impaired, title FROM table_shows WHERE sonarrSeriesId = ? " ,
( no , ) ) . fetchone ( )
2017-10-16 23:27:19 +00:00
c_db . close ( )
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-10-16 23:27:19 +00:00
for episode in episodes_details :
for language in ast . literal_eval ( episode [ 1 ] ) :
2018-08-09 03:43:13 +00:00
if language is not None :
2019-02-24 03:58:13 +00:00
notifications . write ( msg = ' Searching for ' + str ( language_from_alpha2 ( language ) ) + ' subtitles for this episode: ' + path_replace ( episode [ 0 ] ) , queue = ' get_subtitle ' )
2019-03-11 21:44:49 +00:00
result = download_subtitle ( path_replace ( episode [ 0 ] ) , str ( alpha3_from_alpha2 ( language ) ) ,
2018-10-31 19:34:40 +00:00
series_details [ 0 ] , providers_list , providers_auth , str ( episode [ 3 ] ) ,
2018-11-29 13:03:44 +00:00
series_details [ 1 ] , ' series ' )
2019-03-11 21:44:49 +00:00
if result is not None :
message = result [ 0 ]
path = result [ 1 ]
language_code = result [ 2 ]
provider = result [ 3 ]
score = result [ 4 ]
2018-08-09 03:43:13 +00:00
store_subtitles ( path_replace ( episode [ 0 ] ) )
2019-03-11 21:44:49 +00:00
history_log ( 1 , no , episode [ 2 ] , message , path , language_code , provider , score )
2018-08-09 03:43:13 +00:00
send_notifications ( no , episode [ 2 ] , message )
2017-11-16 18:42:23 +00:00
list_missing_subtitles ( no )
2017-10-16 23:27:19 +00:00
2019-02-24 03:58:13 +00:00
notifications . write ( msg = ' Searching completed. Please reload the page. ' , type = ' success ' , duration = ' permanent ' , button = ' refresh ' , queue = ' get_subtitle ' )
2019-02-21 04:30:25 +00:00
2018-04-24 14:48:52 +00:00
def movies_download_subtitles ( no ) :
2018-10-31 19:34:40 +00:00
conn_db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2018-04-24 14:48:52 +00:00
c_db = conn_db . cursor ( )
2018-10-31 19:34:40 +00:00
movie = c_db . execute (
2018-11-29 13:03:44 +00:00
" SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired, title FROM table_movies WHERE radarrId = ? " ,
2018-10-31 19:34:40 +00:00
( no , ) ) . fetchone ( )
2018-04-24 14:48:52 +00:00
c_db . close ( )
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
2018-04-24 14:48:52 +00:00
for language in ast . literal_eval ( movie [ 1 ] ) :
2018-08-09 03:43:13 +00:00
if language is not None :
2019-02-24 03:58:13 +00:00
notifications . write ( msg = ' Searching for ' + str ( language_from_alpha2 ( language ) ) + ' subtitles for this movie: ' + path_replace_movie ( movie [ 0 ] ) , queue = ' get_subtitle ' )
2019-03-11 21:44:49 +00:00
result = download_subtitle ( path_replace_movie ( movie [ 0 ] ) , str ( alpha3_from_alpha2 ( language ) ) , movie [ 4 ] ,
2018-11-29 13:03:44 +00:00
providers_list , providers_auth , str ( movie [ 3 ] ) , movie [ 5 ] , ' movie ' )
2019-03-11 21:44:49 +00:00
if result is not None :
message = result [ 0 ]
path = result [ 1 ]
language_code = result [ 2 ]
provider = result [ 3 ]
score = result [ 4 ]
2018-08-09 03:43:13 +00:00
store_subtitles_movie ( path_replace_movie ( movie [ 0 ] ) )
2019-03-11 21:44:49 +00:00
history_log_movie ( 1 , no , message , path , language_code , provider , score )
2018-08-09 03:43:13 +00:00
send_notifications_movie ( no , message )
2018-04-24 14:48:52 +00:00
list_missing_subtitles_movies ( no )
2019-02-24 03:58:13 +00:00
notifications . write ( msg = ' Searching completed. Please reload the page. ' , type = ' success ' , duration = ' permanent ' , button = ' refresh ' , queue = ' get_subtitle ' )
2019-02-21 04:30:25 +00:00
2018-04-24 14:48:52 +00:00
2017-10-16 23:27:19 +00:00
def wanted_download_subtitles ( path ) :
2018-10-31 19:34:40 +00:00
conn_db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2017-11-16 19:09:40 +00:00
c_db = conn_db . cursor ( )
2018-10-31 19:34:40 +00:00
episodes_details = c_db . execute (
2018-11-29 13:03:44 +00:00
" 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 FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ? AND missing_subtitles != ' [] ' " ,
2018-10-31 19:34:40 +00:00
( path_replace_reverse ( path ) , ) ) . fetchall ( )
2017-11-16 19:09:40 +00:00
c_db . close ( )
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 :
2018-08-27 00:53:02 +00:00
attempt = episode [ 6 ]
if type ( attempt ) == unicode :
attempt = ast . literal_eval ( attempt )
2017-11-16 19:09:40 +00:00
for language in ast . literal_eval ( episode [ 1 ] ) :
2018-08-27 00:53:02 +00:00
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
2018-10-31 19:34:40 +00:00
conn_db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2018-08-27 00:53:02 +00:00
c_db = conn_db . cursor ( )
2018-10-31 19:34:40 +00:00
c_db . execute ( ' UPDATE table_episodes SET failedAttempts = ? WHERE sonarrEpisodeId = ? ' ,
( unicode ( attempt ) , episode [ 2 ] ) )
2018-08-27 00:53:02 +00:00
conn_db . commit ( )
c_db . close ( )
2019-01-15 16:25:13 +00:00
2018-08-27 00:53:02 +00:00
for i in range ( len ( attempt ) ) :
if attempt [ i ] [ 0 ] == language :
2019-01-24 14:00:03 +00:00
if search_active ( attempt [ i ] [ 1 ] ) :
2019-02-21 04:30:25 +00:00
notifications . write (
2019-02-24 03:58:13 +00:00
msg = ' Searching ' + str ( language_from_alpha2 ( language ) ) + ' subtitles for this episode: ' + path , queue = ' get_subtitle ' )
2019-03-11 21:44:49 +00:00
result = download_subtitle ( path_replace ( episode [ 0 ] ) , str ( alpha3_from_alpha2 ( language ) ) ,
2018-10-31 19:34:40 +00:00
episode [ 4 ] , providers_list , providers_auth , str ( episode [ 5 ] ) ,
2018-11-29 13:03:44 +00:00
episode [ 7 ] , ' series ' )
2019-03-11 21:44:49 +00:00
if result is not None :
message = result [ 0 ]
path = result [ 1 ]
language_code = result [ 2 ]
provider = result [ 3 ]
score = result [ 4 ]
2018-08-27 00:53:02 +00:00
store_subtitles ( path_replace ( episode [ 0 ] ) )
list_missing_subtitles ( episode [ 3 ] )
2019-03-11 21:44:49 +00:00
history_log ( 1 , episode [ 3 ] , episode [ 2 ] , message , path , language_code , provider , score )
2018-08-27 00:53:02 +00:00
send_notifications ( episode [ 3 ] , episode [ 2 ] , message )
else :
2018-10-31 19:34:40 +00:00
logging . debug (
' BAZARR Search is not active for episode ' + episode [ 0 ] + ' Language: ' + attempt [ i ] [ 0 ] )
2017-11-16 17:04:20 +00:00
2018-06-06 00:06:00 +00:00
def wanted_download_subtitles_movie ( path ) :
2018-10-31 19:34:40 +00:00
conn_db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2018-06-06 00:06:00 +00:00
c_db = conn_db . cursor ( )
2018-10-31 19:34:40 +00:00
movies_details = c_db . execute (
2018-11-29 13:03:44 +00:00
" SELECT path, missing_subtitles, radarrId, radarrId, hearing_impaired, sceneName, failedAttempts, title FROM table_movies WHERE path = ? AND missing_subtitles != ' [] ' " ,
2018-10-31 19:34:40 +00:00
( path_replace_reverse_movie ( path ) , ) ) . fetchall ( )
2018-06-06 00:06:00 +00:00
c_db . close ( )
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
2018-06-06 00:06:00 +00:00
for movie in movies_details :
2018-08-27 00:53:02 +00:00
attempt = movie [ 6 ]
if type ( attempt ) == unicode :
attempt = ast . literal_eval ( attempt )
2018-06-06 00:06:00 +00:00
for language in ast . literal_eval ( movie [ 1 ] ) :
2018-08-27 00:53:02 +00:00
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
2018-10-31 19:34:40 +00:00
conn_db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2018-08-27 00:53:02 +00:00
c_db = conn_db . cursor ( )
c_db . execute ( ' UPDATE table_movies SET failedAttempts = ? WHERE radarrId = ? ' , ( unicode ( attempt ) , movie [ 2 ] ) )
conn_db . commit ( )
c_db . close ( )
2019-01-15 16:25:13 +00:00
2018-08-27 00:53:02 +00:00
for i in range ( len ( attempt ) ) :
if attempt [ i ] [ 0 ] == language :
if search_active ( attempt [ i ] [ 1 ] ) is True :
2019-02-21 04:30:25 +00:00
notifications . write (
2019-02-24 03:58:13 +00:00
msg = ' Searching ' + str ( language_from_alpha2 ( language ) ) + ' subtitles for this movie: ' + path , queue = ' get_subtitle ' )
2019-03-11 21:44:49 +00:00
result = download_subtitle ( path_replace_movie ( movie [ 0 ] ) , str ( alpha3_from_alpha2 ( language ) ) ,
2018-11-29 13:03:44 +00:00
movie [ 4 ] , providers_list , providers_auth , str ( movie [ 5 ] ) , movie [ 7 ] ,
' movie ' )
2019-03-11 21:44:49 +00:00
if result is not None :
message = result [ 0 ]
path = result [ 1 ]
language_code = result [ 2 ]
provider = result [ 3 ]
score = result [ 4 ]
2018-08-27 00:53:02 +00:00
store_subtitles_movie ( path_replace_movie ( movie [ 0 ] ) )
list_missing_subtitles_movies ( movie [ 3 ] )
2019-03-11 21:44:49 +00:00
history_log_movie ( 1 , movie [ 3 ] , message , path , language_code , provider , score )
2018-08-27 00:53:02 +00:00
send_notifications_movie ( movie [ 3 ] , message )
else :
2018-10-31 19:34:40 +00:00
logging . info (
' BAZARR Search is not active for movie ' + movie [ 0 ] + ' Language: ' + attempt [ i ] [ 0 ] )
2018-06-06 00:06:00 +00:00
2017-10-23 03:00:11 +00:00
def wanted_search_missing_subtitles ( ) :
2018-10-31 19:34:40 +00:00
db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
2017-10-23 03:00:11 +00:00
db . create_function ( " path_substitution " , 1 , path_replace )
2018-06-06 00:06:00 +00:00
db . create_function ( " path_substitution_movie " , 1 , path_replace_movie )
2017-10-23 03:00:11 +00:00
c = db . cursor ( )
2019-01-06 17:15:43 +00:00
if settings . sonarr . getboolean ( ' only_monitored ' ) :
2019-01-10 17:14:25 +00:00
monitored_only_query_string_sonarr = ' AND monitored = " True " '
2018-08-09 03:43:13 +00:00
else :
2019-01-10 17:14:25 +00:00
monitored_only_query_string_sonarr = " "
if settings . radarr . getboolean ( ' only_monitored ' ) :
monitored_only_query_string_radarr = ' AND monitored = " True " '
else :
monitored_only_query_string_radarr = " "
2018-08-09 03:43:13 +00:00
2018-10-31 19:34:40 +00:00
c . execute (
2019-01-15 14:45:38 +00:00
" SELECT path_substitution(path) FROM table_episodes WHERE missing_subtitles != ' [] ' " + monitored_only_query_string_sonarr )
2018-05-02 10:25:42 +00:00
episodes = c . fetchall ( )
2019-01-15 16:25:13 +00:00
2018-10-31 19:34:40 +00:00
c . execute (
2019-01-15 14:45:38 +00:00
" SELECT path_substitution_movie(path) FROM table_movies WHERE missing_subtitles != ' [] ' " + monitored_only_query_string_radarr )
2018-05-02 10:25:42 +00:00
movies = c . fetchall ( )
2019-01-15 16:25:13 +00:00
2017-10-23 03:00:11 +00:00
c . close ( )
2019-01-24 14:00:03 +00:00
providers = get_providers ( )
2018-12-27 19:19:59 +00:00
if settings . general . getboolean ( ' use_sonarr ' ) :
2019-01-24 14:00:03 +00:00
if providers :
for episode in episodes :
wanted_download_subtitles ( episode [ 0 ] )
else :
2019-03-05 14:32:10 +00:00
notifications . write ( msg = ' BAZARR All providers are throttled ' , queue = ' get_subtitle ' )
2019-01-24 14:00:03 +00:00
logging . info ( " BAZARR All providers are throttled " )
2019-01-15 16:25:13 +00:00
2018-12-27 19:19:59 +00:00
if settings . general . getboolean ( ' use_radarr ' ) :
2019-01-24 14:00:03 +00:00
if providers :
for movie in movies :
wanted_download_subtitles_movie ( movie [ 0 ] )
else :
2019-03-05 14:32:10 +00:00
notifications . write ( msg = ' BAZARR All providers are throttled ' , queue = ' get_subtitle ' )
2019-01-24 14:00:03 +00:00
logging . info ( " BAZARR All providers are throttled " )
2019-01-15 16:25:13 +00:00
2018-10-19 03:33:01 +00:00
logging . info ( ' BAZARR Finished searching for missing subtitles. Check histories for more information. ' )
2018-10-31 19:34:40 +00:00
2019-02-24 03:58:13 +00:00
notifications . write ( msg = ' Searching completed. Please reload the page. ' , type = ' success ' , duration = ' permanent ' , button = ' refresh ' , queue = ' get_subtitle ' )
2019-02-21 04:30:25 +00:00
2018-08-27 00:53:02 +00:00
def search_active ( timestamp ) :
2019-01-06 17:15:43 +00:00
if settings . general . getboolean ( ' adaptive_searching ' ) :
2018-08-27 00:53:02 +00:00
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
2019-02-05 03:57:15 +00:00
def refine_from_db ( path , video ) :
if isinstance ( video , Episode ) :
db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
c = db . cursor ( )
2019-03-13 17:28:59 +00:00
data = c . execute ( " SELECT table_shows.title, table_episodes.season, table_episodes.episode, table_episodes.title, table_shows.year, table_shows.tvdbId, table_shows.alternateTitles, table_episodes.format, table_episodes.resolution, table_episodes.video_codec, table_episodes.audio_codec FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ? " , ( unicode ( path_replace_reverse ( path ) ) , ) ) . fetchone ( )
2019-02-05 03:57:15 +00:00
db . close ( )
2019-02-08 03:34:54 +00:00
if data :
2019-03-20 21:20:10 +00:00
video . series , year , country = series_re . match ( data [ 0 ] ) . groups ( )
2019-02-05 03:57:15 +00:00
video . season = int ( data [ 1 ] )
video . episode = int ( data [ 2 ] )
video . title = data [ 3 ]
2019-03-13 17:28:59 +00:00
if data [ 4 ] :
if int ( data [ 4 ] ) > 0 : video . year = int ( data [ 4 ] )
2019-02-05 03:57:15 +00:00
video . series_tvdb_id = int ( data [ 5 ] )
video . alternative_series = ast . literal_eval ( data [ 6 ] )
2019-02-06 11:51:19 +00:00
if not video . format :
video . format = str ( data [ 7 ] )
2019-02-06 03:49:58 +00:00
if not video . resolution :
2019-02-06 11:51:19 +00:00
video . resolution = str ( data [ 8 ] )
2019-02-06 03:49:58 +00:00
if not video . video_codec :
2019-02-08 03:34:54 +00:00
if data [ 9 ] : video . video_codec = data [ 9 ]
2019-02-06 03:49:58 +00:00
if not video . audio_codec :
2019-02-06 11:51:19 +00:00
if data [ 10 ] : video . audio_codec = data [ 10 ]
2019-02-05 03:57:15 +00:00
elif isinstance ( video , Movie ) :
db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
c = db . cursor ( )
2019-03-13 17:28:59 +00:00
data = c . execute ( " SELECT title, year, alternativeTitles, format, resolution, video_codec, audio_codec, imdbId FROM table_movies WHERE path = ? " , ( unicode ( path_replace_reverse_movie ( path ) ) , ) ) . fetchone ( )
2019-02-05 03:57:15 +00:00
db . close ( )
2019-02-08 03:34:54 +00:00
if data :
2019-02-05 11:57:45 +00:00
video . title = re . sub ( r ' ( \ ( \ d \ d \ d \ d \ )) ' , ' ' , data [ 0 ] )
2019-03-13 17:28:59 +00:00
if data [ 1 ] :
if int ( data [ 1 ] ) > 0 : video . year = int ( data [ 1 ] )
2019-02-11 03:42:42 +00:00
if data [ 7 ] : video . imdb_id = data [ 7 ]
2019-02-05 03:57:15 +00:00
video . alternative_titles = ast . literal_eval ( data [ 2 ] )
2019-02-06 11:51:19 +00:00
if not video . format :
if data [ 3 ] : video . format = data [ 3 ]
2019-02-06 03:49:58 +00:00
if not video . resolution :
2019-02-06 11:51:19 +00:00
if data [ 4 ] : video . resolution = data [ 4 ]
2019-02-06 03:49:58 +00:00
if not video . video_codec :
2019-02-06 11:51:19 +00:00
if data [ 5 ] : video . video_codec = data [ 5 ]
2019-02-06 03:49:58 +00:00
if not video . audio_codec :
2019-02-06 11:51:19 +00:00
if data [ 6 ] : video . audio_codec = data [ 6 ]
2019-02-05 03:57:15 +00:00
return video
2019-03-15 18:28:57 +00:00
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-03-15 18:28:57 +00:00
2019-03-19 04:08:53 +00:00
if settings . general . getboolean ( ' upgrade_manual ' ) :
query_actions = [ 1 , 2 , 3 ]
else :
query_actions = [ 1 , 3 ]
2019-03-15 18:28:57 +00:00
db = sqlite3 . connect ( os . path . join ( args . config_dir , ' db ' , ' bazarr.db ' ) , timeout = 30 )
c = db . cursor ( )
2019-03-17 14:29:38 +00:00
episodes_list = c . 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 ,
2019-03-19 04:08:53 +00:00
MAX ( table_history . timestamp ) , table_shows . languages
2019-03-17 14:29:38 +00:00
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
2019-03-19 04:08:53 +00:00
WHERE action IN ( """ + ' , ' .join(map(str, query_actions)) + """ ) AND timestamp > ? AND
2019-03-20 03:44:50 +00:00
score is not null
2019-03-17 21:44:40 +00:00
GROUP BY table_history . video_path , table_history . language """ ,
( minimum_timestamp , ) ) . fetchall ( )
movies_list = c . execute ( """ SELECT table_history_movie.video_path, table_history_movie.language,
table_history_movie . score , table_movies . hearing_impaired , table_movies . sceneName ,
2019-03-19 04:08:53 +00:00
table_movies . title , table_movies . radarrId , MAX ( table_history_movie . timestamp ) ,
table_movies . languages
2019-03-17 21:44:40 +00:00
FROM table_history_movie
INNER JOIN table_movies on table_movies . radarrId = table_history_movie . radarrId
2019-03-19 04:08:53 +00:00
WHERE action IN ( """ + ' , ' .join(map(str, query_actions)) + """ ) AND timestamp > ? AND
2019-03-20 03:44:50 +00:00
score is not null
2019-03-17 21:44:40 +00:00
GROUP BY table_history_movie . video_path , table_history_movie . language """ ,
( minimum_timestamp , ) ) . fetchall ( )
2019-03-15 18:28:57 +00:00
db . close ( )
2019-03-16 19:30:06 +00:00
episodes_to_upgrade = [ ]
2019-03-17 14:29:38 +00:00
for episode in episodes_list :
2019-03-20 03:44:50 +00:00
if os . path . exists ( path_replace ( episode [ 0 ] ) ) and int ( episode [ 2 ] ) < 360 :
2019-03-16 19:30:06 +00:00
episodes_to_upgrade . append ( episode )
2019-03-17 21:44:40 +00:00
movies_to_upgrade = [ ]
for movie in movies_list :
2019-03-20 03:44:50 +00:00
if os . path . exists ( path_replace_movie ( movie [ 0 ] ) ) and int ( movie [ 2 ] ) < 120 :
2019-03-17 21:44:40 +00:00
movies_to_upgrade . append ( movie )
2019-03-16 19:30:06 +00:00
providers_list = get_providers ( )
providers_auth = get_providers_auth ( )
2019-04-04 13:29:08 +00:00
count_episode_to_upgrade = len ( episodes_to_upgrade )
count_movie_to_upgrade = len ( movies_to_upgrade )
2019-03-16 19:30:06 +00:00
2019-04-04 06:25:59 +00:00
for i , episode in enumerate ( episodes_to_upgrade , 1 ) :
2019-03-19 04:08:53 +00:00
if episode [ 1 ] in ast . literal_eval ( str ( episode [ 9 ] ) ) :
2019-04-04 13:29:08 +00:00
notifications . write ( msg = ' Upgrading series subtitles : ' + str ( i ) + ' / ' + str ( count_episode_to_upgrade ) ,
queue = ' get_subtitle ' , duration = ' long ' )
2019-03-19 04:08:53 +00:00
result = download_subtitle ( path_replace ( episode [ 0 ] ) , str ( alpha3_from_alpha2 ( episode [ 1 ] ) ) ,
episode [ 3 ] , providers_list , providers_auth , str ( episode [ 4 ] ) ,
2019-03-20 10:33:11 +00:00
episode [ 5 ] , ' series ' , forced_minimum_score = int ( episode [ 2 ] ) , is_upgrade = True )
2019-03-19 04:08:53 +00:00
if result is not None :
message = result [ 0 ]
path = result [ 1 ]
language_code = result [ 2 ]
provider = result [ 3 ]
score = result [ 4 ]
store_subtitles ( path_replace ( episode [ 0 ] ) )
history_log ( 3 , episode [ 6 ] , episode [ 7 ] , message , path , language_code , provider , score )
send_notifications ( episode [ 6 ] , episode [ 7 ] , message )
2019-03-17 21:44:40 +00:00
2019-04-04 06:25:59 +00:00
for i , movie in enumerate ( movies_to_upgrade , 1 ) :
2019-03-19 04:08:53 +00:00
if movie [ 1 ] in ast . literal_eval ( str ( movie [ 8 ] ) ) :
2019-04-04 13:29:08 +00:00
notifications . write ( msg = ' Upgrading movie subtitles : ' + str ( i ) + ' / ' + str ( count_movie_to_upgrade ) ,
queue = ' get_subtitle ' , duration = ' long ' )
2019-03-19 04:08:53 +00:00
result = download_subtitle ( path_replace_movie ( movie [ 0 ] ) , str ( alpha3_from_alpha2 ( movie [ 1 ] ) ) ,
movie [ 3 ] , providers_list , providers_auth , str ( movie [ 4 ] ) ,
2019-03-20 10:33:11 +00:00
movie [ 5 ] , ' movie ' , forced_minimum_score = int ( movie [ 2 ] ) , is_upgrade = True )
2019-03-19 04:08:53 +00:00
if result is not None :
message = result [ 0 ]
path = result [ 1 ]
language_code = result [ 2 ]
provider = result [ 3 ]
score = result [ 4 ]
store_subtitles_movie ( path_replace_movie ( movie [ 0 ] ) )
history_log_movie ( 3 , movie [ 6 ] , message , path , language_code , provider , score )
send_notifications_movie ( movie [ 6 ] , message )