-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto-alerter.py
68 lines (49 loc) · 1.83 KB
/
crypto-alerter.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
#!/usr/bin/env python3
import os, sys
import requests, smtplib
from dataclasses import dataclass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
CHANGE_PERCENT_THRESHOLD = float(sys.argv[1]) if len(sys.argv) > 1 else 7 # lucky number magic
COINS_TO_CHECK = ['ethereum']
def main():
for coin in COINS_TO_CHECK:
p = Ping(coin)
print(p)
if p.price_change_percentage_24h > CHANGE_PERCENT_THRESHOLD:
send_email('magic internet money update!!', str(p))
elif p.price_change_percentage_24h < -1 * CHANGE_PERCENT_THRESHOLD:
send_email('sell', str(p))
@dataclass
class Ping:
def __init__(self, coin:str, **kwargs):
r = requests.get(f'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids={coin}')
for kw, v in r.json()[0].items():
if kw in self.__dataclass_fields__.keys():
self.__setattr__(kw, v)
symbol: str
last_updated: str
price_change_percentage_24h: float
price_change_24h: float
current_price: float
high_24h: float
low_24h: float
def send_email(subject:str, body: str) -> None:
login_address = os.getenv('GMAIL_USER')
secret = os.getenv('GMAIL_PASS')
to_address = os.getenv('ALERT_PHONE_NUMBER_EMAIL')
if any(map(lambda env: env == None, [login_address, secret, to_address])):
print('missing required environment variables')
exit(1)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login_address, secret)
message = MIMEMultipart()
message['From'] = login_address
message['To'] = to_address
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
server.sendmail(login_address, to_address, message.as_string())
server.quit()
if __name__ == '__main__':
main()