-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwitchSnatcher.py
129 lines (98 loc) · 3.22 KB
/
SwitchSnatcher.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
127
128
129
"""
------------------------------------------------------------------------
SwitchSnatcher.py Functions
------------------------------------------------------------------------
Author: bb $kreetz
Email: [email protected]
__updated__ = "2020-04-12"
------------------------------------------------------------------------
"""
from CONSTANTS import *
from Website import Website
from URLMap import urlmap
from Browser import Browser
from datetime import datetime
import time
import colorama
URL_MAP = urlmap()
website_list = []
position = 12
for uuid in URL_MAP:
site = Website(uuid)
website_list.append([site, position])
position += 1
def clear():
print("\x1b[2J")
def reposition(x, y):
print("\x1b[{};{}H".format(x + 1, y + 1))
def reset_screen():
clear()
reposition(0, 0)
print(colorama.Fore.YELLOW + INTRO + colorama.Fore.RESET)
position = 12
reposition(position, 0)
for _ in URL_MAP:
reposition(position, 0)
print(colorama.Fore.YELLOW + 'Loading...' + colorama.Fore.RESET)
position += 1
reposition(position + 2, 0)
print(colorama.Fore.YELLOW + 'Press CTRL+C to go back to main menu.' + colorama.Fore.RESET)
def add_website(url, site_type, max_price):
browser = Browser()
if url not in URL_MAP.map.values():
uuid = URL_MAP.add(url)
website = Website(uuid, url=url, site_type=site_type, max_price=max_price)
cur_price, in_stock, name, value = browser.check_website(website)
website.update_website(cur_price, in_stock, name)
created = True
else:
created = False
browser.driver.close()
return created
def remove_website(uuid):
removed = False
try:
URL_MAP.remove_site(uuid)
Website.remove_website(uuid)
removed = True
except Exception as e:
print(f'{e}')
time.sleep(5)
return removed
def start_checking():
browser = Browser()
clear()
count = 0
reposition(0, 0)
print(colorama.Fore.BLUE + INTRO + colorama.Fore.RESET)
valid = False
reset_screen()
try:
count = 0
while not valid:
website, line = website_list.pop(0)
website_list.append([website, line])
curtime = datetime.now()
seconds_passed = int((curtime - website.last_checked).total_seconds())
if seconds_passed < REFRESH_PERIOD:
time.sleep(REFRESH_PERIOD - seconds_passed)
else:
count += 1
cur_price, in_stock, name, item_valid = browser.check_website(website)
website.update_website(cur_price, in_stock, name)
if item_valid:
print(colorama.Fore.GREEN + str(website) + colorama.Fore.RESET)
time.sleep(3)
valid = True
else:
if count == len(website_list) * 3:
reset_screen()
count = 0
reposition(line, 0)
print(colorama.Fore.RED + str(website) + colorama.Fore.RESET)
except KeyboardInterrupt:
try:
browser.driver.close()
time.sleep(1)
except Exception as e:
print(e)