crypto api caller refactor started
This commit is contained in:
parent
43d9ea7cca
commit
24e4e890ef
110
api_caller.py
110
api_caller.py
@ -27,45 +27,28 @@ def getInput(Block=False):
|
||||
msg = ''
|
||||
return msg
|
||||
|
||||
def readCSV(file_path, max_stocks):
|
||||
def readJSON(file_path, max_stocks):
|
||||
|
||||
symbols = []
|
||||
stock_info = {}
|
||||
f = open(file_path, 'r')
|
||||
CSV = csv.reader(f)
|
||||
next(CSV)
|
||||
i = 0
|
||||
for row in CSV:
|
||||
|
||||
if i < max_stocks:
|
||||
i += 1
|
||||
|
||||
try:
|
||||
symbol, current_price, opening_price = row
|
||||
symbols.append(symbol)
|
||||
stock_info[symbol] = [current_price, opening_price]
|
||||
except:
|
||||
symbol = row[0]
|
||||
symbols.append(symbol)
|
||||
stock_info[symbol] = []
|
||||
else:
|
||||
|
||||
break
|
||||
|
||||
f.close()
|
||||
stock_settings = json.load(open(file_path, 'r'))
|
||||
|
||||
stock_info = stock_settings['symbols']
|
||||
symbols = list(stock_info.keys())
|
||||
|
||||
return symbols, stock_info
|
||||
|
||||
def readCryptoCSV(file_path, max_crypto):
|
||||
def readCryptoJSON(file_path, max_crypto):
|
||||
|
||||
symbols = []
|
||||
names = []
|
||||
stock_info = {}
|
||||
f = open(file_path, 'r')
|
||||
CSV = csv.reader(f)
|
||||
next(CSV)
|
||||
|
||||
all_crypto_settings = json.load(open(file_path, 'r'))
|
||||
|
||||
i = 0
|
||||
unique_bases = []
|
||||
crypto_info = all_crypto_settings['symbols']
|
||||
|
||||
|
||||
crypto_info = {}
|
||||
for row in CSV:
|
||||
|
||||
if i < max_crypto:
|
||||
@ -76,7 +59,7 @@ def readCryptoCSV(file_path, max_crypto):
|
||||
symbol, name, base, current_price, opening_price = row
|
||||
symbols.append(symbol)
|
||||
names.append(name)
|
||||
stock_info[name] = [symbol, base, current_price, opening_price]
|
||||
coin_info[name] = [symbol, base, current_price, opening_price]
|
||||
if base not in unique_bases:
|
||||
unique_bases.append(base)
|
||||
except:
|
||||
@ -85,19 +68,19 @@ def readCryptoCSV(file_path, max_crypto):
|
||||
unique_bases.append(base)
|
||||
symbols.append(symbol)
|
||||
names.append(name)
|
||||
stock_info[name] = [symbol, base]
|
||||
coin_info[name] = [symbol, base]
|
||||
else:
|
||||
|
||||
break
|
||||
|
||||
f.close()
|
||||
|
||||
return names, stock_info, unique_bases
|
||||
return names, coin_info, unique_bases
|
||||
|
||||
def emptyInfo(symbols, stock_info):
|
||||
update = False
|
||||
for symbol in symbols:
|
||||
if len(stock_info[symbol]) == 1: # stock with no info
|
||||
if stock_info[symbol] == -1: # stock with no info
|
||||
update = True
|
||||
return update
|
||||
|
||||
@ -114,7 +97,7 @@ def updateStockPricesFinhubb():
|
||||
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
||||
|
||||
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
symbols, stock_info = readJSON('csv/tickers.csv', max_stocks)
|
||||
try:
|
||||
quotes = [finnhubClient.quote(symbol) for symbol in symbols]
|
||||
current_prices = [quote['c'] for quote in quotes]
|
||||
@ -148,7 +131,7 @@ def updateStockPrices():
|
||||
iexAPIkey = 'pk_d066d39789bd41caac209bca850a35db' #IEX
|
||||
|
||||
max_stocks = 200
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
symbols, stock_info = readJSON('csv/stocks_settings.json', max_stocks)
|
||||
try:
|
||||
|
||||
|
||||
@ -163,10 +146,11 @@ def updateStockPrices():
|
||||
querystring = '?chartIEXOnly=true&token='+iexAPIkey
|
||||
|
||||
intraday_request_url = host + intradayEndpoint + querystring
|
||||
|
||||
intraday_response = requests.get(intraday_request_url)
|
||||
|
||||
|
||||
intraday_response = requests.get(intraday_request_url)
|
||||
|
||||
|
||||
for i in range(len(intraday_response.json())):
|
||||
opn = intraday_response.json()[i]['open']
|
||||
if opn is not None:
|
||||
@ -179,13 +163,23 @@ def updateStockPrices():
|
||||
|
||||
opening_prices.append(opn)
|
||||
current_prices.append(current)
|
||||
|
||||
|
||||
stock_info = {}
|
||||
|
||||
|
||||
|
||||
|
||||
CSV = open('csv/tickers.csv', 'w+')
|
||||
CSV.write('name,current,opening\n')
|
||||
for i, symbol in enumerate(symbols):
|
||||
|
||||
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
|
||||
CSV.close()
|
||||
stock_info[symbol] = {'current': current_prices[i], 'opening': opening_prices[i]}
|
||||
|
||||
f = open('csv/stocks_settings.json', 'r')
|
||||
all_stocks_settings = json.load(f)
|
||||
f.close()
|
||||
all_stocks_settings['symbols'] = stock_info
|
||||
|
||||
json.dump(all_stocks_settings, open('csv/stocks_settings.json', 'w+'))
|
||||
|
||||
|
||||
|
||||
@ -198,20 +192,34 @@ def updateStockPrices():
|
||||
logf.write('. type: ' + str(exc_type))
|
||||
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
|
||||
|
||||
|
||||
|
||||
|
||||
def updateCrypto():
|
||||
coingecko_client = CoinGeckoAPI()
|
||||
symbol_base, coin_info = readJSON('csv/crypto_settings.json', max_crypto)
|
||||
|
||||
symbols = [sb.split(',')[0] for sb in symbol_base]
|
||||
bases = [sb.split(',')[1] for sb in symbol_base]
|
||||
unique_bases = list(set(bases))
|
||||
coins = []
|
||||
#coins['symbol, base'].keys() = ['name','current','24hr change']
|
||||
|
||||
orderd_syms = [] # so that ymbols and coins in same order
|
||||
# convert symbols to ids for coingecko
|
||||
for coin in coingecko_client.get_coins_list():
|
||||
if coin['symbol'].upper() in symbols:
|
||||
ordered_syms.append(coin['symbol'].upper())
|
||||
if coin['id'] != 'binance-peg-cardano': # hackaround for two coins with symbol ada
|
||||
coins.append(coin['id'])
|
||||
|
||||
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_crypto)
|
||||
crypto_info = {}
|
||||
print(coins)
|
||||
try:
|
||||
response = coingecko_client.get_price(ids=','.join(coins), vs_currencies = unique_bases, include_24hr_change=True)
|
||||
CSV = open('csv/crypto.csv', 'w+')
|
||||
CSV.write('symbol,name,base,current,24hr change\n')
|
||||
|
||||
|
||||
for coin in coins:
|
||||
info = coin_info[coin]
|
||||
#coin_info[name] = [symbol, base, current_price, opening_price]
|
||||
#info = coin_info[coin]
|
||||
|
||||
CSV.write(info[0] + ',' + coin + ',' + info[1] + ',' +str(response[coin][info[1]]) + ',' + str(response[coin]['usd_24h_change']) + '\n')
|
||||
CSV.close()
|
||||
@ -232,6 +240,7 @@ def updateNews():
|
||||
#load user settings
|
||||
headlines = []
|
||||
settings = json.load(open('csv/news_settings.json', 'r'))
|
||||
|
||||
|
||||
for setting in settings:
|
||||
h = newsapi.get_top_headlines(**setting)
|
||||
@ -514,7 +523,7 @@ def checkStocks(last_update, update_frequency):
|
||||
closing = NY_time.replace(hour=16, minute=0, second=0, microsecond=0).replace(tzinfo=None)
|
||||
|
||||
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
symbols, stock_info = readJSON('csv/stocks_settings.json', max_stocks)
|
||||
|
||||
updated = False
|
||||
|
||||
@ -554,7 +563,8 @@ if __name__ == '__main__':
|
||||
max_stocks = 200
|
||||
max_crypto = 100
|
||||
|
||||
|
||||
updateCrypto()
|
||||
sys.exit()
|
||||
|
||||
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
|
||||
|
||||
|
@ -1,4 +0,0 @@
|
||||
symbol,name,base,current,24hr change
|
||||
BTC,bitcoin,usd,47731,-1.6366277806338174
|
||||
ETH,ethereum,gbp,2452.22,-3.272526724697188
|
||||
DOGE,dogecoin,usd,0.238755,-1.8543425846579291
|
|
@ -1 +1 @@
|
||||
{"feature": "Stocks", "speed": "slow", "animation": "continuous", "percent": true, "point": false, "logos": true, "chart": false, "title": true, "symbols": {"APL": 0.0, "GOOG": 0.0, "FB": -1, "ROKU": -1, "TGT": -1, "TSLA": -1, "SNAP": -1, "NFLX": -1, "MSFT": -1}}
|
||||
{"feature": "Stocks", "speed": "medium", "animation": "continuous", "percent": true, "point": false, "logos": true, "chart": false, "title": true, "symbols": {"MSFT": {"current": 300.03, "opening": 298.845}, "GOOG": {"current": 2839.75, "opening": 2832}}}
|
5
log.txt
5
log.txt
@ -0,0 +1,5 @@
|
||||
'bitcoin'. file: api_caller.py. line: 221. type: <class 'KeyError'>
|
||||
Traceback (most recent call last):
|
||||
File "api_caller.py", line 221, in updateCrypto
|
||||
info = coin_info[coin]
|
||||
KeyError: 'bitcoin'
|
@ -396,7 +396,7 @@ def save_trade_settings(input_settings):
|
||||
current_settings['animation'] = input_settings['animation'].lower()
|
||||
current_settings['percent'] = input_settings['percent']
|
||||
current_settings['point'] = input_settings['point']
|
||||
current_settings['no_logos'] = input_settings['no_logos']
|
||||
current_settings['logos'] = input_settings['logos']
|
||||
current_settings['chart'] = input_settings['chart']
|
||||
current_settings['title'] = input_settings['title']
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user