removing code re-use
This commit is contained in:
parent
065a1058b7
commit
36715eded5
34
app/stock.py
34
app/stock.py
@ -34,15 +34,15 @@ def _add_verbose_fields(embed, request):
|
|||||||
"""
|
"""
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Previous Close",
|
name="Previous Close",
|
||||||
value="$%s" % request["price"]["regularMarketPreviousClose"]["raw"],
|
value="$%s" % request["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["price"]["regularMarketChange"]["raw"],
|
request["regularMarketChange"]["raw"],
|
||||||
request["price"]["regularMarketChangePercent"]["raw"],
|
request["regularMarketChangePercent"]["raw"],
|
||||||
),
|
),
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
@ -50,18 +50,17 @@ def _add_verbose_fields(embed, request):
|
|||||||
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",
|
name="Current bid price",
|
||||||
value="$%s" % request["price"]["bid"]["raw"],
|
value="$%s" % request["bid"]["raw"],
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Current ask price",
|
name="Current ask price",
|
||||||
value="$%s" % request["price"]["ask"]["raw"],
|
value="$%s" % request["ask"]["raw"],
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Current bid-ask spread",
|
name="Current bid-ask spread",
|
||||||
value="$%.2f"
|
value="$%.2f" % (request["bid"]["raw"] - request["ask"]["raw"]),
|
||||||
% (request["price"]["bid"]["raw"] - request["price"]["ask"]["raw"]),
|
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -92,7 +91,7 @@ def get_stock(share_name, verbose=False):
|
|||||||
}
|
}
|
||||||
request_string = (
|
request_string = (
|
||||||
# "https://query2.finance.yahoo.com/v11/finance/quoteSummary&symbols=%s?modules=price"
|
# "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
|
% share_name
|
||||||
)
|
)
|
||||||
response = requests.get(request_string, headers=headers).json()["quoteSummary"][
|
response = requests.get(request_string, headers=headers).json()["quoteSummary"][
|
||||||
@ -102,7 +101,7 @@ def get_stock(share_name, verbose=False):
|
|||||||
if not response:
|
if not response:
|
||||||
raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name)
|
raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name)
|
||||||
|
|
||||||
request = response[0]
|
request = response[0]["price"]
|
||||||
|
|
||||||
change_symbol = "+"
|
change_symbol = "+"
|
||||||
embed_color = 2067276
|
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
|
# If stock price has gone down since open, use red and a sad stonk meme
|
||||||
|
|
||||||
current_change = (
|
current_change = (
|
||||||
request["price"]["regularMarketPrice"]["raw"]
|
request["regularMarketPrice"]["raw"] - request["regularMarketOpen"]["raw"]
|
||||||
- request["price"]["regularMarketOpen"]["raw"]
|
|
||||||
)
|
)
|
||||||
if current_change < 0:
|
if current_change < 0:
|
||||||
change_symbol = "-"
|
change_symbol = "-"
|
||||||
@ -120,24 +118,24 @@ 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["price"]["shortName"])
|
embed.set_author(name=request["shortName"])
|
||||||
|
|
||||||
if "postMarketPrice" in request["price"]:
|
if "postMarketPrice" in request:
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="After Hours price",
|
name="After Hours price",
|
||||||
value="$%s" % request["price"]["postMarketPrice"]["raw"],
|
value="$%s" % request["postMarketPrice"]["raw"],
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Current price",
|
name="Current price",
|
||||||
value="$%s" % request["price"]["regularMarketPrice"]["raw"],
|
value="$%s" % request["regularMarketPrice"]["raw"],
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Opening price",
|
name="Opening price",
|
||||||
value="$%s" % request["price"]["regularMarketOpen"]["raw"],
|
value="$%s" % request["regularMarketOpen"]["raw"],
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
@ -145,7 +143,7 @@ def get_stock(share_name, verbose=False):
|
|||||||
value="$%.2f (%.7f%%)"
|
value="$%.2f (%.7f%%)"
|
||||||
% (
|
% (
|
||||||
current_change,
|
current_change,
|
||||||
current_change * 100 / request["price"]["regularMarketOpen"]["raw"],
|
current_change * 100 / request["regularMarketOpen"]["raw"],
|
||||||
),
|
),
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
@ -156,7 +154,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["price"]["shortName"].split()[0].lower()
|
share_name = request["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),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user