2021-09-08 03:04:36 +00:00
import argparse , logging , os , sys , time
2021-02-28 03:56:49 +00:00
from datetime import datetime
2021-08-09 19:45:20 +00:00
from logging . handlers import RotatingFileHandler
2021-03-16 13:56:17 +00:00
try :
import schedule
2021-05-09 05:49:13 +00:00
from modules import util
2021-05-08 02:30:28 +00:00
from modules . builder import CollectionBuilder
2021-03-16 13:56:17 +00:00
from modules . config import Config
2021-05-08 02:30:28 +00:00
from modules . util import Failed
2021-03-16 13:56:17 +00:00
except ModuleNotFoundError :
2021-05-25 22:22:59 +00:00
print ( " Requirements Error: Requirements are not installed " )
sys . exit ( 0 )
if sys . version_info [ 0 ] != 3 or sys . version_info [ 1 ] < 6 :
print ( " Version Error: Version: %s . %s . %s incompatible please use Python 3.6+ " % ( sys . version_info [ 0 ] , sys . version_info [ 1 ] , sys . version_info [ 2 ] ) )
2021-03-16 13:56:17 +00:00
sys . exit ( 0 )
2021-01-20 21:37:59 +00:00
parser = argparse . ArgumentParser ( )
2021-04-22 19:24:09 +00:00
parser . add_argument ( " -db " , " --debug " , dest = " debug " , help = argparse . SUPPRESS , action = " store_true " , default = False )
2021-01-20 21:37:59 +00:00
parser . add_argument ( " -c " , " --config " , dest = " config " , help = " Run with desired *.yml file " , type = str )
2021-09-08 03:04:36 +00:00
parser . add_argument ( " -t " , " --time " , " --times " , dest = " times " , help = " Times to update each day use format HH:MM (Default: 03:00) (comma-separated list) " , default = " 03:00 " , type = str )
2021-03-31 04:20:20 +00:00
parser . add_argument ( " -re " , " --resume " , dest = " resume " , help = " Resume collection run from a specific collection " , type = str )
2021-01-20 21:37:59 +00:00
parser . add_argument ( " -r " , " --run " , dest = " run " , help = " Run without the scheduler " , action = " store_true " , default = False )
2021-02-21 08:13:07 +00:00
parser . add_argument ( " -rt " , " --test " , " --tests " , " --run-test " , " --run-tests " , dest = " test " , help = " Run in debug mode with only collections that have test: true " , action = " store_true " , default = False )
2021-05-18 21:56:57 +00:00
parser . add_argument ( " -co " , " --collection-only " , " --collections-only " , dest = " collection_only " , help = " Run only collection operations " , action = " store_true " , default = False )
parser . add_argument ( " -lo " , " --library-only " , " --libraries-only " , dest = " library_only " , help = " Run only library operations " , action = " store_true " , default = False )
parser . add_argument ( " -rc " , " -cl " , " --collection " , " --collections " , " --run-collection " , " --run-collections " , dest = " collections " , help = " Process only specified collections (comma-separated list) " , type = str )
parser . add_argument ( " -rl " , " -l " , " --library " , " --libraries " , " --run-library " , " --run-libraries " , dest = " libraries " , help = " Process only specified libraries (comma-separated list) " , type = str )
2021-05-26 06:03:00 +00:00
parser . add_argument ( " -nc " , " --no-countdown " , dest = " no_countdown " , help = " Run without displaying the countdown " , action = " store_true " , default = False )
2021-08-09 19:45:20 +00:00
parser . add_argument ( " -nm " , " --no-missing " , dest = " no_missing " , help = " Run without running the missing section " , action = " store_true " , default = False )
2021-01-20 21:37:59 +00:00
parser . add_argument ( " -d " , " --divider " , dest = " divider " , help = " Character that divides the sections (Default: ' = ' ) " , default = " = " , type = str )
parser . add_argument ( " -w " , " --width " , dest = " width " , help = " Screen Width (Default: 100) " , default = 100 , type = int )
args = parser . parse_args ( )
2021-09-08 03:04:36 +00:00
def get_arg ( env_str , default , arg_bool = False , arg_int = False ) :
2021-03-16 21:00:21 +00:00
env_var = os . environ . get ( env_str )
2021-09-08 03:04:36 +00:00
if env_var :
if arg_bool :
if env_var is True or env_var is False :
return env_var
elif env_var . lower ( ) in [ " t " , " true " ] :
return True
else :
return False
elif arg_int :
return int ( env_var )
2021-03-16 21:00:21 +00:00
else :
2021-09-08 03:04:36 +00:00
return str ( env_var )
2021-03-16 21:00:21 +00:00
else :
return default
2021-09-08 03:04:36 +00:00
test = get_arg ( " PMM_TEST " , args . test , arg_bool = True )
debug = get_arg ( " PMM_DEBUG " , args . debug , arg_bool = True )
run = get_arg ( " PMM_RUN " , args . run , arg_bool = True )
no_countdown = get_arg ( " PMM_NO_COUNTDOWN " , args . no_countdown , arg_bool = True )
no_missing = get_arg ( " PMM_NO_MISSING " , args . no_missing , arg_bool = True )
library_only = get_arg ( " PMM_LIBRARIES_ONLY " , args . library_only , arg_bool = True )
collection_only = get_arg ( " PMM_COLLECTIONS_ONLY " , args . collection_only , arg_bool = True )
collections = get_arg ( " PMM_COLLECTIONS " , args . collections )
libraries = get_arg ( " PMM_LIBRARIES " , args . libraries )
resume = get_arg ( " PMM_RESUME " , args . resume )
times = get_arg ( " PMM_TIME " , args . times )
divider = get_arg ( " PMM_DIVIDER " , args . divider )
screen_width = get_arg ( " PMM_WIDTH " , args . width )
config_file = get_arg ( " PMM_CONFIG " , args . config )
util . separating_character = divider [ 0 ]
if screen_width < 90 or screen_width > 300 :
print ( f " Argument Error: width argument invalid: { screen_width } must be an integer between 90 and 300 using the default 100 " )
screen_width = 100
util . screen_width = screen_width
2021-01-20 21:37:59 +00:00
default_dir = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , " config " )
2021-09-08 03:04:36 +00:00
if config_file and os . path . exists ( config_file ) :
default_dir = os . path . join ( os . path . dirname ( os . path . abspath ( config_file ) ) )
elif config_file and not os . path . exists ( config_file ) :
print ( f " Config Error: config not found at { os . path . abspath ( config_file ) } " )
sys . exit ( 0 )
elif not os . path . exists ( os . path . join ( default_dir , " config.yml " ) ) :
print ( f " Config Error: config not found at { os . path . abspath ( default_dir ) } " )
sys . exit ( 0 )
2021-01-20 21:37:59 +00:00
os . makedirs ( os . path . join ( default_dir , " logs " ) , exist_ok = True )
logger = logging . getLogger ( " Plex Meta Manager " )
logger . setLevel ( logging . DEBUG )
def fmt_filter ( record ) :
2021-02-24 06:44:06 +00:00
record . levelname = f " [ { record . levelname } ] "
record . filename = f " [ { record . filename } : { record . lineno } ] "
2021-01-20 21:37:59 +00:00
return True
cmd_handler = logging . StreamHandler ( )
2021-05-09 05:49:13 +00:00
cmd_handler . setLevel ( logging . DEBUG if test or debug else logging . INFO )
2021-01-20 21:37:59 +00:00
logger . addHandler ( cmd_handler )
sys . excepthook = util . my_except_hook
2021-05-25 22:22:59 +00:00
def start ( config_path , is_test = False , time_scheduled = None , requested_collections = None , requested_libraries = None , resume_from = None ) :
2021-05-19 21:30:20 +00:00
file_logger = os . path . join ( default_dir , " logs " , " meta.log " )
should_roll_over = os . path . isfile ( file_logger )
2021-08-09 19:45:20 +00:00
file_handler = RotatingFileHandler ( file_logger , delay = True , mode = " w " , backupCount = 10 , encoding = " utf-8 " )
2021-05-19 21:30:20 +00:00
util . apply_formatter ( file_handler )
file_handler . addFilter ( fmt_filter )
if should_roll_over :
file_handler . doRollover ( )
logger . addHandler ( file_handler )
util . separator ( )
2021-05-24 03:38:46 +00:00
logger . info ( util . centered ( " " ) )
logger . info ( util . centered ( " ____ _ __ __ _ __ __ " ) )
logger . info ( util . centered ( " | _ \\ | | _____ __ | \\ / | ___| |_ __ _ | \\ / | __ _ _ __ __ _ __ _ ___ _ __ " ) )
logger . info ( util . centered ( " | |_) | |/ _ \\ \\ / / | | \\ /| |/ _ \\ __/ _` | | | \\ /| |/ _` | ' _ \\ / _` |/ _` |/ _ \\ ' __| " ) )
logger . info ( util . centered ( " | __/| | __/> < | | | | __/ || (_| | | | | | (_| | | | | (_| | (_| | __/ | " ) )
logger . info ( util . centered ( " |_| |_| \\ ___/_/ \\ _ \\ |_| |_| \\ ___| \\ __ \\ __,_| |_| |_| \\ __,_|_| |_| \\ __,_| \\ __, | \\ ___|_| " ) )
logger . info ( util . centered ( " |___/ " ) )
2021-09-30 14:22:03 +00:00
logger . info ( util . centered ( " Version: 1.12.2-develop0930 " ) )
2021-05-25 22:22:59 +00:00
if time_scheduled : start_type = f " { time_scheduled } "
2021-04-22 18:07:37 +00:00
elif is_test : start_type = " Test "
2021-05-08 02:30:28 +00:00
elif requested_collections : start_type = " Collections "
elif requested_libraries : start_type = " Libraries "
2021-04-22 18:07:37 +00:00
else : start_type = " "
2021-02-28 03:56:49 +00:00
start_time = datetime . now ( )
2021-05-25 22:22:59 +00:00
if time_scheduled is None :
time_scheduled = start_time . strftime ( " % H: % M " )
2021-02-24 06:44:06 +00:00
util . separator ( f " Starting { start_type } Run " )
2021-01-20 21:37:59 +00:00
try :
2021-05-25 22:22:59 +00:00
config = Config ( default_dir , config_path = config_path , is_test = is_test ,
time_scheduled = time_scheduled , requested_collections = requested_collections ,
requested_libraries = requested_libraries , resume_from = resume_from )
update_libraries ( config )
2021-01-20 21:37:59 +00:00
except Exception as e :
util . print_stacktrace ( )
2021-08-17 00:07:35 +00:00
util . print_multiline ( e , critical = True )
2021-01-20 21:37:59 +00:00
logger . info ( " " )
2021-02-28 03:56:49 +00:00
util . separator ( f " Finished { start_type } Run \n Run Time: { str ( datetime . now ( ) - start_time ) . split ( ' . ' ) [ 0 ] } " )
2021-05-20 20:38:48 +00:00
logger . removeHandler ( file_handler )
2021-01-20 21:37:59 +00:00
2021-05-25 22:22:59 +00:00
def update_libraries ( config ) :
2021-05-08 02:30:28 +00:00
for library in config . libraries :
2021-05-19 21:30:20 +00:00
os . makedirs ( os . path . join ( default_dir , " logs " , library . mapping_name , " collections " ) , exist_ok = True )
col_file_logger = os . path . join ( default_dir , " logs " , library . mapping_name , " library.log " )
should_roll_over = os . path . isfile ( col_file_logger )
2021-08-09 19:45:20 +00:00
library_handler = RotatingFileHandler ( col_file_logger , delay = True , mode = " w " , backupCount = 3 , encoding = " utf-8 " )
2021-05-19 21:30:20 +00:00
util . apply_formatter ( library_handler )
if should_roll_over :
library_handler . doRollover ( )
logger . addHandler ( library_handler )
2021-05-08 02:30:28 +00:00
os . environ [ " PLEXAPI_PLEXAPI_TIMEOUT " ] = str ( library . timeout )
logger . info ( " " )
util . separator ( f " { library . name } Library " )
2021-08-24 04:33:04 +00:00
items = None
if not library . is_other :
logger . info ( " " )
util . separator ( f " Mapping { library . name } Library " , space = False , border = False )
logger . info ( " " )
items = library . map_guids ( )
2021-05-25 22:22:59 +00:00
if not config . test_mode and not config . resume_from and not collection_only and library . mass_update :
2021-08-24 04:33:04 +00:00
mass_metadata ( config , library , items = items )
2021-05-08 02:30:28 +00:00
for metadata in library . metadata_files :
logger . info ( " " )
util . separator ( f " Running Metadata File \n { metadata . path } " )
2021-05-25 22:22:59 +00:00
if not config . test_mode and not config . resume_from and not collection_only :
2021-05-08 02:30:28 +00:00
try :
2021-08-05 14:59:45 +00:00
metadata . update_metadata ( )
2021-05-08 02:30:28 +00:00
except Failed as e :
logger . error ( e )
2021-05-25 22:22:59 +00:00
collections_to_run = metadata . get_collections ( config . requested_collections )
if config . resume_from and config . resume_from not in collections_to_run :
2021-05-24 03:38:46 +00:00
logger . info ( " " )
2021-05-25 22:22:59 +00:00
logger . warning ( f " Collection: { config . resume_from } not in Metadata File: { metadata . path } " )
2021-05-08 02:30:28 +00:00
continue
2021-05-18 17:41:22 +00:00
if collections_to_run and not library_only :
2021-05-24 03:38:46 +00:00
logger . info ( " " )
2021-05-25 22:22:59 +00:00
util . separator ( f " { ' Test ' if config . test_mode else ' ' } Collections " )
2021-05-19 21:30:20 +00:00
logger . removeHandler ( library_handler )
2021-05-26 14:45:33 +00:00
run_collection ( config , library , metadata , collections_to_run )
2021-05-19 21:30:20 +00:00
logger . addHandler ( library_handler )
2021-08-12 20:37:03 +00:00
if library . run_sort :
logger . info ( " " )
util . separator ( f " Sorting { library . name } Library ' s Collections " , space = False , border = False )
logger . info ( " " )
for builder in library . run_sort :
logger . info ( " " )
util . separator ( f " Sorting { builder . name } Collection " , space = False , border = False )
logger . info ( " " )
builder . sort_collection ( )
2021-05-08 02:30:28 +00:00
2021-05-25 22:22:59 +00:00
if not config . test_mode and not config . requested_collections and ( ( library . show_unmanaged and not library_only ) or ( library . assets_for_all and not collection_only ) ) :
2021-05-24 03:38:46 +00:00
logger . info ( " " )
util . separator ( f " Other { library . name } Library Operations " )
2021-05-19 19:21:22 +00:00
unmanaged_collections = [ ]
2021-05-08 02:30:28 +00:00
for col in library . get_all_collections ( ) :
2021-05-19 19:21:22 +00:00
if col . title not in library . collections :
unmanaged_collections . append ( col )
if library . show_unmanaged and not library_only :
logger . info ( " " )
2021-05-24 03:38:46 +00:00
util . separator ( f " Unmanaged Collections in { library . name } Library " , space = False , border = False )
2021-05-19 19:21:22 +00:00
logger . info ( " " )
for col in unmanaged_collections :
2021-05-08 02:30:28 +00:00
logger . info ( col . title )
2021-05-24 03:38:46 +00:00
logger . info ( " " )
2021-05-19 19:21:22 +00:00
logger . info ( f " { len ( unmanaged_collections ) } Unmanaged Collections " )
if library . assets_for_all and not collection_only :
logger . info ( " " )
2021-08-24 04:33:04 +00:00
util . separator ( f " All { library . type } s Assets Check for { library . name } Library " , space = False , border = False )
2021-05-19 19:21:22 +00:00
logger . info ( " " )
for col in unmanaged_collections :
2021-08-02 19:00:15 +00:00
poster , background = library . find_collection_assets ( col , create = library . create_asset_folders )
2021-06-22 20:28:12 +00:00
library . upload_images ( col , poster = poster , background = background )
2021-05-19 19:21:22 +00:00
for item in library . get_all ( ) :
2021-08-02 19:00:15 +00:00
library . update_item_from_assets ( item , create = library . create_asset_folders )
2021-05-19 19:21:22 +00:00
2021-05-19 21:30:20 +00:00
logger . removeHandler ( library_handler )
2021-05-08 02:30:28 +00:00
has_run_again = False
for library in config . libraries :
if library . run_again :
has_run_again = True
break
2021-05-18 17:41:22 +00:00
if has_run_again and not library_only :
2021-05-08 02:30:28 +00:00
logger . info ( " " )
util . separator ( " Run Again " )
logger . info ( " " )
for x in range ( 1 , config . general [ " run_again_delay " ] + 1 ) :
2021-05-26 13:25:32 +00:00
util . print_return ( f " Waiting to run again in { config . general [ ' run_again_delay ' ] - x + 1 } minutes " )
2021-05-08 02:30:28 +00:00
for y in range ( 60 ) :
time . sleep ( 1 )
2021-05-26 13:25:32 +00:00
util . print_end ( )
2021-05-08 02:30:28 +00:00
for library in config . libraries :
if library . run_again :
2021-05-19 21:30:20 +00:00
col_file_logger = os . path . join ( default_dir , " logs " , library . mapping_name , f " library.log " )
2021-08-09 19:45:20 +00:00
library_handler = RotatingFileHandler ( col_file_logger , mode = " w " , backupCount = 3 , encoding = " utf-8 " )
2021-05-19 21:30:20 +00:00
util . apply_formatter ( library_handler )
logger . addHandler ( library_handler )
library_handler . addFilter ( fmt_filter )
2021-05-08 02:30:28 +00:00
os . environ [ " PLEXAPI_PLEXAPI_TIMEOUT " ] = str ( library . timeout )
logger . info ( " " )
util . separator ( f " { library . name } Library Run Again " )
logger . info ( " " )
2021-06-02 15:18:37 +00:00
library . map_guids ( )
2021-05-08 02:30:28 +00:00
for builder in library . run_again :
logger . info ( " " )
util . separator ( f " { builder . name } Collection " )
logger . info ( " " )
try :
2021-05-26 14:45:33 +00:00
builder . run_collections_again ( )
2021-05-08 02:30:28 +00:00
except Failed as e :
2021-05-12 14:25:48 +00:00
util . print_stacktrace ( )
2021-05-08 02:30:28 +00:00
util . print_multiline ( e , error = True )
2021-05-19 21:30:20 +00:00
logger . removeHandler ( library_handler )
2021-05-08 02:30:28 +00:00
2021-05-12 23:19:39 +00:00
used_url = [ ]
for library in config . libraries :
if library . url not in used_url :
used_url . append ( library . url )
if library . empty_trash :
library . query ( library . PlexServer . library . emptyTrash )
2021-05-14 02:45:36 +00:00
if library . clean_bundles :
library . query ( library . PlexServer . library . cleanBundles )
2021-05-12 23:19:39 +00:00
if library . optimize :
library . query ( library . PlexServer . library . optimize )
2021-08-24 04:33:04 +00:00
def mass_metadata ( config , library , items = None ) :
2021-05-08 02:30:28 +00:00
logger . info ( " " )
2021-08-24 04:33:04 +00:00
util . separator ( f " Mass Editing { library . type } Library: { library . name } " )
2021-05-08 02:30:28 +00:00
logger . info ( " " )
2021-08-24 04:33:04 +00:00
if items is None :
items = library . get_all ( )
2021-07-03 02:14:36 +00:00
if library . split_duplicates :
items = library . search ( * * { " duplicate " : True } )
for item in items :
item . split ( )
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | Splitting " ) )
2021-05-20 03:36:57 +00:00
radarr_adds = [ ]
sonarr_adds = [ ]
2021-08-02 21:01:19 +00:00
trakt_ratings = config . Trakt . user_ratings ( library . is_movie ) if library . mass_trakt_rating_update else [ ]
2021-05-08 02:30:28 +00:00
for i , item in enumerate ( items , 1 ) :
2021-08-12 20:36:38 +00:00
try :
library . reload ( item )
except Failed as e :
logger . error ( e )
continue
2021-05-26 13:25:32 +00:00
util . print_return ( f " Processing: { i } / { len ( items ) } { item . title } " )
2021-05-09 05:38:41 +00:00
tmdb_id = None
tvdb_id = None
imdb_id = None
2021-05-08 02:30:28 +00:00
if config . Cache :
2021-08-07 06:01:21 +00:00
t_id , i_id , guid_media_type , _ = config . Cache . query_guid_map ( item . guid )
2021-05-09 05:38:41 +00:00
if t_id :
if " movie " in guid_media_type :
2021-08-05 15:18:00 +00:00
tmdb_id = t_id [ 0 ]
2021-05-09 05:38:41 +00:00
else :
2021-08-05 15:18:00 +00:00
tvdb_id = t_id [ 0 ]
2021-08-07 06:01:21 +00:00
if i_id :
imdb_id = i_id [ 0 ]
2021-05-09 05:38:41 +00:00
if not tmdb_id and not tvdb_id :
2021-05-26 14:45:33 +00:00
tmdb_id = library . get_tmdb_from_map ( item )
2021-05-09 05:38:41 +00:00
if not tmdb_id and not tvdb_id and library . is_show :
2021-08-10 15:18:43 +00:00
tvdb_id = library . get_tvdb_from_map ( item )
2021-05-20 03:36:57 +00:00
2021-08-05 18:57:50 +00:00
if library . mass_trakt_rating_update :
try :
if library . is_movie and tmdb_id in trakt_ratings :
new_rating = trakt_ratings [ tmdb_id ]
elif library . is_show and tvdb_id in trakt_ratings :
new_rating = trakt_ratings [ tvdb_id ]
else :
raise Failed
if str ( item . userRating ) != str ( new_rating ) :
library . edit_query ( item , { " userRating.value " : new_rating , " userRating.locked " : 1 } )
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | User Rating | { new_rating } " ) )
except Failed :
pass
2021-05-20 03:36:57 +00:00
if library . Radarr and library . radarr_add_all and tmdb_id :
radarr_adds . append ( tmdb_id )
if library . Sonarr and library . sonarr_add_all and tvdb_id :
sonarr_adds . append ( tvdb_id )
2021-05-09 05:38:41 +00:00
tmdb_item = None
2021-05-15 02:16:33 +00:00
if library . mass_genre_update == " tmdb " or library . mass_audience_rating_update == " tmdb " or library . mass_critic_rating_update == " tmdb " :
2021-05-20 03:36:57 +00:00
if tvdb_id and not tmdb_id :
tmdb_id = config . Convert . tvdb_to_tmdb ( tvdb_id )
2021-05-09 05:38:41 +00:00
if tmdb_id :
2021-05-08 02:30:28 +00:00
try :
2021-05-09 05:38:41 +00:00
tmdb_item = config . TMDb . get_movie ( tmdb_id ) if library . is_movie else config . TMDb . get_show ( tmdb_id )
2021-05-08 02:30:28 +00:00
except Failed as e :
2021-08-04 14:20:52 +00:00
logger . error ( util . adjust_space ( str ( e ) ) )
2021-05-08 02:30:28 +00:00
else :
2021-05-26 13:25:32 +00:00
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | No TMDb ID for Guid: { item . guid } " ) )
2021-05-09 05:38:41 +00:00
omdb_item = None
2021-05-15 02:16:33 +00:00
if library . mass_genre_update in [ " omdb " , " imdb " ] or library . mass_audience_rating_update in [ " omdb " , " imdb " ] or library . mass_critic_rating_update in [ " omdb " , " imdb " ] :
2021-05-09 05:38:41 +00:00
if config . OMDb . limit is False :
2021-05-20 03:36:57 +00:00
if tmdb_id and not imdb_id :
imdb_id = config . Convert . tmdb_to_imdb ( tmdb_id )
elif tvdb_id and not imdb_id :
imdb_id = config . Convert . tvdb_to_imdb ( tvdb_id )
2021-05-09 05:38:41 +00:00
if imdb_id :
try :
omdb_item = config . OMDb . get_omdb ( imdb_id )
except Failed as e :
2021-08-04 14:20:52 +00:00
logger . error ( util . adjust_space ( str ( e ) ) )
2021-06-22 20:28:12 +00:00
except Exception :
logger . error ( f " IMDb ID: { imdb_id } " )
raise
2021-05-09 05:38:41 +00:00
else :
2021-05-26 13:25:32 +00:00
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | No IMDb ID for Guid: { item . guid } " ) )
2021-05-09 05:38:41 +00:00
2021-08-10 15:18:43 +00:00
tvdb_item = None
if library . mass_genre_update == " tvdb " :
if tvdb_id :
try :
tvdb_item = config . TVDb . get_item ( tvdb_id , library . is_movie )
except Failed as e :
logger . error ( util . adjust_space ( str ( e ) ) )
else :
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | No TVDb ID for Guid: { item . guid } " ) )
if not tmdb_item and not omdb_item and not tvdb_item :
2021-05-09 05:38:41 +00:00
continue
if library . mass_genre_update :
try :
if tmdb_item and library . mass_genre_update == " tmdb " :
new_genres = [ genre . name for genre in tmdb_item . genres ]
elif omdb_item and library . mass_genre_update in [ " omdb " , " imdb " ] :
new_genres = omdb_item . genres
2021-08-10 15:18:43 +00:00
elif tvdb_item and library . mass_genre_update == " tvdb " :
new_genres = tvdb_item . genres
2021-05-09 05:38:41 +00:00
else :
raise Failed
2021-08-20 13:13:54 +00:00
library . edit_tags ( " genre " , item , sync_tags = new_genres )
2021-05-09 05:38:41 +00:00
except Failed :
pass
2021-08-02 21:01:19 +00:00
if library . mass_audience_rating_update :
2021-05-09 05:38:41 +00:00
try :
2021-08-02 21:01:19 +00:00
if tmdb_item and library . mass_audience_rating_update == " tmdb " :
2021-05-09 05:38:41 +00:00
new_rating = tmdb_item . vote_average
2021-08-02 21:01:19 +00:00
elif omdb_item and library . mass_audience_rating_update in [ " omdb " , " imdb " ] :
2021-05-09 05:38:41 +00:00
new_rating = omdb_item . imdb_rating
else :
raise Failed
if new_rating is None :
2021-05-26 13:25:32 +00:00
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | No Rating Found " ) )
2021-05-15 02:16:33 +00:00
else :
if library . mass_audience_rating_update and str ( item . audienceRating ) != str ( new_rating ) :
library . edit_query ( item , { " audienceRating.value " : new_rating , " audienceRating.locked " : 1 } )
2021-05-26 13:25:32 +00:00
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | Audience Rating | { new_rating } " ) )
2021-08-02 21:01:19 +00:00
except Failed :
pass
if library . mass_critic_rating_update :
try :
if tmdb_item and library . mass_critic_rating_update == " tmdb " :
new_rating = tmdb_item . vote_average
elif omdb_item and library . mass_critic_rating_update in [ " omdb " , " imdb " ] :
new_rating = omdb_item . imdb_rating
else :
raise Failed
if new_rating is None :
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | No Rating Found " ) )
else :
2021-05-15 02:16:33 +00:00
if library . mass_critic_rating_update and str ( item . rating ) != str ( new_rating ) :
library . edit_query ( item , { " rating.value " : new_rating , " rating.locked " : 1 } )
2021-05-26 13:25:32 +00:00
logger . info ( util . adjust_space ( f " { item . title [ : 25 ] : <25 } | Critic Rating | { new_rating } " ) )
2021-05-09 05:38:41 +00:00
except Failed :
pass
2021-05-08 02:30:28 +00:00
2021-05-20 03:36:57 +00:00
if library . Radarr and library . radarr_add_all :
try :
library . Radarr . add_tmdb ( radarr_adds )
except Failed as e :
logger . error ( e )
if library . Sonarr and library . sonarr_add_all :
try :
library . Sonarr . add_tvdb ( sonarr_adds )
except Failed as e :
logger . error ( e )
2021-05-26 14:45:33 +00:00
def run_collection ( config , library , metadata , requested_collections ) :
2021-05-19 17:12:34 +00:00
logger . info ( " " )
2021-05-26 04:35:54 +00:00
for mapping_name , collection_attrs in requested_collections . items ( ) :
2021-05-20 20:38:48 +00:00
collection_start = datetime . now ( )
2021-05-25 22:22:59 +00:00
if config . test_mode and ( " test " not in collection_attrs or collection_attrs [ " test " ] is not True ) :
2021-05-12 14:25:48 +00:00
no_template_test = True
if " template " in collection_attrs and collection_attrs [ " template " ] :
for data_template in util . get_list ( collection_attrs [ " template " ] , split = False ) :
if " name " in data_template \
and data_template [ " name " ] \
and metadata . templates \
and data_template [ " name " ] in metadata . templates \
and metadata . templates [ data_template [ " name " ] ] \
and " test " in metadata . templates [ data_template [ " name " ] ] \
and metadata . templates [ data_template [ " name " ] ] [ " test " ] is True :
no_template_test = False
if no_template_test :
continue
2021-05-25 22:22:59 +00:00
if config . resume_from and config . resume_from != mapping_name :
2021-05-19 17:12:34 +00:00
continue
2021-05-25 22:22:59 +00:00
elif config . resume_from == mapping_name :
config . resume_from = None
2021-05-12 14:25:48 +00:00
logger . info ( " " )
2021-05-19 17:12:34 +00:00
util . separator ( f " Resuming Collections " )
2021-05-19 21:30:20 +00:00
if " name_mapping " in collection_attrs and collection_attrs [ " name_mapping " ] :
2021-05-20 20:38:48 +00:00
collection_log_name , output_str = util . validate_filename ( collection_attrs [ " name_mapping " ] )
2021-05-19 21:30:20 +00:00
else :
2021-05-20 20:38:48 +00:00
collection_log_name , output_str = util . validate_filename ( mapping_name )
2021-05-19 21:30:20 +00:00
collection_log_folder = os . path . join ( default_dir , " logs " , library . mapping_name , " collections " , collection_log_name )
os . makedirs ( collection_log_folder , exist_ok = True )
col_file_logger = os . path . join ( collection_log_folder , f " collection.log " )
should_roll_over = os . path . isfile ( col_file_logger )
2021-08-09 19:45:20 +00:00
collection_handler = RotatingFileHandler ( col_file_logger , delay = True , mode = " w " , backupCount = 3 , encoding = " utf-8 " )
2021-05-19 21:30:20 +00:00
util . apply_formatter ( collection_handler )
if should_roll_over :
collection_handler . doRollover ( )
logger . addHandler ( collection_handler )
2021-05-19 17:12:34 +00:00
try :
2021-05-12 14:25:48 +00:00
util . separator ( f " { mapping_name } Collection " )
logger . info ( " " )
2021-05-20 20:38:48 +00:00
if output_str :
logger . info ( output_str )
logger . info ( " " )
2021-05-12 14:25:48 +00:00
2021-05-24 03:38:46 +00:00
util . separator ( f " Validating { mapping_name } Attributes " , space = False , border = False )
2021-08-16 05:41:04 +00:00
builder = CollectionBuilder ( config , library , metadata , mapping_name , no_missing , collection_attrs )
2021-05-24 03:38:46 +00:00
logger . info ( " " )
util . separator ( f " Building { mapping_name } Collection " , space = False , border = False )
2021-05-12 14:25:48 +00:00
if len ( builder . schedule ) > 0 :
util . print_multiline ( builder . schedule , info = True )
2021-05-24 03:38:46 +00:00
if len ( builder . smart_filter_details ) > 0 :
2021-05-29 02:33:55 +00:00
logger . info ( " " )
2021-05-24 03:38:46 +00:00
util . print_multiline ( builder . smart_filter_details , info = True )
2021-05-19 17:12:34 +00:00
if not builder . smart_url :
2021-05-12 14:25:48 +00:00
logger . info ( " " )
2021-05-19 17:12:34 +00:00
logger . info ( f " Sync Mode: { ' sync ' if builder . sync else ' append ' } " )
if len ( builder . filters ) > 0 :
logger . info ( " " )
for filter_key , filter_value in builder . filters :
logger . info ( f " Collection Filter { filter_key } : { filter_value } " )
2021-05-12 14:25:48 +00:00
2021-08-16 05:41:04 +00:00
builder . find_rating_keys ( )
2021-05-24 03:38:46 +00:00
2021-09-13 01:37:56 +00:00
if len ( builder . rating_keys ) > = builder . minimum and builder . build_collection :
2021-05-24 03:38:46 +00:00
logger . info ( " " )
util . separator ( f " Adding to { mapping_name } Collection " , space = False , border = False )
logger . info ( " " )
2021-05-26 14:45:33 +00:00
builder . add_to_collection ( )
2021-09-13 01:37:56 +00:00
elif len ( builder . rating_keys ) < builder . minimum and builder . build_collection :
logger . info ( " " )
2021-09-15 03:16:59 +00:00
logger . info ( f " Collection Minimum: { builder . minimum } not met for { mapping_name } Collection " )
if builder . details [ " delete_below_minimum " ] and builder . obj :
2021-09-13 01:37:56 +00:00
builder . delete_collection ( )
2021-09-15 03:16:59 +00:00
logger . info ( " " )
2021-09-13 01:37:56 +00:00
logger . info ( f " Collection { builder . obj . title } deleted " )
2021-08-16 05:41:04 +00:00
if builder . do_missing and ( len ( builder . missing_movies ) > 0 or len ( builder . missing_shows ) > 0 ) :
2021-07-08 18:26:31 +00:00
if builder . details [ " show_missing " ] is True :
logger . info ( " " )
util . separator ( f " Missing from Library " , space = False , border = False )
logger . info ( " " )
2021-05-14 19:22:59 +00:00
builder . run_missing ( )
2021-05-19 20:42:05 +00:00
if builder . sync and len ( builder . rating_keys ) > 0 and builder . build_collection :
2021-05-12 14:25:48 +00:00
builder . sync_collection ( )
2021-09-15 03:16:59 +00:00
run_item_details = True
2021-05-19 20:42:05 +00:00
if builder . build_collection :
2021-09-15 03:16:59 +00:00
try :
builder . load_collection ( )
except Failed :
run_item_details = False
logger . info ( " " )
util . separator ( " No Collection to Update " , space = False , border = False )
else :
builder . update_details ( )
if builder . custom_sort :
library . run_sort . append ( builder )
2021-09-21 02:24:07 +00:00
# builder.sort_collection()
2021-07-29 13:36:30 +00:00
2021-09-15 03:16:59 +00:00
if builder . item_details and run_item_details :
try :
builder . load_collection_items ( )
except Failed :
logger . info ( " " )
util . separator ( " No Items Found " , space = False , border = False )
else :
builder . update_item_details ( )
2021-05-26 19:14:51 +00:00
2021-05-14 19:22:59 +00:00
if builder . run_again and ( len ( builder . run_again_movies ) > 0 or len ( builder . run_again_shows ) > 0 ) :
2021-05-12 14:25:48 +00:00
library . run_again . append ( builder )
except Failed as e :
2021-05-08 02:30:28 +00:00
util . print_stacktrace ( )
2021-05-12 14:25:48 +00:00
util . print_multiline ( e , error = True )
except Exception as e :
util . print_stacktrace ( )
logger . error ( f " Unknown Error: { e } " )
2021-05-19 17:12:34 +00:00
logger . info ( " " )
2021-05-20 20:38:48 +00:00
util . separator ( f " Finished { mapping_name } Collection \n Collection Run Time: { str ( datetime . now ( ) - collection_start ) . split ( ' . ' ) [ 0 ] } " )
2021-05-19 21:30:20 +00:00
logger . removeHandler ( collection_handler )
2021-05-08 02:30:28 +00:00
2021-01-20 21:37:59 +00:00
try :
2021-04-22 19:24:09 +00:00
if run or test or collections or libraries or resume :
2021-05-25 22:22:59 +00:00
start ( config_file , is_test = test , requested_collections = collections , requested_libraries = libraries , resume_from = resume )
2021-01-20 21:37:59 +00:00
else :
2021-09-08 03:04:36 +00:00
times_to_run = util . get_list ( times )
valid_times = [ ]
2021-05-25 22:22:59 +00:00
for time_to_run in times_to_run :
2021-09-08 03:04:36 +00:00
try :
valid_times . append ( datetime . strftime ( datetime . strptime ( time_to_run , " % H: % M " ) , " % H: % M " ) )
except ValueError :
if time_to_run :
raise Failed ( f " Argument Error: time argument invalid: { time_to_run } must be in the HH:MM format " )
else :
raise Failed ( f " Argument Error: blank time argument " )
for time_to_run in valid_times :
2021-05-25 22:22:59 +00:00
schedule . every ( ) . day . at ( time_to_run ) . do ( start , config_file , time_scheduled = time_to_run )
2021-01-20 21:37:59 +00:00
while True :
schedule . run_pending ( )
2021-05-24 22:16:19 +00:00
if not no_countdown :
current = datetime . now ( ) . strftime ( " % H: % M " )
2021-05-25 22:22:59 +00:00
seconds = None
og_time_str = " "
2021-09-08 03:04:36 +00:00
for time_to_run in valid_times :
2021-05-25 22:22:59 +00:00
new_seconds = ( datetime . strptime ( time_to_run , " % H: % M " ) - datetime . strptime ( current , " % H: % M " ) ) . total_seconds ( )
if new_seconds < 0 :
new_seconds + = 86400
if ( seconds is None or new_seconds < seconds ) and new_seconds > 0 :
seconds = new_seconds
og_time_str = time_to_run
2021-09-22 12:43:21 +00:00
if seconds is not None :
hours = int ( seconds / / 3600 )
minutes = int ( ( seconds % 3600 ) / / 60 )
time_str = f " { hours } Hour { ' s ' if hours > 1 else ' ' } and " if hours > 0 else " "
time_str + = f " { minutes } Minute { ' s ' if minutes > 1 else ' ' } "
util . print_return ( f " Current Time: { current } | { time_str } until the next run at { og_time_str } | Runs: { ' , ' . join ( times_to_run ) } " )
else :
logger . error ( f " Time Error: { valid_times } " )
2021-05-25 22:22:59 +00:00
time . sleep ( 60 )
2021-01-20 21:37:59 +00:00
except KeyboardInterrupt :
2021-02-24 06:42:58 +00:00
util . separator ( " Exiting Plex Meta Manager " )