Skip to content

Instantly share code, notes, and snippets.

@sidward35
Created September 21, 2019 19:32
Show Gist options
  • Save sidward35/dbcb03cc102f62efcd1660e40fb8387b to your computer and use it in GitHub Desktop.
Save sidward35/dbcb03cc102f62efcd1660e40fb8387b to your computer and use it in GitHub Desktop.
Python script that sends IFTTT notifications when Bitcoin price drops below threshold
import requests
import time
#make sure you change the event name and key below
IFTTT_WEBHOOKS_URL = 'https://maker.ifttt.com/trigger/EVENT_NAME/with/key/YOUR_KEY_HERE'
BITCOIN_API_URL = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
BITCOIN_PRICE_THRESHOLD = 9500
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[0]['price_usd'])
def post_ifttt_webhook(event, value):
# The payload that will be sent to IFTTT service
data = {'value1': value}
# inserts our desired event
ifttt_event_url = IFTTT_WEBHOOKS_URL.format(event)
# Sends a HTTP POST request to the webhook URL
requests.post(ifttt_event_url, json=data)
def main():
while True:
price = get_latest_bitcoin_price()
# Send an emergency notification
if price < BITCOIN_PRICE_THRESHOLD:
post_ifttt_webhook('bitcoin_drop', price)
# Sleep for 5 minutes
# (For testing purposes you can set it to a lower number)
time.sleep(5 * 60)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment