-
Notifications
You must be signed in to change notification settings - Fork 0
/
zapper.py
80 lines (58 loc) · 2.32 KB
/
zapper.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
75
76
77
78
79
80
from datetime import datetime, timedelta
import pytz
import requests
from random import randrange
import time
from util import make_url, rand_minutes
class ZapTimer:
def __init__(self, timezone_str, interval_minutes, interval_percent, sleep_hour_before, sleep_hour_after):
self.timezone = pytz.timezone(timezone_str)
self.interval_minutes = interval_minutes
self.interval_percent = interval_percent
self.sleep_hour_before = sleep_hour_before
self.sleep_hour_after = sleep_hour_after
self.next_time: datetime = self.get_next_time()
def now(self) -> datetime:
return datetime.now(self.timezone)
def get_next_time(self) -> datetime:
return self.now() + timedelta(minutes=rand_minutes(self.interval_minutes, self.interval_percent))
def set_next_time(self):
self.next_time = self.get_next_time()
def sleep_time(self) -> bool:
now = self.now()
return now.hour < self.sleep_hour_before or now.hour >= self.sleep_hour_after
def time_remaining(self):
return self.next_time - self.now()
def time_to_zap(self) -> bool:
if self.sleep_time():
print(f"{self.now()} - It's sleeping time...")
return False
if self.time_remaining().total_seconds() <= 0:
print(f"{self.now()} - It's time! Zapping...")
return True
print(f"{self.now()} - Not time yet ({self.time_remaining()} remaining). Sleeping...")
return False
class Zapper:
def __init__(self, timer: ZapTimer, api_name, zap_level, message="", sleep_time_s=5):
self.timer = timer
self.api_name = api_name
self.zap_level = zap_level
self.message = message
self.sleep_time_s = sleep_time_s
def now(self) -> datetime:
return self.timer.now()
def zap(self):
try:
url = make_url(self.api_name, self.zap_level, self.message)
print(f"{self.now()} - Zapping to {url}")
requests.get(url)
self.timer.set_next_time()
except Exception as e:
print(f"{self.now()} - ERROR ZAPPING: {e}")
def attempt_zapping(self):
if self.timer.time_to_zap():
self.zap()
def run(self):
while True:
self.attempt_zapping()
time.sleep(self.sleep_time_s)