removing code re-use

This commit is contained in:
Luke Robles 2023-05-28 16:02:44 -07:00
parent 7efbf4c494
commit d209fbb01d

View File

@ -34,15 +34,15 @@ def _add_verbose_fields(embed, request):
"""
embed.add_field(
name="Previous Close",
value="$%s" % request["price"]["regularMarketPreviousClose"]["raw"],
value="$%s" % request["regularMarketPreviousClose"]["raw"],
inline=False,
)
embed.add_field(
name="Change since prev. close (as %)",
value="$%.2f (%s%%)"
% (
request["price"]["regularMarketChange"]["raw"],
request["price"]["regularMarketChangePercent"]["raw"],
request["regularMarketChange"]["raw"],
request["regularMarketChangePercent"]["raw"],
),
inline=False,
)
@ -50,18 +50,17 @@ def _add_verbose_fields(embed, request):
if "bid" in request and "ask" in request:
embed.add_field(
name="Current bid price",
value="$%s" % request["price"]["bid"]["raw"],
value="$%s" % request["bid"]["raw"],
inline=False,
)
embed.add_field(
name="Current ask price",
value="$%s" % request["price"]["ask"]["raw"],
value="$%s" % request["ask"]["raw"],
inline=False,
)
embed.add_field(
name="Current bid-ask spread",
value="$%.2f"
% (request["price"]["bid"]["raw"] - request["price"]["ask"]["raw"]),
value="$%.2f" % (request["bid"]["raw"] - request["ask"]["raw"]),
inline=False,
)
@ -92,7 +91,7 @@ def get_stock(share_name, verbose=False):
}
request_string = (
# "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"
"https://query2.finance.yahoo.com/v11/finance/quoteSummary/%s?modules=price"
% share_name
)
response = requests.get(request_string, headers=headers).json()["quoteSummary"][
@ -102,7 +101,7 @@ def get_stock(share_name, verbose=False):
if not response:
raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name)
request = response[0]
request = response[0]["price"]
change_symbol = "+"
embed_color = 2067276
@ -110,8 +109,7 @@ def get_stock(share_name, verbose=False):
# If stock price has gone down since open, use red and a sad stonk meme
current_change = (
request["price"]["regularMarketPrice"]["raw"]
- request["price"]["regularMarketOpen"]["raw"]
request["regularMarketPrice"]["raw"] - request["regularMarketOpen"]["raw"]
)
if current_change < 0:
change_symbol = "-"
@ -120,24 +118,24 @@ def get_stock(share_name, verbose=False):
embed = discord.Embed(description="-------", color=embed_color, type="rich")
embed.set_thumbnail(url=meme_url)
embed.set_author(name=request["price"]["shortName"])
embed.set_author(name=request["shortName"])
if "postMarketPrice" in request["price"]:
if "postMarketPrice" in request:
embed.add_field(
name="After Hours price",
value="$%s" % request["price"]["postMarketPrice"]["raw"],
value="$%s" % request["postMarketPrice"]["raw"],
inline=False,
)
embed.add_field(
name="Current price",
value="$%s" % request["price"]["regularMarketPrice"]["raw"],
value="$%s" % request["regularMarketPrice"]["raw"],
inline=False,
)
embed.add_field(
name="Opening price",
value="$%s" % request["price"]["regularMarketOpen"]["raw"],
value="$%s" % request["regularMarketOpen"]["raw"],
inline=False,
)
embed.add_field(
@ -145,7 +143,7 @@ def get_stock(share_name, verbose=False):
value="$%.2f (%.7f%%)"
% (
current_change,
current_change * 100 / request["price"]["regularMarketOpen"]["raw"],
current_change * 100 / request["regularMarketOpen"]["raw"],
),
inline=False,
)
@ -156,7 +154,7 @@ def get_stock(share_name, verbose=False):
chart_url = "https://www.marketwatch.com/investing/stock"
if "-" in share_name:
chart_url = "https://coinmarketcap.com/currencies"
share_name = request["price"]["shortName"].split()[0].lower()
share_name = request["shortName"].split()[0].lower()
embed.add_field(
name="Link to stock price",
value="%s/%s" % (chart_url, share_name),