Fixing the /stock command to use a differnet yahoo api

This commit is contained in:
Luke Robles 2023-05-28 16:00:00 -07:00
parent 3294ef6b3b
commit 9cad312b3e
2 changed files with 34 additions and 16 deletions

0
app/star_citizen.py Executable file → Normal file
View File

View File

@ -8,7 +8,8 @@ def parse_message(symbols, verbose):
for s in symbols.split(): for s in symbols.split():
try: try:
embeds.append(get_stock(s, verbose=verbose)) embeds.append(get_stock(s, verbose=verbose))
except: except Exception as e:
print(e)
bad_tickers.append(s) bad_tickers.append(s)
if bad_tickers: if bad_tickers:
embeds.append(_make_error_embed(bad_tickers)) embeds.append(_make_error_embed(bad_tickers))
@ -34,26 +35,34 @@ def _add_verbose_fields(embed, request):
""" """
embed.add_field( embed.add_field(
name="Previous Close", name="Previous Close",
value="$%s" % request["regularMarketPreviousClose"], value="$%s" % request["price"]["regularMarketPreviousClose"]["raw"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Change since prev. close (as %)", name="Change since prev. close (as %)",
value="$%.2f (%s%%)" value="$%.2f (%s%%)"
% (request["regularMarketChange"], request["regularMarketChangePercent"]), % (
request["price"]["regularMarketChange"]["raw"],
request["price"]["regularMarketChangePercent"]["raw"],
),
inline=False, inline=False,
) )
if "bid" in request and "ask" in request: if "bid" in request and "ask" in request:
embed.add_field( embed.add_field(
name="Current bid price", value="$%s" % request["bid"], inline=False name="Current bid price",
value="$%s" % request["price"]["bid"]["raw"],
inline=False,
) )
embed.add_field( embed.add_field(
name="Current ask price", value="$%s" % request["ask"], inline=False name="Current ask price",
value="$%s" % request["price"]["ask"]["raw"],
inline=False,
) )
embed.add_field( embed.add_field(
name="Current bid-ask spread", name="Current bid-ask spread",
value="$%.2f" % (request["bid"] - request["ask"]), value="$%.2f"
% (request["price"]["bid"]["raw"] - request["price"]["ask"]["raw"]),
inline=False, inline=False,
) )
@ -83,10 +92,11 @@ def get_stock(share_name, verbose=False):
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
} }
request_string = ( request_string = (
"https://query1.finance.yahoo.com/v6/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=%s" # "https://query2.finance.yahoo.com/v11/finance/quoteSummary&symbols=%s?modules=price"
"https://query2.finance.yahoo.com/v11/finance/quoteSummary/%s?modules=price,summaryDetail,defaultKeyStatistics"
% share_name % share_name
) )
response = requests.get(request_string, headers=headers).json()["quoteResponse"][ response = requests.get(request_string, headers=headers).json()["quoteSummary"][
"result" "result"
] ]
@ -100,7 +110,10 @@ def get_stock(share_name, verbose=False):
meme_url = "https://i.ytimg.com/vi/if-2M3K1tqk/hqdefault.jpg" meme_url = "https://i.ytimg.com/vi/if-2M3K1tqk/hqdefault.jpg"
# If stock price has gone down since open, use red and a sad stonk meme # If stock price has gone down since open, use red and a sad stonk meme
current_change = request["regularMarketPrice"] - request["regularMarketOpen"] current_change = (
request["price"]["regularMarketPrice"]["raw"]
- request["price"]["regularMarketOpen"]["raw"]
)
if current_change < 0: if current_change < 0:
change_symbol = "-" change_symbol = "-"
embed_color = 15158332 embed_color = 15158332
@ -108,28 +121,33 @@ def get_stock(share_name, verbose=False):
embed = discord.Embed(description="-------", color=embed_color, type="rich") embed = discord.Embed(description="-------", color=embed_color, type="rich")
embed.set_thumbnail(url=meme_url) embed.set_thumbnail(url=meme_url)
embed.set_author(name=request["shortName"]) embed.set_author(name=request["price"]["shortName"])
if "postMarketPrice" in request: if "postMarketPrice" in request["price"]:
embed.add_field( embed.add_field(
name="After Hours price", name="After Hours price",
value="$%s" % request["postMarketPrice"], value="$%s" % request["price"]["postMarketPrice"]["raw"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Current price", name="Current price",
value="$%s" % request["regularMarketPrice"], value="$%s" % request["price"]["regularMarketPrice"]["raw"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Opening price", value="$%s" % request["regularMarketOpen"], inline=False name="Opening price",
value="$%s" % request["price"]["regularMarketOpen"]["raw"],
inline=False,
) )
embed.add_field( embed.add_field(
name="Change since day open (as %)", name="Change since day open (as %)",
value="$%.2f (%.7f%%)" value="$%.2f (%.7f%%)"
% (current_change, current_change * 100 / request["regularMarketOpen"]), % (
current_change,
current_change * 100 / request["price"]["regularMarketOpen"]["raw"],
),
inline=False, inline=False,
) )
@ -139,7 +157,7 @@ def get_stock(share_name, verbose=False):
chart_url = "https://www.marketwatch.com/investing/stock" chart_url = "https://www.marketwatch.com/investing/stock"
if "-" in share_name: if "-" in share_name:
chart_url = "https://coinmarketcap.com/currencies" chart_url = "https://coinmarketcap.com/currencies"
share_name = request["shortName"].split()[0].lower() share_name = request["price"]["shortName"].split()[0].lower()
embed.add_field( embed.add_field(
name="Link to stock price", name="Link to stock price",
value="%s/%s" % (chart_url, share_name), value="%s/%s" % (chart_url, share_name),