import requests
import time
from datetime import datetime
BITCOIN_API_URL = 'https://api.coinmarketcap.com/v2/ticker/1/?convert=BRL'
IFTTT_WEBHOOKS_URL = 'https://maker.ifttt.com/trigger/{event}/with/key/<IFTTT_KEY>'
BITCOIN_PRICE_THRESHOLD = 10000
def get_latest_bitcoin_price():
response = requests.get(BITCOIN_API_URL)
response_json = response.json()
# Convert the price to a floating point number
return float(response_json['data']['quotes']['USD']['price'])
def post_ifttt_webhook(event, value):
# Payload that will be sent to IFTTT service
data = {'value1': value}
ifttt_event_url = IFTTT_WEBHOOKS_URL.format(event=event)
# Sends a HTTP POST request to the IFTTT webhook
requests.post(ifttt_event_url, json=data)
def format_bitcoin_history(bitcoin_history):
rows = []
for bitcoin_price in bitcoin_history:
# Formats the date into a string: '15.07.2018 00:11'
date = bitcoin_price['date'].strftime('%d.%m.%Y %H:%M')
price = bitcoin_price['price']
# <b> (bold) tag creates bolded text
# 15.07.2018 00:11: $<b>6240.93
row = '{}: $<b>{}</b>'.format(date, price)
rows.append(row)
# Use a <br> (break) tag to create a new line
# Join the rows delimited by <br> tag: row1<br>row2<br>
return '<br>'.join(rows)
def main():
bitcoin_history = []
while True:
price = get_latest_bitcoin_price()
date = datetime.now()
bitcoin_history.append({'date': date, 'price': price})
# Send an emergency notification
if price < BITCOIN_PRICE_THRESHOLD:
post_ifttt_webhook('bitcoin_price_emergency', price)
# Send a Telegram notification
# Once we have 5 items in bitcoin_history send an update
if len(bitcoin_history) == 5:
post_ifttt_webhook('bitcoin_price_update',
format_bitcoin_history(bitcoin_history))
# reset the history
bitcoin_history = []
# Sleep for 5 minutes
time.sleep(60)
if __name__ == '__main__':
main()