2021-03-04 06:16:37 +00:00
import argparse , logging , os , re , sys , time
2021-02-28 03:56:49 +00:00
from datetime import datetime
2021-03-04 06:16:37 +00:00
try :
import schedule
from modules import tests , util
from modules . config import Config
except ModuleNotFoundError :
print ( " Error: Requirements are not installed " )
sys . exit ( 0 )
2021-01-20 21:37:59 +00:00
parser = argparse . ArgumentParser ( )
2021-02-24 06:42:58 +00:00
parser . add_argument ( " --my-tests " , dest = " tests " , help = argparse . SUPPRESS , action = " store_true " , default = False )
2021-02-21 08:13:07 +00:00
parser . add_argument ( " --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 )
parser . add_argument ( " -t " , " --time " , dest = " time " , help = " Time to update each day use format HH:MM (Default: 03:00) " , default = " 03:00 " , type = str )
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 )
parser . add_argument ( " -cl " , " --collection " , " --collections " , dest = " collections " , help = " Process only specified collections (comma-separated list) " , type = str , default = " " )
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 ( )
if not re . match ( " ^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$ " , args . time ) :
2021-02-24 06:44:06 +00:00
raise util . Failed ( f " Argument Error: time argument invalid: { args . time } must be in the HH:MM format " )
2021-01-20 21:37:59 +00:00
2021-02-24 06:42:58 +00:00
util . separating_character = args . divider [ 0 ]
2021-01-20 21:37:59 +00:00
if 90 < = args . width < = 300 :
util . screen_width = args . width
else :
2021-02-24 06:44:06 +00:00
raise util . Failed ( f " Argument Error: width argument invalid: { args . width } must be an integer between 90 and 300 " )
2021-01-20 21:37:59 +00:00
default_dir = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , " config " )
if args . config and os . path . exists ( args . config ) : default_dir = os . path . join ( os . path . dirname ( os . path . abspath ( args . config ) ) )
2021-02-24 06:44:06 +00:00
elif args . config and not os . path . exists ( args . config ) : raise util . Failed ( f " Config Error: config not found at { os . path . abspath ( args . config ) } " )
elif not os . path . exists ( os . path . join ( default_dir , " config.yml " ) ) : raise util . Failed ( f " Config Error: config not found at { os . path . abspath ( default_dir ) } " )
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
file_handler = logging . handlers . TimedRotatingFileHandler ( os . path . join ( default_dir , " logs " , " meta.log " ) , when = " midnight " , backupCount = 10 , encoding = " utf-8 " )
file_handler . addFilter ( fmt_filter )
file_handler . setFormatter ( logging . Formatter ( " [ %(asctime)s ] %(filename)-27s %(levelname)-10s | %(message)-100s | " ) )
cmd_handler = logging . StreamHandler ( )
cmd_handler . setFormatter ( logging . Formatter ( " | %(message)-100s | " ) )
2021-02-21 08:13:07 +00:00
cmd_handler . setLevel ( logging . DEBUG if args . tests or args . test or args . debug else logging . INFO )
2021-01-20 21:37:59 +00:00
logger . addHandler ( cmd_handler )
logger . addHandler ( file_handler )
sys . excepthook = util . my_except_hook
2021-02-24 06:42:58 +00:00
util . separator ( )
2021-01-20 21:37:59 +00:00
logger . info ( util . get_centered_text ( " " ) )
logger . info ( util . get_centered_text ( " ____ _ __ __ _ __ __ " ) )
2021-02-24 06:42:58 +00:00
logger . info ( util . get_centered_text ( " | _ \\ | | _____ __ | \\ / | ___| |_ __ _ | \\ / | __ _ _ __ __ _ __ _ ___ _ __ " ) )
logger . info ( util . get_centered_text ( " | |_) | |/ _ \\ \\ / / | | \\ /| |/ _ \\ __/ _` | | | \\ /| |/ _` | ' _ \\ / _` |/ _` |/ _ \\ ' __| " ) )
2021-01-20 21:37:59 +00:00
logger . info ( util . get_centered_text ( " | __/| | __/> < | | | | __/ || (_| | | | | | (_| | | | | (_| | (_| | __/ | " ) )
2021-02-24 06:42:58 +00:00
logger . info ( util . get_centered_text ( " |_| |_| \\ ___/_/ \\ _ \\ |_| |_| \\ ___| \\ __ \\ __,_| |_| |_| \\ __,_|_| |_| \\ __,_| \\ __, | \\ ___|_| " ) )
2021-01-20 21:37:59 +00:00
logger . info ( util . get_centered_text ( " |___/ " ) )
2021-03-02 07:19:22 +00:00
logger . info ( util . get_centered_text ( " Version: 1.3.0 " ) )
2021-02-24 06:42:58 +00:00
util . separator ( )
2021-01-20 21:37:59 +00:00
2021-02-15 06:51:15 +00:00
if args . tests :
2021-01-20 21:37:59 +00:00
tests . run_tests ( default_dir )
sys . exit ( 0 )
2021-02-17 06:10:50 +00:00
def start ( config_path , test , daily , collections ) :
2021-02-24 06:42:58 +00:00
if daily : start_type = " Daily "
elif test : start_type = " Test "
elif collections : start_type = " Collections "
else : start_type = " "
2021-02-28 03:56:49 +00:00
start_time = datetime . now ( )
2021-02-24 06:44:06 +00:00
util . separator ( f " Starting { start_type } Run " )
2021-01-20 21:37:59 +00:00
try :
config = Config ( default_dir , config_path )
2021-02-15 23:09:24 +00:00
config . update_libraries ( test , collections )
2021-01-20 21:37:59 +00:00
except Exception as e :
util . print_stacktrace ( )
logger . critical ( e )
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-01-20 21:37:59 +00:00
try :
2021-02-15 23:09:24 +00:00
if args . run or args . test or args . collections :
start ( args . config , args . test , False , args . collections )
2021-01-20 21:37:59 +00:00
else :
2021-01-21 01:53:00 +00:00
length = 0
2021-02-17 06:10:50 +00:00
schedule . every ( ) . day . at ( args . time ) . do ( start , args . config , False , True , None )
2021-01-20 21:37:59 +00:00
while True :
schedule . run_pending ( )
2021-02-28 03:56:49 +00:00
current = datetime . now ( ) . strftime ( " % H: % M " )
seconds = ( datetime . strptime ( args . time , " % H: % M " ) - datetime . strptime ( current , " % H: % M " ) ) . total_seconds ( )
2021-01-21 01:53:00 +00:00
hours = int ( seconds / / 3600 )
if hours < 0 :
hours + = 24
minutes = int ( ( seconds % 3600 ) / / 60 )
2021-02-24 06:44:06 +00:00
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 ' ' } "
2021-01-21 01:53:00 +00:00
2021-02-24 06:44:06 +00:00
length = util . print_return ( length , f " Current Time: { current } | { time_str } until the daily run at { args . time } " )
2021-01-20 21:37:59 +00:00
time . sleep ( 1 )
except KeyboardInterrupt :
2021-02-24 06:42:58 +00:00
util . separator ( " Exiting Plex Meta Manager " )