chatgpt/letters/openGPT-btc-price-fetch.py
2022-12-17 22:39:21 +02:00

22 lines
674 B
Python

import requests
def fetch_price_data():
# Set up the URL and payload for the API request
source = 'http://api.binance.com/api/v3/avgPrice'
payload = {'symbol': 'BTCUSDT'}
# Send the API request and parse the response
response = requests.get(source, params=payload)
data = response.json()
mins = data['mins']
price = data['price']
# Store the data in the SQLite database
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO price_data (timestamp, value, symbol, source) VALUES (?, ?, ?, ?)', (time.time(), price, 'BTCUSDT', 'Binance'))
conn.commit()
conn.close()
return mins