Skip to content

Commit aa5a4b2

Browse files
authoredOct 2, 2022
Merge pull request #856 from anshu189/main
API Based Crypto Mail Alert Program
2 parents bd906bd + e7c1235 commit aa5a4b2

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
 

‎auto_crypto_alert_mail/Readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Crypto Exchange Mail Alert
2+
3+
Packages Used - smtplib & request
4+
5+
## Setup instructions
6+
7+
Open cmd/vscode_terminal run the following commands:
8+
```
9+
pip install secure-smtplib
10+
pip install requests
11+
```
12+
That's All, Now run script ```api_crypto_mail_alert.py```
13+
14+
## Output
15+
16+
Script will run in background and alerts automatically by sending an email wheneven the provided crypto currency reaches/crosses a target limit.
17+
18+
## Author
19+
20+
[Anshu](https://github.com/anshu189)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# To install the unsed libraries run the below commands in the terminal
2+
# pip install secure-smtplib
3+
# pip install requests
4+
5+
import requests
6+
import smtplib
7+
8+
'''
9+
<===== IMPORTANT =====>
10+
1. THIS WILL NOT WORK WHEN SENDING MAIL FROM GMAIL BECAUSE OF NEW GOOGLE SECURITY POLICIES.
11+
2. BUT YOU CAN SEND MAIL FROM OUTLOOK OR ANY OTHER MAIL SERVICES WITHOUT ANY PROBLEM.
12+
3. ONLY UPDATE 'from_email', 'from_email_password' & 'to_email'.
13+
4. IF USING ANY OTHER MAIL SERVICE THEN CHANGE THE SERVER AND PORT TOO IN LINE 19 & 20.
14+
'''
15+
16+
# Add your Email ID & it's Password by which you are sending the email alert
17+
from_email = "<SENDER'S_EMAIL_ID>"
18+
from_email_password = "<SENDER'S_EMAIL_PASSWORD>"
19+
mail_port = 587
20+
mail_server = "smtp-mail.outlook.com"
21+
22+
# Add Email ID to whom you want to send the alert
23+
to_email = "RECEIVER'S_EMAIL_ID"
24+
25+
26+
def check_price():
27+
spot_name = input("Input the crypto currency to get alerts (eg. BTCUSDT): ")
28+
print("1.If Price hits Above\n2.If Price hits Below\n")
29+
cpolarity = int(input("Choose from 1 & 2: "))
30+
trig_point = 1
31+
if cpolarity == 1:
32+
trig_point = float(input("Input the trigger price(dollar) above at which you want to recieve mail alert: "))
33+
else:
34+
trig_point = float(input("Input the trigger price(dollar) below at which you want to recieve mail alert: "))
35+
36+
def send_mail():
37+
server = smtplib.SMTP(mail_server, mail_port)
38+
server.ehlo()
39+
server.starttls()
40+
server.ehlo()
41+
server.login(from_email, from_email_password)
42+
subject = f"{spot_name.upper()} EXCHANGE RATE"
43+
44+
if cpolarity == 1:
45+
body = f"{spot_name.upper()} Exchange is now above ${trig_point}: Current Exchange Rate: ${lprice}."
46+
else:
47+
body = f"{spot_name.upper()} Exchange is now below ${trig_point}: Current Exchange Rate: ${lprice}."
48+
49+
msg = f'''Subject: {subject}\n
50+
To: {"".join(to_email)}\n
51+
{body}'''
52+
53+
server.sendmail(from_email, to_email, msg.encode("utf8"))
54+
55+
print("Alert! The E-mail has been sent!")
56+
server.quit()
57+
58+
while True:
59+
Url = "https://api.binance.com/api/v3/ticker/24hr"
60+
r = requests.get(Url)
61+
json_list = r.json()
62+
cryptoname = {}
63+
lprice = 0
64+
for i in range(len(json_list)):
65+
if json_list[i]["symbol"] == spot_name.upper():
66+
cryptoname = json_list[i]
67+
try:
68+
lprice = float(cryptoname["lastPrice"][4:6])
69+
print(lprice)
70+
except ValueError:
71+
print("This Exchange is not available.")
72+
73+
if lprice >= trig_point and cpolarity == 1:
74+
send_mail()
75+
exit()
76+
if lprice <= trig_point and cpolarity == 2:
77+
send_mail()
78+
exit()
79+
80+
81+
check_price()

0 commit comments

Comments
 (0)
Please sign in to comment.