2022-06-07 20:42:17 +00:00
import argparse , os , sys , time , uuid
from concurrent . futures import ProcessPoolExecutor
2022-10-14 21:50:51 +00:00
from datetime import datetime , timedelta
2022-09-27 06:19:29 +00:00
from modules . logs import MyLogger
2021-12-17 14:24:46 +00:00
2022-10-15 19:20:04 +00:00
urls_expire_after = {
' *.imdb.com ' : 60 * 60 * 6 ,
' www.omdbapi.com/ ' : 60 * 60 * 24 ,
' ergast.com/api/f1/ ' : 60 * 60 * 24 ,
' anidb.net ' : 60 * 60 * 24 ,
' api.anidb.net:9001/httpapi ' : 60 * 60 * 24 ,
' graphql.anilist.co ' : 60 * 60 * 24 ,
' raw.githubusercontent.com/meisnate12/Plex-Meta-Manager-Anime-IDs/master/pmm_anime_ids.json ' : 60 * 60 * 24 ,
' flixpatrol.com ' : 60 * 60 * 24 ,
' www.themoviedb.org ' : 60 * 60 * 24 ,
' api.github.com/repos/meisnate12/Plex-Meta-Manager ' : 60 * 60 * 24 ,
' raw.githubusercontent.com/meisnate12/Plex-Meta-Manager-Configs ' : 60 * 60 * 24 ,
' www.icheckmovies.com/lists/ ' : 60 * 60 * 24 ,
' letterboxd.com ' : 60 * 60 * 24 ,
' api.myanimelist.net/v2/ ' : 60 * 60 * 24 ,
' api.jikan.moe/v4/ ' : 60 * 60 * 24 ,
' api.trakt.tv ' : 60 * 60 * 24 ,
' www.thetvdb.com ' : 60 * 60 * 6 ,
' thetvdb.com ' : 60 * 60 * 6 ,
' s3.amazonaws.com/popular-movies/movies.json ' : 60 * 60 * 24 ,
' mdblist.com/lists ' : 60 * 60 * 12 ,
' mdblist.com/api ' : 60 * 60 * 1 ,
' api.github.com/repos/meisnate12/Plex-Meta-Manager ' : 60 * 60 * 1 ,
' raw.githubusercontent.com/meisnate12/ ' : 60 * 60 * 1 ,
' notifiarr.com/api/v1 ' : 0
' dev.notifiarr.com/api/v1 ' : 0
' discord.com ' : 0
' hooks.slack.com ' : 0
' raw.githubusercontent.com/meisnate12/Plex-Meta-Manager/**/VERSION ' : 0
' * ' : 60 * 60 * 24 ,
}
2022-10-14 21:49:17 +00:00
from requests_cache import install_cache
install_cache (
' config/http_cache ' ,
expire_after = timedelta ( days = 1 ) , # Otherwise expire responses after one day
allowable_methods = [ ' GET ' , ' POST ' ] , # Cache POST requests to avoid sending the same data twice
allowable_codes = [ 200 ] ,
match_headers = True ,
2022-10-15 19:20:04 +00:00
urls_expire_after = urls_expire_after ,
2022-10-14 21:49:17 +00:00
)
2021-03-16 13:56:17 +00:00
try :
2022-03-15 06:47:50 +00:00
import plexapi , requests , schedule
2022-07-10 08:04:06 +00:00
from PIL import ImageFile
2022-06-30 13:14:09 +00:00
from plexapi import server
2022-01-26 14:27:22 +00:00
from plexapi . exceptions import NotFound
from plexapi . video import Show , Season
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 )
2022-04-22 15:24:44 +00:00
if sys . version_info [ 0 ] != 3 or sys . version_info [ 1 ] < 7 :
print ( " Version Error: Version: %s . %s . %s incompatible please use Python 3.7+ " % ( 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-11-03 14:38:43 +00:00
parser . add_argument ( " -tr " , " --trace " , dest = " trace " , 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 )
2022-06-07 20:42:17 +00:00
parser . add_argument ( " -t " , " --time " , " --times " , dest = " times " , help = " Times to update each day use format HH:MM (Default: 05:00) (comma-separated list) " , default = " 05:00 " , type = str )
2022-08-01 20:26:10 +00:00
parser . add_argument ( " -ti " , " --timeout " , dest = " timeout " , help = " PMM Global Timeout (Default: 180) " , default = 180 , type = int )
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 )
2022-01-02 04:23:47 +00:00
parser . add_argument ( " -is " , " --ignore-schedules " , dest = " ignore_schedules " , help = " Run ignoring collection schedules " , action = " store_true " , default = False )
2022-02-13 16:33:57 +00:00
parser . add_argument ( " -ig " , " --ignore-ghost " , dest = " ignore_ghost " , help = " Run ignoring ghost logging " , 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 )
2022-05-11 04:37:42 +00:00
parser . add_argument ( " -po " , " --playlist-only " , " --playlists-only " , dest = " playlist_only " , help = " Run only playlist operations " , action = " store_true " , default = False )
2022-04-18 18:16:39 +00:00
parser . add_argument ( " -op " , " --operation " , " --operations " , " -lo " , " --library-only " , " --libraries-only " , " --operation-only " , " --operations-only " , dest = " operations " , help = " Run only operations " , action = " store_true " , default = False )
parser . add_argument ( " -ov " , " --overlay " , " --overlays " , " --overlay-only " , " --overlays-only " , dest = " overlays " , help = " Run only overlays " , action = " store_true " , default = False )
2022-01-09 00:45:42 +00:00
parser . add_argument ( " -lf " , " --library-first " , " --libraries-first " , dest = " library_first " , help = " Run library operations before collections " , action = " store_true " , default = False )
2021-05-18 21:56:57 +00:00
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 )
2022-01-25 07:45:31 +00:00
parser . add_argument ( " -rm " , " -m " , " --metadata " , " --metadata-files " , " --run-metadata-files " , dest = " metadata " , help = " Process only specified Metadata files (comma-separated list) " , type = str )
2022-05-10 15:47:13 +00:00
parser . add_argument ( " -ca " , " --cache-library " , " --cache-libraries " , dest = " cache_libraries " , help = " Cache Library load for 1 day " , action = " store_true " , default = False )
2021-12-31 04:34:04 +00:00
parser . add_argument ( " -dc " , " --delete " , " --delete-collections " , dest = " delete " , help = " Deletes all Collections in the Plex Library before running " , action = " store_true " , default = False )
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-12-26 15:48:52 +00:00
parser . add_argument ( " -ro " , " --read-only-config " , dest = " read_only_config " , help = " Run without writing to the config " , 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 ) :
2022-04-18 18:16:39 +00:00
env_vars = [ env_str ] if not isinstance ( env_str , list ) else env_str
final_value = None
for env_var in env_vars :
env_value = os . environ . get ( env_var )
if env_value is not None :
final_value = env_value
break
if final_value is not None :
2021-09-08 03:04:36 +00:00
if arg_bool :
2022-04-18 18:16:39 +00:00
if final_value is True or final_value is False :
return final_value
elif final_value . lower ( ) in [ " t " , " true " ] :
2021-09-08 03:04:36 +00:00
return True
else :
return False
elif arg_int :
2022-04-18 18:16:39 +00:00
return int ( final_value )
2021-03-16 21:00:21 +00:00
else :
2022-04-18 18:16:39 +00:00
return str ( final_value )
2021-03-16 21:00:21 +00:00
else :
return default
2021-12-06 22:30:38 +00:00
config_file = get_arg ( " PMM_CONFIG " , args . config )
times = get_arg ( " PMM_TIME " , args . times )
2021-09-08 03:04:36 +00:00
run = get_arg ( " PMM_RUN " , args . run , arg_bool = True )
2021-12-06 22:30:38 +00:00
test = get_arg ( " PMM_TEST " , args . test , arg_bool = True )
2022-01-02 04:23:47 +00:00
ignore_schedules = get_arg ( " PMM_IGNORE_SCHEDULES " , args . ignore_schedules , arg_bool = True )
2022-02-13 16:33:57 +00:00
ignore_ghost = get_arg ( " PMM_IGNORE_GHOST " , args . ignore_ghost , arg_bool = True )
2021-09-08 03:04:36 +00:00
collection_only = get_arg ( " PMM_COLLECTIONS_ONLY " , args . collection_only , arg_bool = True )
2022-05-11 04:37:42 +00:00
playlist_only = get_arg ( " PMM_PLAYLISTS_ONLY " , args . playlist_only , arg_bool = True )
2022-04-18 18:16:39 +00:00
operations_only = get_arg ( [ " PMM_OPERATIONS " , " PMM_LIBRARIES_ONLY " ] , args . operations , arg_bool = True )
overlays_only = get_arg ( [ " PMM_OVERLAYS " , " PMM_OVERLAYS_ONLY " ] , args . overlays , arg_bool = True )
2022-01-04 16:46:56 +00:00
library_first = get_arg ( " PMM_LIBRARIES_FIRST " , args . library_first , arg_bool = True )
2021-09-08 03:04:36 +00:00
collections = get_arg ( " PMM_COLLECTIONS " , args . collections )
libraries = get_arg ( " PMM_LIBRARIES " , args . libraries )
2022-01-25 07:45:31 +00:00
metadata_files = get_arg ( " PMM_METADATA_FILES " , args . metadata )
2022-05-10 15:47:13 +00:00
cache_libraries = get_arg ( " PMM_CACHE_LIBRARIES " , args . cache_libraries , arg_bool = True )
2021-12-31 04:34:04 +00:00
delete = get_arg ( " PMM_DELETE_COLLECTIONS " , args . delete , arg_bool = True )
2021-09-08 03:04:36 +00:00
resume = get_arg ( " PMM_RESUME " , args . resume )
2021-12-06 22:30:38 +00:00
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 )
2021-12-24 00:16:46 +00:00
read_only_config = get_arg ( " PMM_READ_ONLY_CONFIG " , args . read_only_config , arg_bool = True )
2021-09-08 03:04:36 +00:00
divider = get_arg ( " PMM_DIVIDER " , args . divider )
2021-11-17 14:20:04 +00:00
screen_width = get_arg ( " PMM_WIDTH " , args . width , arg_int = True )
2022-08-01 20:26:10 +00:00
timeout = get_arg ( " PMM_TIMEOUT " , args . timeout , arg_int = True )
2021-12-06 22:30:38 +00:00
debug = get_arg ( " PMM_DEBUG " , args . debug , arg_bool = True )
trace = get_arg ( " PMM_TRACE " , args . trace , arg_bool = True )
2021-09-08 03:04:36 +00:00
2022-06-27 19:09:16 +00:00
if collections :
2022-04-25 20:18:04 +00:00
collection_only = True
2021-09-08 03:04:36 +00:00
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
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
2022-09-07 19:32:52 +00:00
logger = MyLogger ( " Plex Meta Manager " , default_dir , screen_width , divider [ 0 ] , ignore_ghost , test or debug , trace )
2021-01-20 21:37:59 +00:00
2022-02-13 16:33:57 +00:00
from modules import util
util . logger = logger
from modules . builder import CollectionBuilder
from modules . config import ConfigFile
2022-07-19 13:25:48 +00:00
from modules . util import Failed , NonExisting , NotScheduled , Deleted
2021-01-20 21:37:59 +00:00
2022-02-13 16:33:57 +00:00
def my_except_hook ( exctype , value , tb ) :
2022-06-07 20:42:17 +00:00
if issubclass ( exctype , KeyboardInterrupt ) :
sys . __excepthook__ ( exctype , value , tb )
else :
logger . critical ( " Uncaught Exception " , exc_info = ( exctype , value , tb ) )
2021-01-20 21:37:59 +00:00
2022-02-13 16:33:57 +00:00
sys . excepthook = my_except_hook
2021-01-20 21:37:59 +00:00
2022-08-01 20:26:10 +00:00
old_send = requests . Session . send
def new_send ( * send_args , * * kwargs ) :
if kwargs . get ( " timeout " , None ) is None :
kwargs [ " timeout " ] = timeout
return old_send ( * send_args , * * kwargs )
requests . Session . send = new_send
2022-03-16 21:01:39 +00:00
version = ( " Unknown " , " Unknown " , 0 )
2021-10-21 16:00:00 +00:00
with open ( os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , " VERSION " ) ) as handle :
2021-10-19 04:23:52 +00:00
for line in handle . readlines ( ) :
line = line . strip ( )
if len ( line ) > 0 :
2022-03-18 13:00:42 +00:00
version = util . parse_version ( line )
2021-10-19 04:23:52 +00:00
break
2022-04-21 04:39:38 +00:00
uuid_file = os . path . join ( default_dir , " UUID " )
uuid_num = None
if os . path . exists ( uuid_file ) :
with open ( uuid_file ) as handle :
for line in handle . readlines ( ) :
line = line . strip ( )
if len ( line ) > 0 :
uuid_num = line
break
if not uuid_num :
uuid_num = uuid . uuid4 ( )
with open ( uuid_file , " w " ) as handle :
handle . write ( str ( uuid_num ) )
plexapi . BASE_HEADERS [ " X-Plex-Client-Identifier " ] = str ( uuid_num )
2022-07-10 08:04:06 +00:00
ImageFile . LOAD_TRUNCATED_IMAGES = True
2022-02-08 06:19:35 +00:00
2022-06-07 20:42:17 +00:00
def process ( attrs ) :
with ProcessPoolExecutor ( max_workers = 1 ) as executor :
executor . submit ( start , * [ attrs ] )
2021-10-04 17:51:32 +00:00
def start ( attrs ) :
2022-02-13 16:33:57 +00:00
logger . add_main_handler ( )
logger . separator ( )
2021-10-19 04:23:52 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . info_center ( " ____ _ __ __ _ __ __ " )
logger . info_center ( " | _ \\ | | _____ __ | \\ / | ___| |_ __ _ | \\ / | __ _ _ __ __ _ __ _ ___ _ __ " )
logger . info_center ( " | |_) | |/ _ \\ \\ / / | | \\ /| |/ _ \\ __/ _` | | | \\ /| |/ _` | ' _ \\ / _` |/ _` |/ _ \\ ' __| " )
logger . info_center ( " | __/| | __/> < | | | | __/ || (_| | | | | | (_| | | | | (_| | (_| | __/ | " )
logger . info_center ( " |_| |_| \\ ___/_/ \\ _ \\ |_| |_| \\ ___| \\ __ \\ __,_| |_| |_| \\ __,_|_| |_| \\ __,_| \\ __, | \\ ___|_| " )
logger . info_center ( " |___/ " )
2022-03-16 21:01:39 +00:00
logger . info ( f " Version: { version [ 0 ] } " )
2022-04-22 15:24:44 +00:00
latest_version = util . current_version ( version )
2022-03-18 13:00:42 +00:00
new_version = latest_version [ 0 ] if latest_version and ( version [ 1 ] != latest_version [ 1 ] or ( version [ 2 ] and version [ 2 ] < latest_version [ 2 ] ) ) else None
2022-03-16 21:01:39 +00:00
if new_version :
logger . info ( f " Newest Version: { new_version } " )
2021-10-07 15:17:20 +00:00
if " time " in attrs and attrs [ " time " ] : start_type = f " { attrs [ ' time ' ] } "
elif " test " in attrs and attrs [ " test " ] : start_type = " Test "
elif " collections " in attrs and attrs [ " collections " ] : start_type = " Collections "
elif " libraries " in attrs and attrs [ " libraries " ] : start_type = " Libraries "
else : start_type = " "
2021-02-28 03:56:49 +00:00
start_time = datetime . now ( )
2021-10-04 17:51:32 +00:00
if " time " not in attrs :
attrs [ " time " ] = start_time . strftime ( " % H: % M " )
2021-12-01 05:38:20 +00:00
attrs [ " time_obj " ] = start_time
2022-03-16 21:01:39 +00:00
attrs [ " read_only " ] = read_only_config
2022-03-18 13:00:42 +00:00
attrs [ " version " ] = version
2022-04-13 04:30:59 +00:00
attrs [ " no_missing " ] = no_missing
2022-04-26 19:56:46 +00:00
attrs [ " collection_only " ] = collection_only
2022-05-11 04:37:42 +00:00
attrs [ " playlist_only " ] = playlist_only
2022-04-26 19:56:46 +00:00
attrs [ " operations_only " ] = operations_only
attrs [ " overlays_only " ] = overlays_only
2022-02-13 16:33:57 +00:00
logger . separator ( debug = True )
2021-12-06 22:30:38 +00:00
logger . debug ( f " --config (PMM_CONFIG): { config_file } " )
logger . debug ( f " --time (PMM_TIME): { times } " )
logger . debug ( f " --run (PMM_RUN): { run } " )
logger . debug ( f " --run-tests (PMM_TEST): { test } " )
logger . debug ( f " --collections-only (PMM_COLLECTIONS_ONLY): { collection_only } " )
2022-05-11 04:37:42 +00:00
logger . debug ( f " --playlists-only (PMM_PLAYLISTS_ONLY): { playlist_only } " )
2022-04-18 18:16:39 +00:00
logger . debug ( f " --operations (PMM_OPERATIONS): { operations_only } " )
logger . debug ( f " --overlays (PMM_OVERLAYS): { overlays_only } " )
2022-01-04 16:46:56 +00:00
logger . debug ( f " --libraries-first (PMM_LIBRARIES_FIRST): { library_first } " )
2021-12-06 22:30:38 +00:00
logger . debug ( f " --run-collections (PMM_COLLECTIONS): { collections } " )
logger . debug ( f " --run-libraries (PMM_LIBRARIES): { libraries } " )
2022-01-25 07:45:31 +00:00
logger . debug ( f " --run-metadata-files (PMM_METADATA_FILES): { metadata_files } " )
2022-01-02 04:23:47 +00:00
logger . debug ( f " --ignore-schedules (PMM_IGNORE_SCHEDULES): { ignore_schedules } " )
2022-02-13 16:33:57 +00:00
logger . debug ( f " --ignore-ghost (PMM_IGNORE_GHOST): { ignore_ghost } " )
2022-05-10 18:50:19 +00:00
logger . debug ( f " --cache-libraries (PMM_CACHE_LIBRARIES): { cache_libraries } " )
2021-12-31 04:34:04 +00:00
logger . debug ( f " --delete-collections (PMM_DELETE_COLLECTIONS): { delete } " )
2021-12-06 22:30:38 +00:00
logger . debug ( f " --resume (PMM_RESUME): { resume } " )
logger . debug ( f " --no-countdown (PMM_NO_COUNTDOWN): { no_countdown } " )
logger . debug ( f " --no-missing (PMM_NO_MISSING): { no_missing } " )
2021-12-24 00:16:46 +00:00
logger . debug ( f " --read-only-config (PMM_READ_ONLY_CONFIG): { read_only_config } " )
2021-12-06 22:30:38 +00:00
logger . debug ( f " --divider (PMM_DIVIDER): { divider } " )
logger . debug ( f " --width (PMM_WIDTH): { screen_width } " )
logger . debug ( f " --debug (PMM_DEBUG): { debug } " )
logger . debug ( f " --trace (PMM_TRACE): { trace } " )
logger . debug ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Starting { start_type } Run " )
2021-11-03 14:36:11 +00:00
config = None
2022-05-28 05:32:02 +00:00
stats = { " created " : 0 , " modified " : 0 , " deleted " : 0 , " added " : 0 , " unchanged " : 0 , " removed " : 0 , " radarr " : 0 , " sonarr " : 0 , " names " : [ ] }
2021-01-20 21:37:59 +00:00
try :
2022-03-16 21:01:39 +00:00
config = ConfigFile ( default_dir , attrs )
2021-01-20 21:37:59 +00:00
except Exception as e :
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
logger . critical ( e )
2021-10-04 17:51:32 +00:00
else :
try :
2022-05-28 05:32:02 +00:00
stats = run_config ( config , stats )
2021-10-04 17:51:32 +00:00
except Exception as e :
config . notify ( e )
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
logger . critical ( e )
2021-01-20 21:37:59 +00:00
logger . info ( " " )
2021-12-01 05:38:20 +00:00
end_time = datetime . now ( )
2022-03-25 20:13:51 +00:00
run_time = str ( end_time - start_time ) . split ( " . " ) [ 0 ]
2021-11-03 14:36:11 +00:00
if config :
2021-11-28 07:14:35 +00:00
try :
2022-03-18 13:00:42 +00:00
config . Webhooks . end_time_hooks ( start_time , end_time , run_time , stats )
2021-11-28 07:14:35 +00:00
except Failed as e :
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
2021-11-28 07:14:35 +00:00
logger . error ( f " Webhooks Error: { e } " )
2022-03-16 21:01:39 +00:00
version_line = f " Version: { version [ 0 ] } "
if new_version :
version_line = f " { version_line } Newest Version: { new_version } "
2022-03-15 06:47:50 +00:00
logger . separator ( f " Finished { start_type } Run \n { version_line } \n Finished: { end_time . strftime ( ' % H: % M: % S % Y- % m- %d ' ) } Run Time: { run_time } " )
2022-02-13 16:33:57 +00:00
logger . remove_main_handler ( )
2021-01-20 21:37:59 +00:00
2022-05-28 05:32:02 +00:00
def run_config ( config , stats ) :
2022-05-22 17:26:54 +00:00
library_status = run_libraries ( config )
2022-05-11 04:37:42 +00:00
playlist_status = { }
playlist_stats = { }
2022-07-14 13:46:33 +00:00
if ( config . playlist_files or config . general [ " playlist_report " ] ) and not overlays_only and not operations_only and not collection_only and not config . requested_metadata_files :
2022-05-11 04:37:42 +00:00
logger . add_playlists_handler ( )
if config . playlist_files :
playlist_status , playlist_stats = run_playlists ( config )
if config . general [ " playlist_report " ] :
ran = [ ]
for library in config . libraries :
if library . PlexServer . machineIdentifier in ran :
continue
ran . append ( library . PlexServer . machineIdentifier )
logger . info ( " " )
logger . separator ( f " { library . PlexServer . friendlyName } Playlist Report " )
logger . info ( " " )
report = library . playlist_report ( )
max_length = 0
for playlist_name in report :
if len ( playlist_name ) > max_length :
max_length = len ( playlist_name )
logger . info ( f " { ' Playlist Title ' : < { max_length } } | Users " )
logger . separator ( f " { logger . separating_character * max_length } | " , space = False , border = False , side_space = False , left = True )
for playlist_name , users in report . items ( ) :
logger . info ( f " { playlist_name : < { max_length } } | { ' all ' if len ( users ) == len ( library . users ) + 1 else ' , ' . join ( users ) } " )
logger . remove_playlists_handler ( )
amount_added = 0
if not operations_only and not overlays_only and not playlist_only :
has_run_again = False
for library in config . libraries :
if library . run_again :
has_run_again = True
break
if has_run_again :
logger . info ( " " )
logger . separator ( " Run Again " )
logger . info ( " " )
for x in range ( 1 , config . general [ " run_again_delay " ] + 1 ) :
logger . ghost ( f " Waiting to run again in { config . general [ ' run_again_delay ' ] - x + 1 } minutes " )
for y in range ( 60 ) :
time . sleep ( 1 )
logger . exorcise ( )
for library in config . libraries :
if library . run_again :
try :
logger . re_add_library_handler ( library . mapping_name )
os . environ [ " PLEXAPI_PLEXAPI_TIMEOUT " ] = str ( library . timeout )
logger . info ( " " )
logger . separator ( f " { library . name } Library Run Again " )
logger . info ( " " )
2022-06-01 20:15:15 +00:00
library . map_guids ( library . cache_items ( ) )
2022-05-11 04:37:42 +00:00
for builder in library . run_again :
logger . info ( " " )
logger . separator ( f " { builder . name } Collection in { library . name } " )
logger . info ( " " )
try :
amount_added + = builder . run_collections_again ( )
except Failed as e :
library . notify ( e , collection = builder . name , critical = False )
logger . stacktrace ( )
logger . error ( e )
logger . remove_library_handler ( library . mapping_name )
except Exception as e :
library . notify ( e )
logger . stacktrace ( )
logger . critical ( e )
if not collection_only and not overlays_only and not playlist_only :
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 )
if library . clean_bundles :
library . query ( library . PlexServer . library . cleanBundles )
if library . optimize :
library . query ( library . PlexServer . library . optimize )
longest = 20
for library in config . libraries :
for title in library . status :
if len ( title ) > longest :
longest = len ( title )
if playlist_status :
for title in playlist_status :
if len ( title ) > longest :
longest = len ( title )
def print_status ( status ) :
logger . info ( f " { ' Title ' : ^ { longest } } | + | = | - | Run Time | { ' Status ' } " )
breaker = f " { logger . separating_character * longest } | { logger . separating_character * 7 } | { logger . separating_character * 7 } | { logger . separating_character * 7 } | { logger . separating_character * 10 } | "
logger . separator ( breaker , space = False , border = False , side_space = False , left = True )
for name , data in status . items ( ) :
logger . info ( f " { name : < { longest } } | { data [ ' added ' ] : >5 } | { data [ ' unchanged ' ] : >5 } | { data [ ' removed ' ] : >5 } | { data [ ' run_time ' ] : >8 } | { data [ ' status ' ] } " )
if data [ " errors " ] :
for error in data [ " errors " ] :
logger . info ( error )
logger . info ( " " )
logger . info ( " " )
logger . separator ( " Summary " )
for library in config . libraries :
logger . info ( " " )
logger . separator ( f " { library . name } Summary " , space = False , border = False )
logger . info ( " " )
logger . info ( f " { ' Title ' : <27 } | Run Time | " )
logger . info ( f " { logger . separating_character * 27 } | { logger . separating_character * 8 } | " )
2022-05-17 07:25:11 +00:00
if library . name in library_status :
for text , value in library_status [ library . name ] . items ( ) :
logger . info ( f " { text : <27 } | { value : >8 } | " )
2022-05-11 04:37:42 +00:00
logger . info ( " " )
print_status ( library . status )
if playlist_status :
logger . info ( " " )
logger . separator ( f " Playlists Summary " , space = False , border = False )
logger . info ( " " )
print_status ( playlist_status )
stats [ " added " ] + = amount_added
for library in config . libraries :
stats [ " created " ] + = library . stats [ " created " ]
stats [ " modified " ] + = library . stats [ " modified " ]
stats [ " deleted " ] + = library . stats [ " deleted " ]
stats [ " added " ] + = library . stats [ " added " ]
stats [ " unchanged " ] + = library . stats [ " unchanged " ]
stats [ " removed " ] + = library . stats [ " removed " ]
stats [ " radarr " ] + = library . stats [ " radarr " ]
stats [ " sonarr " ] + = library . stats [ " sonarr " ]
stats [ " names " ] . extend ( [ { " name " : n , " library " : library . name } for n in library . stats [ " names " ] ] )
if playlist_stats :
stats [ " created " ] + = playlist_stats [ " created " ]
stats [ " modified " ] + = playlist_stats [ " modified " ]
stats [ " deleted " ] + = playlist_stats [ " deleted " ]
stats [ " added " ] + = playlist_stats [ " added " ]
stats [ " unchanged " ] + = playlist_stats [ " unchanged " ]
stats [ " removed " ] + = playlist_stats [ " removed " ]
stats [ " radarr " ] + = playlist_stats [ " radarr " ]
stats [ " sonarr " ] + = playlist_stats [ " sonarr " ]
stats [ " names " ] . extend ( [ { " name " : n , " library " : " PLAYLIST " } for n in playlist_stats [ " names " ] ] )
return stats
def run_libraries ( config ) :
2022-05-05 13:21:17 +00:00
library_status = { }
2021-05-08 02:30:28 +00:00
for library in config . libraries :
2021-12-18 03:02:24 +00:00
if library . skip_library :
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Skipping { library . name } Library " )
2021-12-18 03:02:24 +00:00
continue
2022-05-05 13:21:17 +00:00
library_status [ library . name ] = { }
2021-10-04 17:51:32 +00:00
try :
2022-02-13 16:33:57 +00:00
logger . add_library_handler ( library . mapping_name )
2021-10-26 05:40:23 +00:00
plexapi . server . TIMEOUT = library . timeout
2022-06-30 13:14:09 +00:00
os . environ [ " PLEXAPI_PLEXAPI_TIMEOUT " ] = str ( library . timeout )
2021-08-12 20:37:03 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " { library . name } Library " )
2021-12-06 07:52:08 +00:00
logger . debug ( " " )
logger . debug ( f " Mapping Name: { library . original_mapping_name } " )
logger . debug ( f " Folder Name: { library . mapping_name } " )
for ad in library . asset_directory :
logger . debug ( f " Asset Directory: { ad } " )
logger . debug ( f " Asset Folders: { library . asset_folders } " )
logger . debug ( f " Create Asset Folders: { library . create_asset_folders } " )
2022-01-11 01:23:56 +00:00
logger . debug ( f " Download URL Assets: { library . download_url_assets } " )
2021-12-06 07:52:08 +00:00
logger . debug ( f " Sync Mode: { library . sync_mode } " )
2022-03-10 18:28:56 +00:00
logger . debug ( f " Minimum Items: { library . minimum_items } " )
2021-12-06 07:52:08 +00:00
logger . debug ( f " Delete Below Minimum: { library . delete_below_minimum } " )
2021-12-06 22:30:38 +00:00
logger . debug ( f " Delete Not Scheduled: { library . delete_not_scheduled } " )
2022-01-11 01:23:56 +00:00
logger . debug ( f " Default Collection Order: { library . default_collection_order } " )
2021-12-06 07:52:08 +00:00
logger . debug ( f " Missing Only Released: { library . missing_only_released } " )
logger . debug ( f " Only Filter Missing: { library . only_filter_missing } " )
logger . debug ( f " Show Unmanaged: { library . show_unmanaged } " )
logger . debug ( f " Show Filtered: { library . show_filtered } " )
logger . debug ( f " Show Missing: { library . show_missing } " )
logger . debug ( f " Show Missing Assets: { library . show_missing_assets } " )
2022-05-10 18:50:19 +00:00
logger . debug ( f " Save Report: { library . save_report } " )
logger . debug ( f " Report Path: { library . report_path } " )
2021-12-06 07:52:08 +00:00
logger . debug ( f " Clean Bundles: { library . clean_bundles } " )
logger . debug ( f " Empty Trash: { library . empty_trash } " )
logger . debug ( f " Optimize: { library . optimize } " )
logger . debug ( f " Timeout: { library . timeout } " )
2022-05-22 17:26:54 +00:00
if config . delete_collections and not playlist_only :
2022-05-05 13:21:17 +00:00
time_start = datetime . now ( )
2021-12-31 04:34:04 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Deleting all Collections from the { library . name } Library " , space = False , border = False )
2021-12-31 04:34:04 +00:00
logger . info ( " " )
for collection in library . get_all_collections ( ) :
logger . info ( f " Collection { collection . title } Deleted " )
library . query ( collection . delete )
2022-05-05 13:21:17 +00:00
library_status [ library . name ] [ " All Collections Deleted " ] = str ( datetime . now ( ) - time_start ) . split ( ' . ' ) [ 0 ]
2022-04-20 21:30:31 +00:00
2022-05-10 15:47:13 +00:00
time_start = datetime . now ( )
temp_items = None
list_key = None
expired = None
2022-07-21 18:19:25 +00:00
if config . Cache :
2022-05-10 15:47:13 +00:00
list_key , expired = config . Cache . query_list_cache ( " library " , library . mapping_name , 1 )
2022-07-21 18:19:25 +00:00
if cache_libraries and list_key and expired is False :
2022-05-10 15:47:13 +00:00
logger . info ( f " Library: { library . mapping_name } loaded from Cache " )
temp_items = config . Cache . query_list_ids ( list_key )
if not temp_items :
temp_items = library . cache_items ( )
2022-07-21 18:19:25 +00:00
if config . Cache and list_key :
config . Cache . delete_list_ids ( list_key )
if config . Cache and cache_libraries :
2022-05-10 15:47:13 +00:00
list_key = config . Cache . update_list_cache ( " library " , library . mapping_name , expired , 1 )
config . Cache . update_list_ids ( list_key , [ ( i . ratingKey , i . guid ) for i in temp_items ] )
2022-05-15 20:29:54 +00:00
if not library . is_music :
2021-08-12 20:37:03 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Mapping { library . name } Library " , space = False , border = False )
2021-08-12 20:37:03 +00:00
logger . info ( " " )
2022-04-20 21:30:31 +00:00
library . map_guids ( temp_items )
2022-05-10 15:47:13 +00:00
library_status [ library . name ] [ " Library Loading and Mapping " ] = str ( datetime . now ( ) - time_start ) . split ( ' . ' ) [ 0 ]
2022-04-20 21:30:31 +00:00
2022-07-14 13:46:33 +00:00
def run_operations_and_overlays ( ) :
if not config . test_mode and not collection_only and not playlist_only and not config . requested_metadata_files :
if not overlays_only and library . library_operation :
library_status [ library . name ] [ " Library Operations " ] = library . Operations . run_operations ( )
if not operations_only and ( library . overlay_files or library . remove_overlays ) :
library_status [ library . name ] [ " Library Overlays " ] = library . Overlays . run_overlays ( )
if config . library_first :
run_operations_and_overlays ( )
2022-04-20 21:30:31 +00:00
2022-05-22 17:26:54 +00:00
if not operations_only and not overlays_only and not playlist_only :
2022-05-05 13:21:17 +00:00
time_start = datetime . now ( )
2022-04-22 02:57:26 +00:00
for metadata in library . metadata_files :
metadata_name = metadata . get_file_name ( )
if config . requested_metadata_files and metadata_name not in config . requested_metadata_files :
logger . info ( " " )
logger . separator ( f " Skipping { metadata_name } Metadata File " )
continue
2022-03-18 13:01:12 +00:00
logger . info ( " " )
2022-04-22 02:57:26 +00:00
logger . separator ( f " Running { metadata_name } Metadata File \n { metadata . path } " )
if not config . test_mode and not config . resume_from and not collection_only :
try :
metadata . update_metadata ( )
except Failed as e :
library . notify ( e )
logger . error ( e )
collections_to_run = metadata . get_collections ( config . requested_collections )
if config . resume_from and config . resume_from not in collections_to_run :
logger . info ( " " )
logger . warning ( f " Collection: { config . resume_from } not in Metadata File: { metadata . path } " )
continue
if collections_to_run :
logger . info ( " " )
logger . separator ( f " { ' Test ' if config . test_mode else ' ' } Collections " )
logger . remove_library_handler ( library . mapping_name )
run_collection ( config , library , metadata , collections_to_run )
logger . re_add_library_handler ( library . mapping_name )
2022-05-05 13:21:17 +00:00
library_status [ library . name ] [ " Library Metadata Files " ] = str ( datetime . now ( ) - time_start ) . split ( ' . ' ) [ 0 ]
2021-05-19 19:21:22 +00:00
2022-07-14 13:46:33 +00:00
if not config . library_first :
run_operations_and_overlays ( )
2021-10-04 17:51:32 +00:00
2022-02-13 16:33:57 +00:00
logger . remove_library_handler ( library . mapping_name )
2021-10-04 17:51:32 +00:00
except Exception as e :
library . notify ( e )
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
logger . critical ( e )
2022-05-11 04:37:42 +00:00
return library_status
2021-12-30 21:29:16 +00:00
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 \
2022-04-28 14:47:40 +00:00
and metadata . templates [ data_template [ " name " ] ] [ 0 ] \
and " test " in metadata . templates [ data_template [ " name " ] ] [ 0 ] \
and metadata . templates [ data_template [ " name " ] ] [ 0 ] [ " test " ] is True :
2021-05-12 14:25:48 +00:00
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 ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Resuming Collections " )
2021-05-19 17:12:34 +00:00
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 )
2022-02-13 16:33:57 +00:00
logger . add_collection_handler ( library . mapping_name , collection_log_name )
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] = { " status " : " Unchanged " , " errors " : [ ] , " added " : 0 , " unchanged " : 0 , " removed " : 0 , " radarr " : 0 , " sonarr " : 0 }
2021-05-19 21:30:20 +00:00
2021-05-19 17:12:34 +00:00
try :
2022-04-19 05:58:38 +00:00
builder = CollectionBuilder ( config , metadata , mapping_name , collection_attrs , library = library , extra = output_str )
2022-02-20 00:08:41 +00:00
library . stats [ " names " ] . append ( builder . name )
2021-05-24 03:38:46 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Running { mapping_name } Collection " , space = False , border = False )
2021-05-12 14:25:48 +00:00
if len ( builder . schedule ) > 0 :
2022-02-13 16:33:57 +00:00
logger . info ( builder . schedule )
2021-05-12 14:25:48 +00:00
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 ( " " )
2022-02-13 16:33:57 +00:00
logger . info ( builder . smart_filter_details )
2021-05-24 03:38:46 +00:00
2021-11-03 14:36:11 +00:00
items_added = 0
items_removed = 0
2022-01-27 21:24:50 +00:00
if not builder . smart_url and builder . builders and not builder . blank_collection :
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 ' } " )
2022-04-21 20:56:32 +00:00
for method , value in builder . builders :
logger . debug ( " " )
logger . debug ( f " Builder: { method } : { value } " )
logger . info ( " " )
2022-07-19 13:25:48 +00:00
try :
builder . filter_and_save_items ( builder . gather_ids ( method , value ) )
except NonExisting as e :
if builder . ignore_blank_results :
logger . warning ( e )
else :
raise Failed ( e )
if not builder . added_items and builder . ignore_blank_results :
raise NonExisting ( f " Overlay Warning: No items found " )
2022-04-21 20:56:32 +00:00
2022-10-03 19:32:50 +00:00
builder . display_filters ( )
2021-05-12 14:25:48 +00:00
2022-02-09 02:31:29 +00:00
if len ( builder . added_items ) > 0 and len ( builder . added_items ) + builder . beginning_count > = builder . minimum and builder . build_collection :
2021-12-30 21:29:16 +00:00
items_added , items_unchanged = builder . add_to_collection ( )
library . stats [ " added " ] + = items_added
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " added " ] = items_added
2021-12-30 21:29:16 +00:00
library . stats [ " unchanged " ] + = items_unchanged
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " unchanged " ] = items_unchanged
2021-11-03 14:36:11 +00:00
items_removed = 0
2021-10-20 19:58:25 +00:00
if builder . sync :
2021-11-03 14:36:11 +00:00
items_removed = builder . sync_collection ( )
2021-12-30 21:29:16 +00:00
library . stats [ " removed " ] + = items_removed
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " removed " ] = items_removed
2021-10-20 19:58:25 +00:00
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-11-03 14:36:11 +00:00
radarr_add , sonarr_add = builder . run_missing ( )
2021-12-30 21:29:16 +00:00
library . stats [ " radarr " ] + = radarr_add
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " radarr " ] + = radarr_add
2021-12-30 21:29:16 +00:00
library . stats [ " sonarr " ] + = sonarr_add
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " sonarr " ] + = sonarr_add
2021-05-12 14:25:48 +00:00
2022-03-14 13:29:34 +00:00
valid = True
2022-04-01 06:39:23 +00:00
if builder . build_collection and not builder . blank_collection and (
2022-03-14 13:29:34 +00:00
( builder . smart_url and len ( library . get_filter_items ( builder . smart_url ) ) < builder . minimum )
2022-03-15 20:53:37 +00:00
or ( not builder . smart_url and len ( builder . added_items ) + builder . beginning_count < builder . minimum )
2022-03-14 13:29:34 +00:00
) :
logger . info ( " " )
logger . info ( f " Collection Minimum: { builder . minimum } not met for { mapping_name } Collection " )
2022-05-04 16:27:17 +00:00
delete_status = f " Minimum { builder . minimum } Not Met "
2022-03-14 13:29:34 +00:00
valid = False
if builder . details [ " delete_below_minimum " ] and builder . obj :
logger . info ( " " )
logger . info ( builder . delete ( ) )
2022-05-04 16:27:17 +00:00
library . stats [ " deleted " ] + = 1
delete_status = f " Deleted; { delete_status } "
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = delete_status
2022-03-14 13:29:34 +00:00
2021-09-15 03:16:59 +00:00
run_item_details = True
2022-01-27 21:24:50 +00:00
if valid and builder . build_collection and ( builder . builders or builder . smart_url or builder . blank_collection ) :
2021-09-15 03:16:59 +00:00
try :
builder . load_collection ( )
2021-11-03 14:36:11 +00:00
if builder . created :
2021-12-30 21:29:16 +00:00
library . stats [ " created " ] + = 1
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " Created "
2021-11-03 14:36:11 +00:00
elif items_added > 0 or items_removed > 0 :
2021-12-30 21:29:16 +00:00
library . stats [ " modified " ] + = 1
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " Modified "
2021-09-15 03:16:59 +00:00
except Failed :
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
2021-09-15 03:16:59 +00:00
run_item_details = False
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( " No Collection to Update " , space = False , border = False )
2021-09-15 03:16:59 +00:00
else :
2022-05-04 16:27:17 +00:00
details_list = builder . update_details ( )
if details_list :
pre = " "
2022-06-07 07:06:20 +00:00
if library . status [ str ( mapping_name ) ] [ " status " ] != " Unchanged " :
pre = f " { library . status [ str ( mapping_name ) ] [ ' status ' ] } and "
library . status [ str ( mapping_name ) ] [ " status " ] = f " { pre } Updated { ' , ' . join ( details_list ) } "
2021-12-09 04:57:00 +00:00
2021-12-08 03:31:20 +00:00
if builder . server_preroll is not None :
library . set_server_preroll ( builder . server_preroll )
logger . info ( " " )
logger . info ( f " Plex Server Movie pre-roll video updated to { builder . server_preroll } " )
2022-09-29 21:03:55 +00:00
if valid and run_item_details and ( builder . item_details or builder . custom_sort or builder . sync_to_trakt_list ) :
2021-09-15 03:16:59 +00:00
try :
builder . load_collection_items ( )
except Failed :
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( " No Items Found " , space = False , border = False )
2021-09-15 03:16:59 +00:00
else :
2021-12-17 14:24:46 +00:00
if builder . item_details :
builder . update_item_details ( )
2021-12-21 18:48:27 +00:00
if builder . custom_sort :
2022-03-19 22:52:59 +00:00
builder . sort_collection ( )
2022-05-09 06:58:09 +00:00
if builder . sync_to_trakt_list :
builder . sync_trakt_list ( )
2021-05-26 19:14:51 +00:00
2021-12-13 19:43:36 +00:00
builder . send_notifications ( )
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 )
2022-07-19 13:25:48 +00:00
except NonExisting as e :
logger . warning ( e )
library . status [ str ( mapping_name ) ] [ " status " ] = " Ignored "
2021-10-21 14:00:16 +00:00
except NotScheduled as e :
2022-02-13 16:33:57 +00:00
logger . info ( e )
2022-05-04 16:27:17 +00:00
if str ( e ) . endswith ( " and was deleted " ) :
library . stats [ " deleted " ] + = 1
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " Deleted Not Scheduled "
2022-05-12 15:04:52 +00:00
elif str ( e ) . startswith ( " Skipped because allowed_library_types " ) :
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " Skipped Invalid Library Type "
2022-05-04 16:27:17 +00:00
else :
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " Not Scheduled "
2021-05-12 14:25:48 +00:00
except Failed as e :
2021-10-04 17:51:32 +00:00
library . notify ( e , collection = mapping_name )
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
logger . error ( e )
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " PMM Failure "
library . status [ str ( mapping_name ) ] [ " errors " ] . append ( e )
2021-05-12 14:25:48 +00:00
except Exception as e :
2021-10-04 17:51:32 +00:00
library . notify ( f " Unknown Error: { e } " , collection = mapping_name )
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
2021-05-12 14:25:48 +00:00
logger . error ( f " Unknown Error: { e } " )
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " status " ] = " Unknown Error "
library . status [ str ( mapping_name ) ] [ " errors " ] . append ( e )
2022-05-04 16:27:17 +00:00
collection_run_time = str ( datetime . now ( ) - collection_start ) . split ( ' . ' ) [ 0 ]
2022-06-07 07:06:20 +00:00
library . status [ str ( mapping_name ) ] [ " run_time " ] = collection_run_time
2021-05-19 17:12:34 +00:00
logger . info ( " " )
2022-05-04 16:27:17 +00:00
logger . separator ( f " Finished { mapping_name } Collection \n Collection Run Time: { collection_run_time } " )
2022-02-13 16:33:57 +00:00
logger . remove_collection_handler ( library . mapping_name , collection_log_name )
2021-05-08 02:30:28 +00:00
2021-12-23 17:02:07 +00:00
def run_playlists ( config ) :
2022-02-20 00:08:41 +00:00
stats = { " created " : 0 , " modified " : 0 , " deleted " : 0 , " added " : 0 , " unchanged " : 0 , " removed " : 0 , " radarr " : 0 , " sonarr " : 0 , " names " : [ ] }
2021-12-30 21:29:16 +00:00
status = { }
2021-12-23 17:02:07 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( " Playlists " )
2021-12-23 17:02:07 +00:00
logger . info ( " " )
for playlist_file in config . playlist_files :
for mapping_name , playlist_attrs in playlist_file . playlists . items ( ) :
playlist_start = datetime . now ( )
if config . test_mode and ( " test " not in playlist_attrs or playlist_attrs [ " test " ] is not True ) :
no_template_test = True
if " template " in playlist_attrs and playlist_attrs [ " template " ] :
for data_template in util . get_list ( playlist_attrs [ " template " ] , split = False ) :
if " name " in data_template \
and data_template [ " name " ] \
and playlist_file . templates \
and data_template [ " name " ] in playlist_file . templates \
2022-04-28 14:47:40 +00:00
and playlist_file . templates [ data_template [ " name " ] ] [ 0 ] \
and " test " in playlist_file . templates [ data_template [ " name " ] ] [ 0 ] \
and playlist_file . templates [ data_template [ " name " ] ] [ 0 ] [ " test " ] is True :
2021-12-23 17:02:07 +00:00
no_template_test = False
if no_template_test :
continue
if " name_mapping " in playlist_attrs and playlist_attrs [ " name_mapping " ] :
playlist_log_name , output_str = util . validate_filename ( playlist_attrs [ " name_mapping " ] )
else :
playlist_log_name , output_str = util . validate_filename ( mapping_name )
2022-02-13 16:33:57 +00:00
logger . add_playlist_handler ( playlist_log_name )
2022-05-04 16:27:17 +00:00
status [ mapping_name ] = { " status " : " Unchanged " , " errors " : [ ] , " added " : 0 , " unchanged " : 0 , " removed " : 0 , " radarr " : 0 , " sonarr " : 0 }
2021-12-23 17:02:07 +00:00
server_name = None
library_names = None
try :
2022-04-19 05:58:38 +00:00
builder = CollectionBuilder ( config , playlist_file , mapping_name , playlist_attrs , extra = output_str )
2022-02-20 00:08:41 +00:00
stats [ " names " ] . append ( builder . name )
2021-12-23 17:02:07 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( f " Running { mapping_name } Playlist " , space = False , border = False )
2021-12-23 17:02:07 +00:00
if len ( builder . schedule ) > 0 :
2022-02-13 16:33:57 +00:00
logger . info ( builder . schedule )
2021-12-23 17:02:07 +00:00
items_added = 0
items_removed = 0
valid = True
logger . info ( " " )
logger . info ( f " Sync Mode: { ' sync ' if builder . sync else ' append ' } " )
method , value = builder . builders [ 0 ]
logger . debug ( " " )
logger . debug ( f " Builder: { method } : { value } " )
logger . info ( " " )
2022-09-08 20:11:11 +00:00
if method == " plex_watchlist " :
ids = builder . libraries [ 0 ] . get_rating_keys ( method , value , True )
elif " plex " in method :
2022-01-24 22:36:40 +00:00
ids = [ ]
2022-02-08 22:57:36 +00:00
for pl_library in builder . libraries :
2022-09-08 16:07:15 +00:00
try :
2022-09-08 20:11:11 +00:00
ids . extend ( pl_library . get_rating_keys ( method , value , True ) )
2022-09-08 16:07:15 +00:00
except Failed as e :
if builder . validate_builders :
raise
else :
logger . error ( e )
2022-01-24 22:36:40 +00:00
elif " tautulli " in method :
ids = [ ]
2022-02-08 22:57:36 +00:00
for pl_library in builder . libraries :
2022-09-08 16:07:15 +00:00
try :
ids . extend ( pl_library . Tautulli . get_rating_keys ( value , True ) )
except Failed as e :
if builder . validate_builders :
raise
else :
logger . error ( e )
2022-01-24 22:36:40 +00:00
else :
ids = builder . gather_ids ( method , value )
2021-12-23 17:02:07 +00:00
2022-10-03 19:32:50 +00:00
builder . display_filters ( )
2022-02-08 22:57:36 +00:00
builder . filter_and_save_items ( ids )
2021-12-23 17:02:07 +00:00
2022-05-04 16:27:17 +00:00
if len ( builder . added_items ) > 0 and len ( builder . added_items ) + builder . beginning_count > = builder . minimum :
2021-12-30 21:29:16 +00:00
items_added , items_unchanged = builder . add_to_collection ( )
2021-12-23 17:02:07 +00:00
stats [ " added " ] + = items_added
2021-12-30 21:29:16 +00:00
status [ mapping_name ] [ " added " ] + = items_added
stats [ " unchanged " ] + = items_unchanged
status [ mapping_name ] [ " unchanged " ] + = items_unchanged
2021-12-23 17:02:07 +00:00
items_removed = 0
if builder . sync :
items_removed = builder . sync_collection ( )
stats [ " removed " ] + = items_removed
2021-12-30 21:29:16 +00:00
status [ mapping_name ] [ " removed " ] + = items_removed
2021-12-23 17:02:07 +00:00
elif len ( builder . added_items ) < builder . minimum :
logger . info ( " " )
logger . info ( f " Playlist Minimum: { builder . minimum } not met for { mapping_name } Playlist " )
2022-05-04 16:27:17 +00:00
delete_status = f " Minimum { builder . minimum } Not Met "
2021-12-23 17:02:07 +00:00
valid = False
if builder . details [ " delete_below_minimum " ] and builder . obj :
2021-12-26 15:48:52 +00:00
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . info ( builder . delete ( ) )
2022-05-04 16:27:17 +00:00
stats [ " deleted " ] + = 1
delete_status = f " Deleted; { delete_status } "
status [ mapping_name ] [ " status " ] = delete_status
2021-12-23 17:02:07 +00:00
if builder . do_missing and ( len ( builder . missing_movies ) > 0 or len ( builder . missing_shows ) > 0 ) :
radarr_add , sonarr_add = builder . run_missing ( )
stats [ " radarr " ] + = radarr_add
2021-12-30 21:29:16 +00:00
status [ mapping_name ] [ " radarr " ] + = radarr_add
2021-12-23 17:02:07 +00:00
stats [ " sonarr " ] + = sonarr_add
2021-12-30 21:29:16 +00:00
status [ mapping_name ] [ " sonarr " ] + = sonarr_add
2021-12-23 17:02:07 +00:00
run_item_details = True
2022-05-04 16:27:17 +00:00
if valid and builder . builders :
try :
builder . load_collection ( )
if builder . created :
stats [ " created " ] + = 1
status [ mapping_name ] [ " status " ] = " Created "
elif items_added > 0 or items_removed > 0 :
stats [ " modified " ] + = 1
status [ mapping_name ] [ " status " ] = " Modified "
except Failed :
logger . stacktrace ( )
run_item_details = False
logger . info ( " " )
logger . separator ( " No Playlist to Update " , space = False , border = False )
else :
details_list = builder . update_details ( )
if details_list :
pre = " "
if status [ mapping_name ] [ " status " ] != " Unchanged " :
pre = f " { status [ mapping_name ] [ ' status ' ] } and "
status [ mapping_name ] [ " status " ] = f " { pre } Updated { ' , ' . join ( details_list ) } "
2021-12-23 17:02:07 +00:00
if valid and run_item_details and builder . builders and ( builder . item_details or builder . custom_sort ) :
try :
builder . load_collection_items ( )
except Failed :
logger . info ( " " )
2022-02-13 16:33:57 +00:00
logger . separator ( " No Items Found " , space = False , border = False )
2021-12-23 17:02:07 +00:00
else :
if builder . item_details :
builder . update_item_details ( )
if builder . custom_sort :
builder . sort_collection ( )
if valid :
2021-12-26 15:48:52 +00:00
builder . sync_playlist ( )
2021-12-23 17:02:07 +00:00
builder . send_notifications ( playlist = True )
2022-05-28 16:01:51 +00:00
except Deleted as e :
logger . info ( e )
2022-06-21 06:12:47 +00:00
status [ mapping_name ] [ " status " ] = " Deleted "
2021-12-23 17:02:07 +00:00
except NotScheduled as e :
2022-02-13 16:33:57 +00:00
logger . info ( e )
2022-05-04 16:27:17 +00:00
if str ( e ) . endswith ( " and was deleted " ) :
stats [ " deleted " ] + = 1
status [ mapping_name ] [ " status " ] = " Deleted Not Scheduled "
else :
status [ mapping_name ] [ " status " ] = " Not Scheduled "
2021-12-23 17:02:07 +00:00
except Failed as e :
config . notify ( e , server = server_name , library = library_names , playlist = mapping_name )
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
logger . error ( e )
2021-12-30 21:29:16 +00:00
status [ mapping_name ] [ " status " ] = " PMM Failure "
status [ mapping_name ] [ " errors " ] . append ( e )
2021-12-23 17:02:07 +00:00
except Exception as e :
config . notify ( f " Unknown Error: { e } " , server = server_name , library = library_names , playlist = mapping_name )
2022-02-13 16:33:57 +00:00
logger . stacktrace ( )
2021-12-23 17:02:07 +00:00
logger . error ( f " Unknown Error: { e } " )
2021-12-30 21:29:16 +00:00
status [ mapping_name ] [ " status " ] = " Unknown Error "
status [ mapping_name ] [ " errors " ] . append ( e )
2021-12-23 17:02:07 +00:00
logger . info ( " " )
2022-05-04 16:27:17 +00:00
playlist_run_time = str ( datetime . now ( ) - playlist_start ) . split ( ' . ' ) [ 0 ]
status [ mapping_name ] [ " run_time " ] = playlist_run_time
logger . info ( " " )
logger . separator ( f " Finished { mapping_name } Playlist \n Playlist Run Time: { playlist_run_time } " )
2022-02-13 16:33:57 +00:00
logger . remove_playlist_handler ( playlist_log_name )
2021-12-30 21:29:16 +00:00
return status , stats
2021-12-23 17:02:07 +00:00
2022-06-07 20:42:17 +00:00
if __name__ == " __main__ " :
try :
if run or test or collections or libraries or metadata_files or resume :
process ( {
" config_file " : config_file ,
" test " : test ,
" delete " : delete ,
" ignore_schedules " : ignore_schedules ,
" collections " : collections ,
" libraries " : libraries ,
" metadata_files " : metadata_files ,
" library_first " : library_first ,
" resume " : resume ,
" trace " : trace
} )
else :
times_to_run = util . get_list ( times )
valid_times = [ ]
for time_to_run in times_to_run :
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 between 00:00-23:59 " )
else :
raise Failed ( f " Argument Error: blank time argument " )
for time_to_run in valid_times :
schedule . every ( ) . day . at ( time_to_run ) . do ( process , { " config_file " : config_file , " time " : time_to_run , " delete " : delete , " library_first " : library_first , " trace " : trace } )
while True :
schedule . run_pending ( )
if not no_countdown :
current_time = datetime . now ( ) . strftime ( " % H: % M " )
seconds = None
og_time_str = " "
for time_to_run in valid_times :
new_seconds = ( datetime . strptime ( time_to_run , " % H: % M " ) - datetime . strptime ( current_time , " % 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
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 ' ' } "
logger . ghost ( f " Current Time: { current_time } | { time_str } until the next run at { og_time_str } | Runs: { ' , ' . join ( times_to_run ) } " )
else :
logger . error ( f " Time Error: { valid_times } " )
time . sleep ( 60 )
except KeyboardInterrupt :
logger . separator ( " Exiting Plex Meta Manager " )