try and except statements added

This commit is contained in:
Justin 2023-07-24 17:11:04 +08:00 committed by GitHub
parent 8e7d19b2a0
commit 3b1c5a7d55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -176,8 +176,10 @@ def updateStocks(api_key, logf):
data = response.json() data = response.json()
if len(data) > 0: if len(data) > 0:
for symbol in symbols: for symbol in symbols:
stock_info[data[symbol]['quote']['symbol']] = {'current': data[symbol]['quote']['latestPrice'], 'change': data[symbol]['quote']['change'], 'percent_change':data[symbol]['quote']['changePercent'] * 100} try:
stock_info[data[symbol]['quote']['symbol']] = {'current': data[symbol]['quote']['latestPrice'], 'change': data[symbol]['quote']['change'], 'percent_change':data[symbol]['quote']['changePercent'] * 100}
except:
pass
all_stocks_settings['symbols'] = stock_info all_stocks_settings['symbols'] = stock_info
with open('csv/stocks_settings.json', 'w+') as f: with open('csv/stocks_settings.json', 'w+') as f:
json.dump(all_stocks_settings, f) json.dump(all_stocks_settings, f)
@ -211,10 +213,12 @@ def updateStocks(api_key, logf):
# stock_info = {} # stock_info = {}
if len(data) > 0: if len(data) > 0:
for symbol in symbols: for symbol in symbols:
for stock in data['quoteResponse']['result']: try:
if stock['symbol'] == symbol: for stock in data['quoteResponse']['result']:
stock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': stock['regularMarketPrice'], 'change': stock['regularMarketChange'], 'percent_change':stock['regularMarketChangePercent']} if stock['symbol'] == symbol:
stock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': stock['regularMarketPrice'], 'change': stock['regularMarketChange'], 'percent_change':stock['regularMarketChangePercent']}
except:
pass
all_stocks_settings['symbols'] = stock_info all_stocks_settings['symbols'] = stock_info
with open('csv/stocks_settings.json', 'w+') as f: with open('csv/stocks_settings.json', 'w+') as f:
json.dump(all_stocks_settings, f) json.dump(all_stocks_settings, f)