1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-16 15:17:28 +00:00

added error handling for if ticker doesnt exist

This commit is contained in:
Anson 2019-05-28 05:27:50 -07:00
parent a195c87b6e
commit 230f6fdeac

View File

@ -26,19 +26,22 @@ def tickerDataReply(tickers: list):
IEXURL = ( IEXURL = (
f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={cred.secret}" f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={cred.secret}"
) )
with urllib.request.urlopen(IEXURL) as url: try:
IEXData = json.loads(url.read().decode()) with urllib.request.urlopen(IEXURL) as url:
IEXData = json.loads(url.read().decode())
reply = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**" reply = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**"
# Determine wording of change text # Determine wording of change text
change = round(IEXData["changePercent"] * 100, 2) change = round(IEXData["changePercent"] * 100, 2)
if change > 0: if change > 0:
reply += f", the stock is currently **up {change}%**" reply += f", the stock is currently **up {change}%**"
elif change < 0: elif change < 0:
reply += f", the stock is currently **down {change}%**" reply += f", the stock is currently **down {change}%**"
else: else:
reply += ", the stock hasn't shown any movement today." reply += ", the stock hasn't shown any movement today."
except:
reply = f"The ticker: {ticker} was not found."
tickerReplies[ticker] = reply tickerReplies[ticker] = reply