-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.py
126 lines (105 loc) · 3.38 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
# App to help get a PS5
# Author: Tyler Gamvrelis
# Standard library imports
from datetime import datetime, timezone
from functools import partial
from queue import Queue
import logging
import sys
import threading
import time
import webbrowser
# Local application imports
from AppUtils import parse_args, setup_logger
from AudioNotifier import AudioNotifier
from Emailer import Emailer
from LbabinzTracker import LbabinzTracker
from NowInStockTracker import NowInStockTracker
# Globals
logger = logging.getLogger(__name__)
snscrape_logger = logging.getLogger('snscrape')
snscrape_logger.setLevel(logging.WARNING)
def add_input(input_queue):
while True:
input_queue.put(sys.stdin.readline())
def stock_check_callback(audio_notifier, emailer, result):
# TODO: Consider checking for duplicate drops across trackers within last
# X seconds
logger.debug(result)
if result is None:
return
logger.info(result.info)
for link in result.links:
webbrowser.open(link)
if audio_notifier:
audio_notifier.start_audio()
if emailer:
emailer.send_drop_message(result)
def main():
args = parse_args()
period = args['period']
log_level = args['log']
mute = args['mute']
test_mode = args['test']
send_emails = args['email']
setup_logger(log_level, __file__)
logger.info('Started app')
if test_mode:
logger.info('~~TEST MODE~~')
emailer = None
if send_emails:
emailer = Emailer()
emailer.save_credentials_if_needed()
audio_notifier = None
if not mute:
audio_notifier = AudioNotifier()
audio_notifier.start()
trackers = []
trackers.append(LbabinzTracker(datetime.now(timezone.utc)))
trackers.append(NowInStockTracker())
for tracker in trackers:
tracker.set_callback(
partial(stock_check_callback, audio_notifier, emailer)
)
if test_mode:
tracker.enable_test_mode()
tracker.start()
# https://stackoverflow.com/questions/2408560/python-nonblocking-console-input
input_queue = Queue()
input_thread = threading.Thread(target=add_input, args=(input_queue,))
input_thread.daemon = True
input_thread.start()
try:
ticks = 0
while True:
if ticks % period == 0:
# At the start of each period, tell each tracker to perform a
# stock check. Handling of results is dealt with in the callback
# function
logger.info(
f'{datetime.now()}: Requesting stock checks...'
)
for tracker in trackers:
tracker.request_stock_check()
# Any user input containing enter stops the audio notification
if audio_notifier and not input_queue.empty():
input_queue.get()
audio_notifier.stop_audio()
# Bookkeeping
ticks += 1
time.sleep(1)
except KeyboardInterrupt as e:
print('Interrupted: {0}'.format(e))
# Clean up trackers
for tracker in trackers:
tracker.stop()
for tracker in trackers:
tracker.join()
if audio_notifier:
audio_notifier.stop()
audio_notifier.join()
logger.info('Exiting...')
if __name__ == '__main__':
main()
sys.exit(0)