-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathnotifier.py
214 lines (177 loc) · 6.3 KB
/
notifier.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import json
import platform
import random
import webbrowser
from datetime import datetime, time
from os import path, getenv, system
from time import sleep
from urllib.request import urlopen, Request
from enum import Enum
import sys
import requests
from dotenv import load_dotenv
platform = platform.system()
PLT_WIN = "Windows"
PLT_LIN = "Linux"
PLT_MAC = "Darwin"
class Methods(str, Enum):
GET_SELENIUM = "GET_SELENIUM"
GET_URLLIB = "GET_URLLIB"
GET_API = "GET_API"
# Set up environment variables and constants. Do not modify this unless you know what you are doing!
load_dotenv()
USE_TWILIO = False
USE_SELENIUM = False
USE_DISCORD_HOOK = False
WEBDRIVER_PATH = path.normpath(getenv('WEBDRIVER_PATH'))
DISCORD_WEBHOOK_URL = getenv('DISCORD_WEBHOOK_URL')
TWILIO_TO_NUM = getenv('TWILIO_TO_NUM')
TWILIO_FROM_NUM = getenv('TWILIO_FROM_NUM')
TWILIO_SID = getenv('TWILIO_SID')
TWILIO_AUTH = getenv('TWILIO_AUTH')
ALERT_DELAY = int(getenv('ALERT_DELAY'))
MIN_DELAY = int(getenv('MIN_DELAY'))
MAX_DELAY = int(getenv('MAX_DELAY'))
OPEN_WEB_BROWSER = getenv('OPEN_WEB_BROWSER') == 'true'
with open('sites.json', 'r') as f:
sites = json.load(f)
# Selenium Setup
if WEBDRIVER_PATH:
USE_SELENIUM = True
print("Enabling Selenium... ", end='')
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=WEBDRIVER_PATH)
reload_count = 0
print("Done!")
# Twilio Setup
if TWILIO_TO_NUM and TWILIO_FROM_NUM and TWILIO_SID and TWILIO_AUTH:
USE_TWILIO = True
print("Enabling Twilio... ", end='')
from twilio.rest import Client
client = Client(TWILIO_SID, TWILIO_AUTH)
print("Done!")
# Discord Setup
if DISCORD_WEBHOOK_URL:
USE_DISCORD_HOOK = True
print('Enabled Discord Web Hook.')
# Platform specific settings
print("Running on {}".format(platform))
if platform == PLT_WIN:
from win10toast import ToastNotifier
toast = ToastNotifier()
def alert(site):
product = site.get('name')
print("{} IN STOCK".format(product))
print(site.get('url'))
if OPEN_WEB_BROWSER:
webbrowser.open(site.get('url'), new=1)
os_notification("{} IN STOCK".format(product), site.get('url'))
sms_notification(site.get('url'))
discord_notification(product, site.get('url'))
sleep(ALERT_DELAY)
def os_notification(title, text):
if platform == PLT_MAC:
system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
system('afplay /System/Library/Sounds/Glass.aiff')
system('say "{}"'.format(title))
elif platform == PLT_WIN:
toast.show_toast(title, text, duration=5, icon_path="icon.ico")
elif platform == PLT_LIN:
try:
icon_path = path.realpath('icon.ico')
system('notify-send "{}" "{}" -i {}'.format(title, text, icon_path))
except:
# No system support for notify-send
pass
def sms_notification(url):
if USE_TWILIO:
client.messages.create(to=TWILIO_TO_NUM, from_=TWILIO_FROM_NUM, body=url)
def discord_notification(product, url):
if USE_DISCORD_HOOK:
data = {
"content": "{} in stock at {}".format(product, url),
"username": "In Stock Alert!"
}
result = requests.post(DISCORD_WEBHOOK_URL, data=json.dumps(data), headers={"Content-Type": "application/json"})
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print("Payload delivered successfully, code {}.".format(result.status_code))
def selenium_get(url):
global driver
global reload_count
driver.get(url)
http = driver.page_source
reload_count += 1
if reload_count == 10:
reload_count = 0
driver.close()
driver.quit()
driver = webdriver.Firefox(options=options, executable_path=WEBDRIVER_PATH)
return http
def urllib_get(url):
# for regular sites
# Fake a Firefox client
request = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
page = urlopen(request, timeout=30)
html_bytes = page.read()
html = html_bytes.decode("utf-8")
return html
def nvidia_get(url, api_url):
response = requests.get(api_url, timeout=5)
item = response.json()
if item['products']['product'][0]['inventoryStatus']['status'] != "PRODUCT_INVENTORY_OUT_OF_STOCK":
alert(url)
def is_test():
try:
if sys.argv[1] == 'test':
alert(sites[0])
print("Test complete, if you received notification, you're good to go.")
return True
except:
return False
def main():
search_count = 0
exit() if is_test() else False
while True:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Starting search {} at {}".format(search_count, current_time))
search_count += 1
for site in sites:
if site.get('enabled'):
print("\tChecking {}...".format(site.get('name')))
try:
if site.get('method') == Methods.GET_SELENIUM:
if not USE_SELENIUM:
continue
html = selenium_get(site.get('url'))
elif site.get('method') == Methods.GET_API:
if 'nvidia' in site.get('name').lower():
nvidia_get(site.get('url'), site.get('api'))
continue
else:
html = urllib_get(site.get('url'))
except Exception as e:
print("\t\tConnection failed...")
print("\t\t{}".format(e))
continue
keyword = site.get('keyword')
alert_on_found = site.get('alert')
index = html.upper().find(keyword.upper())
if alert_on_found and index != -1:
alert(site)
elif not alert_on_found and index == -1:
alert(site)
base_sleep = 1
total_sleep = base_sleep + random.uniform(MIN_DELAY, MAX_DELAY)
sleep(total_sleep)
if __name__ == '__main__':
main()