-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_in_loop.py
68 lines (45 loc) · 1.92 KB
/
run_in_loop.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
import traceback
import subprocess
from time import sleep
from datetime import datetime
from logger import logger
from config_reader import config
def _inside_running_interval() -> bool:
"""Check if the current time is in the running interval
:rtype: bool
:returns: Whether the current time is in the running interval or not
"""
start_time_str = config.behavior.running_interval_start
end_time_str = config.behavior.running_interval_end
if start_time_str == "00:00" and end_time_str == "00:00":
return True
start_time = datetime.strptime(start_time_str, "%H:%M").time()
end_time = datetime.strptime(end_time_str, "%H:%M").time()
if start_time > end_time:
raise SystemExit("Start time must be before the end time!")
now = datetime.now().time()
current_hour = now.hour
if current_hour == start_time.hour and current_hour == end_time.hour:
if end_time.minute - start_time.minute < 10:
raise SystemExit("There should be at least 10 minutes between the start and end!")
return start_time <= now <= end_time
def main() -> None:
command = ["python", "run_ad_clicker.py"]
while True:
if not _inside_running_interval():
start_time = config.behavior.running_interval_start
logger.info(f"Outside of the running interval. Waiting {start_time} to start...")
sleep(60)
continue
subprocess.run(command)
logger.info(f"Sleeping {config.behavior.loop_wait_time} seconds...")
sleep(config.behavior.loop_wait_time)
if __name__ == "__main__":
try:
main()
except Exception as exp:
logger.error("Exception occurred. See the details in the log file.")
message = str(exp).split("\n")[0]
logger.debug(f"Exception: {message}")
details = traceback.format_tb(exp.__traceback__)
logger.debug(f"Exception details: \n{''.join(details)}")