-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwswap_notifier.py
59 lines (50 loc) · 2.25 KB
/
hwswap_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
import config
import datetime
import requests
from constants import KEYWORDS
REDDIT = config.reddit_config()
TWILIO = config.twilio_config()
def check_new_posts(sub, limit=25, flair=None, timeframe=datetime.timedelta(minutes=10)):
if flair:
return {post for post in REDDIT.subreddit(sub).new(limit=limit)
if post.link_flair_text == flair
and datetime.datetime.now() - datetime.datetime.fromtimestamp(post.created_utc) < timeframe}
else:
return {post for post in REDDIT.subreddit(sub).new(limit=limit)
if datetime.datetime.now() - datetime.datetime.fromtimestamp(post.created_utc) < timeframe}
def notify_buildapcsales_keywords(twilio_number, recipient):
new_posts = check_new_posts('buildapcsales', flair='GPU')
if len(new_posts) > 0:
info = "GPUs in stock!\n"
for post in new_posts:
info += post.url + '\n'
message = TWILIO.messages.create(body=info,
from_=twilio_number,
to=recipient)
requests.post(f"https://api.twilio.com/2010-04-01/Accounts/{message.account_sid}/Messages.json")
print(f"Found {len(new_posts)} GPUs in stock")
else:
print("No keywords found")
def notify_hardwareswap_keywords(twilio_number, recipient):
new_posts = check_new_posts('hardwareswap', flair='SELLING')
matches = {}
for post in new_posts:
for key in set(KEYWORDS.keys()):
if key in post.title.lower():
keyword = matches.setdefault(KEYWORDS[key], set())
keyword.add(post.url)
if len(matches) > 0:
info = "GPU for sale!\n"
for keyword, posts in matches.items():
info += f'{keyword}: '
for link in posts:
info += f'{link}\n'
# for post in new_posts:
# info += post.url + '\n'
message = TWILIO.messages.create(body=info,
from_=twilio_number,
to=recipient)
requests.post(f"https://api.twilio.com/2010-04-01/Accounts/{message.account_sid}/Messages.json")
print(f"Found {len(new_posts)} GPUs for sale")
else:
print("No keywords found")