Using free public APIs for price data
This commit is contained in:
parent
7161bbd7a4
commit
3fd1310833
@ -1104,42 +1104,66 @@ async def getPriceData():
|
|||||||
''' Stuff required for getPrices endpoint '''
|
''' Stuff required for getPrices endpoint '''
|
||||||
def updatePrices():
|
def updatePrices():
|
||||||
prices = {}
|
prices = {}
|
||||||
# apilayer
|
|
||||||
|
# USD -> INR
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
f"http://apilayer.net/api/live?access_key={apilayerAccesskey}")
|
f"https://api.exchangeratesapi.io/latest?base=USD&symbols=INR")
|
||||||
try:
|
try:
|
||||||
price = response.json()
|
price = response.json()
|
||||||
prices['USDINR'] = price['quotes']['USDINR']
|
prices['USDINR'] = price['quotes']['USDINR']
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print('Json parse error')
|
response = requests.get(
|
||||||
|
f"https://api.exchangerate-api.com/v4/latest/usd")
|
||||||
|
|
||||||
# bitpay
|
|
||||||
response = requests.get('https://bitpay.com/api/rates')
|
|
||||||
try:
|
|
||||||
bitcoinRates = response.json()
|
|
||||||
for currency in bitcoinRates:
|
|
||||||
if currency['code'] == 'USD':
|
|
||||||
prices['BTCUSD'] = currency['rate']
|
|
||||||
elif currency['code'] == 'INR':
|
|
||||||
prices['BTCINR'] = currency['rate']
|
|
||||||
except ValueError:
|
|
||||||
# coindesk
|
|
||||||
response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
|
|
||||||
try:
|
try:
|
||||||
price = response.json()
|
price = response.json()
|
||||||
prices['BTCUSD'] = price['bpi']['USD']['rate']
|
prices['USDINR'] = price['rates']['INR']
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print('Json parse error')
|
response = requests.get(
|
||||||
|
f"https://api.ratesapi.io/api/latest?base=USD&symbols=INR")
|
||||||
|
try:
|
||||||
|
price = response.json()
|
||||||
|
prices['USDINR'] = price['rates']['INR']
|
||||||
|
except ValueError:
|
||||||
|
# todo : add logger to the application
|
||||||
|
print('USD to Fiat APIs arent responding')
|
||||||
|
|
||||||
|
|
||||||
|
# Blockchain stuff : BTC,FLO -> USD,INR
|
||||||
# cryptocompare
|
response = requests.get(
|
||||||
response = requests.get('https://min-api.cryptocompare.com/data/histoday?fsym=FLO&tsym=USD&limit=1&aggregate=3&e=CCCAGG')
|
f"https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,flo&vs_currencies=usd,inr")
|
||||||
try:
|
try:
|
||||||
price = response.json()
|
price = response.json()
|
||||||
prices['FLOUSD'] = price['Data'][-1]['close']
|
prices['BTCUSD'] = price['bitcoin']['usd']
|
||||||
except ValueError:
|
prices['BTCINR'] = price['bitcoin']['inr']
|
||||||
print('Json parse error')
|
prices['FLOUSD'] = price['flo']['usd']
|
||||||
|
prices['FLOINR'] = price['flo']['inr']
|
||||||
|
except ValueError:
|
||||||
|
response1 = requests.get(
|
||||||
|
f"https://api.coinlore.net/api/ticker/?id=90")
|
||||||
|
response2 = requests.get(
|
||||||
|
f"https://api.coinlore.net/api/ticker/?id=67")
|
||||||
|
try:
|
||||||
|
price1 = response1.json()
|
||||||
|
price2 = response2.json()
|
||||||
|
prices['BTCUSD'] = price1[0]['price_usd']
|
||||||
|
prices['BTCINR'] = price1[0]['price_usd'] * prices['USDINR']
|
||||||
|
prices['FLOUSD'] = price2[0]['price_usd']
|
||||||
|
prices['FLOINR'] = price2[0]['price_usd'] * prices['USDINR']
|
||||||
|
except ValueError:
|
||||||
|
response1 = requests.get(
|
||||||
|
f"https://api.coinpaprika.com/v1/tickers/btc-bitcoin")
|
||||||
|
response2 = requests.get(
|
||||||
|
f"https://api.coinpaprika.com/v1/tickers/flo-flo")
|
||||||
|
try:
|
||||||
|
price1 = response1.json()
|
||||||
|
price2 = response2.json()
|
||||||
|
prices['BTCUSD'] = price1['quotes']['usd']['price']
|
||||||
|
prices['BTCINR'] = price1['quotes']['usd']['price'] * prices['USDINR']
|
||||||
|
prices['FLOUSD'] = price2['quotes']['usd']['price']
|
||||||
|
prices['FLOINR'] = price2['quotes']['usd']['price'] * prices['USDINR']
|
||||||
|
except ValueError:
|
||||||
|
# todo : add logger to the application
|
||||||
|
print('Blockchain to Fiat APIs arent responding')
|
||||||
|
|
||||||
|
|
||||||
# 3. update latest price data
|
# 3. update latest price data
|
||||||
@ -1163,8 +1187,9 @@ if not os.path.isfile(f"system.db"):
|
|||||||
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCBTC', 1)")
|
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCBTC', 1)")
|
||||||
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCUSD', -1)")
|
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCUSD', -1)")
|
||||||
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCINR', -1)")
|
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCINR', -1)")
|
||||||
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('USDINR', -1)")
|
|
||||||
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('FLOUSD', -1)")
|
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('FLOUSD', -1)")
|
||||||
|
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('FLOINR', -1)")
|
||||||
|
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('USDINR', -1)")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user