I use an API to have an update of the Bitcoin price.
The following code give me a lot of informations :
resp = requests.get('https://www.alphavantage.co/query', params={
'function': 'CURRENCY_EXCHANGE_RATE',
'from_currency': 'BTC',
'to_currency': 'EUR',
'apikey': AV_API_KEY
})
resp
resp.json()
output :
{'Realtime Currency Exchange Rate': {'1. From_Currency Code': 'BTC',
'2. From_Currency Name': 'Bitcoin',
'3. To_Currency Code': 'EUR',
'4. To_Currency Name': 'Euro',
'5. Exchange Rate': '16006.64235500',
'6. Last Refreshed': '2020-11-30 14:12:04',
'7. Time Zone': 'UTC',
'8. Bid Price': '16006.64235500',
'9. Ask Price': '16006.65069000'}}
I want to only have the 5. Exchange rate and to update the price every 10 seconds, so I tried this :
resp = requests.get('https://www.alphavantage.co/query', params={
'function': 'CURRENCY_EXCHANGE_RATE',
'from_currency': 'BTC',
'to_currency': 'EUR',
'apikey': AV_API_KEY
})
resp
resp.json()
a=resp.json()
def update_price_10sec():
print(a.get('Realtime Currency Exchange Rate').get('5. Exchange Rate'))
while True:
update_price_10sec()
time.sleep(10) #make function to sleep for 10 seconds
output is then :
16006.64235500
16006.64235500
16006.64235500
16006.64235500
16006.64235500
(etc...)
So the problem is that the price is no more updated, and every 10 sec I have a copy paste of the last known price. Do someone know how can I fix it ?
Seem you need to put the request inside the while loop.
def GET():
resp = requests.get('https://www.alphavantage.co/query', params={
'function': 'CURRENCY_EXCHANGE_RATE',
'from_currency': 'BTC',
'to_currency': 'EUR',
'apikey': AV_API_KEY
})
return resp.json()
def update_price_10sec(a):
print(a.get('Realtime Currency Exchange Rate').get('5. Exchange Rate'))
while True:
update_price_10sec(GET())
time.sleep(10) #make function to sleep for 10 seconds