Skip to content

Instantly share code, notes, and snippets.

@sidward35
Last active February 3, 2021 04:03
Show Gist options
  • Save sidward35/70cda0fc14d674818121e7d0c2b68fa4 to your computer and use it in GitHub Desktop.
Save sidward35/70cda0fc14d674818121e7d0c2b68fa4 to your computer and use it in GitHub Desktop.
Fetches the current Bitcoin price in USD and saves it to a local file
import requests
import json
import time
from datetime import datetime
while True:
response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
price = response.json().get('bpi').get('USD').get('rate')
now = datetime.now()
dt_string = now.strftime("%m/%d/%Y %H:%M:%S")
result = '[' + dt_string + '] BTC Price: $' + price
print(result)
file1 = open('btc_price_history.txt', 'a')
file1.write(result+'\n')
file1.close()
time.sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment