-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.py
executable file
·265 lines (200 loc) · 6.97 KB
/
notify.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from imapclient import IMAPClient, SEEN
import requests
import threading
import praw
import time
import json
import os
import email
import re
# Seen db
seen = set()
if os.path.exists('db.json'):
with open('db.json', 'r') as f:
seen = set(json.load(f))
seen_lock = threading.Lock()
# Notify Slack
def notify_slack(url, title):
print(url + ' | ' + title)
res = requests.post('https://hooks.slack.com/services/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',
data=json.dumps({
'username': 'gearbot',
'icon_emoji': ':moneybag:',
'text': ':fire: <' + url + '|' + title + '> :fire:',
}), headers={'Content-Type': 'application/json'})
if res.status_code != 200:
print(res.text)
# Notify Discord
def notify_discord(url, title):
res = requests.post('https://discordapp.com/api/webhooks/412776843805065216/GX5E7CZtDcF44K0ZlZHCg-C3xyJMQzPSXGrzYoJWvRXMREXHFuYxZF5Gpqs-h898BDo-',
data=json.dumps({
'username': 'gearbot',
'content': title + '\n' + url,
}), headers={'Content-Type': 'application/json'})
if res.status_code != 200:
print(res.text)
# Posts we care about
def reject_reddit(id, title):
if id in seen:
return True
tag = title.split(' ')[0]
if tag == '[GPU]':
return False
if tag == '[MOBO]':
return False
if tag == '[PSU':
return False
return True
def reddit():
# Set up reddit api
reddit = praw.Reddit(client_id='zGsOy6Dtyv5TUQ',
client_secret='-haf_3Kez9TbWwg2zFtPwy3PsRI',
password='BMYBSevMjpO9Jd9s',
user_agent='hanzo',
username='zeekay')
while True:
# Reddit Posts
print('checking for submissions...')
try:
submissions = list(reddit.subreddit('buildapcsales').new(limit=10))
except Exception as err:
print(err)
continue
for post in submissions:
id = 'reddit' + post.id
if reject_reddit(id, post.title):
continue
try:
seen_lock.acquire()
seen.add(id)
finally:
seen_lock.release()
print(post.title, post.permalink)
print(post.url, '\n')
notify_slack(post.url, post.title)
notify_discord(post.url, post.title)
print('sleeping for 10 seconds...')
time.sleep(10)
# Emails we care about
def reject_email(id, host):
if id in seen:
return True
if host == 'nowinstock.net':
return False
if host == 'blockchainrigs.io':
return False
return True
def format_blockchainrigs_email(msg):
msg = msg.replace('=\r\n', '').replace('\r\n', '').replace('<br>', '').replace('=3D', '=')
match = re.search(r"^In stock: (.*?) Buy Link : <a href='(.*?)'>", msg, re.MULTILINE)
if not match:
return ['', '']
return [match.group(2), match.group(1)]
def format_nowinstock_email(msg):
msg = msg.replace('=\r\n', '').replace('\r\n', '').replace('<br>', '').replace('=3D', '=')
match = re.search(r'^(.*?). Goto: (.*?)NowInStock', msg, re.MULTILINE)
if not match:
return ['', '']
return [match.group(2), match.group(1)]
def email_():
# Email Constants
FROM_EMAIL = '[email protected]'
FROM_PWD = '4QEN7C07Bz7h'
SMTP_SERVER = 'imap.gmail.com'
# Set up email api
# idle client
mail_clients = [None, None]
def reset_connection():
try:
print('resetting email connection')
idle_mail = mail_clients[0] = IMAPClient(SMTP_SERVER, use_uid=True, ssl=True)
idle_mail.login(FROM_EMAIL, FROM_PWD)
idle_mail.select_folder('INBOX')
idle_mail.idle()
mail = mail_clients[1] = IMAPClient(SMTP_SERVER, use_uid=True, ssl=True)
mail.login(FROM_EMAIL, FROM_PWD)
mail.select_folder('inbox')
except Exception as err:
print(err)
reset_connection()
reset_connection()
old_time = time.time()
while True:
idle_mail = mail_clients[0]
mail = mail_clients[1]
print('checking for emails...')
responses = []
if time.time() - old_time > 599:
try:
responses.extend(idle_mail.idle_done())
except Exception as err:
print(err)
print('idle_done had an issue, continuing')
reset_connection()
old_time = time.time()
try:
responses.extend(idle_mail.idle_check(timeout=30))
except Exception as err:
print(err)
print('idle_mail had an issue, continuing')
continue
print('Server sent:', responses or 'nothing')
if not responses:
continue
try:
# this is needed to force refresh
mail.fetch([1], ['RFC822'])
messages = mail.search(['UNSEEN'])
except Exception as err:
print(err)
print('search had an issue, continuing')
continue
print('messages')
print(messages)
try:
items = mail.fetch(messages, ['ENVELOPE', 'RFC822']).items()
except Exception as err:
print(err)
print('fetch had an issue, continuing')
continue
for uid, data in items:
try:
print('UID')
print(uid)
id = 'email' + str(uid)
mail.set_flags([uid], SEEN)
envelope = data[b'ENVELOPE']
host = envelope.sender[0].host.decode()
if reject_email(id, host):
continue
message = email.message_from_bytes(data[b'RFC822'])
text = ''
if message.is_multipart():
for payload in message.get_payload():
text += payload.get_payload()
else:
text = message.get_payload()
url = ''
title = ''
if host == 'nowinstock.net':
[url, title] = format_nowinstock_email(text)
elif host == 'blockchainrigs.io':
[url, title] = format_blockchainrigs_email(text)
else:
print('Host not recognized: ' + host)
if url and title:
notify_slack(url, title)
notify_discord(url, title)
else:
print('Could not decode email')
except Exception as err:
print(err)
if __name__ == '__main__':
reddit_thread = threading.Thread(target=reddit)
reddit_thread.start()
email_thread = threading.Thread(target=email_)
email_thread.start()
while True:
with open('db.json', 'w') as f:
json.dump(list(seen), f)
time.sleep(30)