forked from ahivert/tgtg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
101 lines (84 loc) · 2.91 KB
/
service.py
File metadata and controls
101 lines (84 loc) · 2.91 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import time
import random
import argparse
from datetime import datetime
from wakepy import set_keepawake
set_keepawake(keep_screen_awake=False)
from constants import SLEEP_LOWER, SLEEP_UPPER, TWO_HOURS
from utils import (
send_push_notification,
get_client,
time_since,
load_json,
)
class Service:
def __init__(
self,
items_to_track,
push_notification=True,
mail_notification=False,
exit_after_notification=True,
):
self.push_notification = push_notification
self.mail_notification = mail_notification
self.exit_after_notification = exit_after_notification
self.client = get_client()
self.start_time = time.time()
self.items_to_track = items_to_track
self.print_items_that_service_tracks()
def run(self):
while True:
self.get_time()
self.check_items()
if time_since(self.start_time) > TWO_HOURS:
self.restart_client()
self.start_time = time.time()
def check_items(self):
for item_id in self.items_to_track:
item = self.client.get_item(item_id=item_id)
if item["items_available"]:
print(f"\n\t{item['display_name']} is available\n")
self.send_notification(item)
if self.exit_after_notification:
exit()
self._sleep()
def restart_client(self):
self.client = get_client()
def send_notification(self, item):
if self.push_notification:
send_push_notification(message=item["display_name"])
def print_items_that_service_tracks(self):
favorite_items = load_json("favorite_items.json")
print("Service tracking:")
for item_id in self.items_to_track:
print(f"\t{favorite_items[str(item_id)]}")
@staticmethod
def _sleep():
sleep_duration = random.randint(SLEEP_LOWER, SLEEP_UPPER)
time.sleep(sleep_duration)
@staticmethod
def get_time():
time_now = datetime.now().strftime("%H:%M:%S")
print(f"{time_now}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--push-notification", type=int, default=1)
parser.add_argument("-m", "--mail-notification", type=int, default=0)
parser.add_argument("-e", "--exit-after-notification", type=int, default=1)
parser.add_argument(
"-l",
"--list",
nargs="+",
help="list of items ids (separated by a space)",
type=int,
default=[477157],
)
args = parser.parse_args()
push_notification = args.push_notification
mail_notification = args.mail_notification
exit_after_notification = args.exit_after_notification
items_to_track = args.list
service = Service(
items_to_track, push_notification, mail_notification, exit_after_notification
)
service.run()