-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin_price_alert.py
49 lines (40 loc) · 2.09 KB
/
bitcoin_price_alert.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
from boltiot import Sms,Email
import json,time
import bitcoin_conf as conf
from requests import request
#set your desirable limits
minimum_limit=4000
maximum_limit=5000
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
def get_bitcoin_price():
#here I am using only USD and INR you can add more by editing the URL here
URL = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD,INR"
try:
response=request("GET",URL)
print(response)
response=json.loads(response.text)
current_price={}
current_price['USD']=response["USD"]
current_price['INR']=response["INR"]
return current_price
except Exception as e:
print ("Error occured: Below are the details")
print (e)
while True:
price=get_bitcoin_price()
print("Bitcoin price in USD and INR is as follows :\n1. USD : "+str(price['USD'])+"\n2. INR : "+str(price['INR'])+"\n")
try:
if price['INR']>maximum_limit or price['INR']<minimum_limit:
print("Making request to Twilio to send a SMS")
response = sms.send_sms("Bitcoin price has crossed your set limits.\nCurrent bitcoin price in USD is " +str(price['USD'])+" and in INR is "+str(price['INR']))
print("Response received from Twilio is: " + str(response))
print("Status of SMS at Twilio is :" + str(response.status))
print("Making request to Mailgun to send an email")
response = mailer.send_email("Bitcoin_Price_Alert", "Bitcoin price has crossed your set limits.\nCurrent bitcoin price in USD is " +str(price['USD'])+" and in INR is "+str(price['INR']))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
except Exception as e:
print ("Error occured: Below are the details")
print (e)
time.sleep(10) #set the time you want to delay I have set it to 10sec