-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
executable file
·83 lines (61 loc) · 2.35 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import arr.radarr as radarrAPI
import arr.sonarr as sonarrAPI
import setup.args as args
import console
import setup.logger as logger
import pathlib
import portalocker
import schedule
import time
import threading
import os
import setup.defaults as defaults
def run_threaded(job_func,userargs):
job_thread = threading.Thread(target=job_func,args=[userargs])
job_thread.start()
def run(userargs,block=False):
try:
with portalocker.Lock(os.path.join(defaults.getHomeDir(),f"{userargs.clientname}.lock"),fail_when_locked=block, timeout=1000) as fh:
console.logging.info(f"Start RUN")
console.mainConsole.print(console.Panel(f"Looking through {userargs.subcommand} for matches",style=console.normal_header_style))
#Create Folder for log and torrents
if userargs.subcommand=="sonarr":
pathlib.Path(userargs.sonarr.folder).mkdir(parents=True, exist_ok=True)
sonarrObj=sonarrAPI.Sonarr()
sonarrObj.dryrun=userargs["dryrun"]
sonarrObj.process()
else:
pathlib.Path(userargs.radarr.folder).mkdir(parents=True, exist_ok=True)
radarrObj=radarrAPI.Radarr()
radarrObj.dryrun=userargs["dryrun"]
radarrObj.process()
console.logging.info(f"Finish RUN")
except Exception as E:
if isinstance(E,portalocker.exceptions.AlreadyLocked):
#Fix Later to use rich if possible
print(E)
console.logging.info(str(E))
#probably a crash
else:
print(E)
console.logging.info(E)
lock=os.path.join(defaults.getHomeDir(),f"{userargs.clientname}.lock")
if os.path.exists(lock):
os.remove(lock)
console.logging.info(f"Finish RUN")
def main():
logger.setupLogging()
userargs=args.getArgs()
if userargs.interval>0:
console.logging.info(f"Running Every {userargs.interval} Minutes")
schedule.every(userargs.interval).minutes.do(run_threaded,run,userargs=userargs)
schedule.run_all()
while True:
schedule.run_pending()
time.sleep(1)
else:
console.logging.info(f"Running Program Once")
run(userargs,block=True)
if __name__ == '__main__':
main()