[stock] Add a flag for verbose information
This commit is contained in:
parent
c976dfda3c
commit
1653357418
12
app/bot.py
12
app/bot.py
@ -541,11 +541,15 @@ async def rat(ctx):
|
||||
@bot.command(name="stock")
|
||||
async def stock(ctx):
|
||||
|
||||
msg = ctx.message.content
|
||||
if len(msg.split()) < 2:
|
||||
import help_methods
|
||||
await ctx.send(help_methods.get_help_message("stock"))
|
||||
|
||||
import stock
|
||||
|
||||
result = stock.parse_message(ctx.message.content)
|
||||
|
||||
await ctx.send(embed=result)
|
||||
results = stock.parse_message(msg)
|
||||
for res in results:
|
||||
await ctx.send(embed=res)
|
||||
|
||||
|
||||
@bot.command(name="tts")
|
||||
|
@ -160,8 +160,8 @@ def get_help_message(method):
|
||||
"smug": ["Returns the URL for smug anime girl"],
|
||||
"source": ["Links you to the git repo with dale-bot's source code"],
|
||||
"stock": [
|
||||
"Returns basic stock information for the stock you entered.",
|
||||
"\nUsage: !stock AAPL TSLA",
|
||||
"Returns basic stock information for the stock you entered. Add -v or --verbose for additional stock info.",
|
||||
"\nUsage: !stock AAPL TSLA -v",
|
||||
],
|
||||
"trackdays": [
|
||||
"Returns the track schedule for the tracks we frequent (buttonwillow and thunderhill).\n",
|
||||
|
133
app/stock.py
133
app/stock.py
@ -4,18 +4,85 @@ import requests
|
||||
|
||||
|
||||
def parse_message(msg):
|
||||
if len(msg.split()) > 1:
|
||||
try:
|
||||
res = ""
|
||||
for s in msg.split()[1:]:
|
||||
res = get_stock(s)
|
||||
except:
|
||||
res = "```Please input valid shares: !stock [share_name]```"
|
||||
return res
|
||||
return "```Please input at least one valid share: !stock [share_name]```"
|
||||
split_msg = msg.split()
|
||||
verbose = False
|
||||
embeds = []
|
||||
bad_tickers = []
|
||||
if set(['-v', '--verbose']) & set(split_msg):
|
||||
verbose = True
|
||||
for s in split_msg[1:]:
|
||||
# Skip flags
|
||||
if s[0] != '-':
|
||||
try:
|
||||
embeds.append(get_stock(s, verbose=verbose))
|
||||
except:
|
||||
bad_tickers.append(s)
|
||||
if bad_tickers:
|
||||
embeds.append(_make_error_embed(bad_tickers))
|
||||
return embeds
|
||||
|
||||
|
||||
def get_stock(share_name):
|
||||
def _make_error_embed(symbols):
|
||||
embed = discord.Embed(
|
||||
title="Errors when querying symbol data",
|
||||
description="I was unable to find information from Yahoo's API for the following symbols:",
|
||||
)
|
||||
embed.add_field(
|
||||
name="Invalid symbols",
|
||||
value=", ".join(symbols),
|
||||
inline=False,
|
||||
)
|
||||
return embed
|
||||
|
||||
|
||||
def _add_verbose_fields(embed, request):
|
||||
"""
|
||||
Helper function to add verbose fields.
|
||||
"""
|
||||
embed.add_field(
|
||||
name="Previous Close",
|
||||
value="$%s" % request["regularMarketPreviousClose"],
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Change since prev. close (as %)",
|
||||
value="$%.2f (%s%%)" % (request["regularMarketChange"], request["regularMarketChangePercent"]),
|
||||
inline=False,
|
||||
)
|
||||
|
||||
if 'bid' in request and 'ask' in request:
|
||||
embed.add_field(
|
||||
name="Current bid price", value="$%s" % request["bid"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Current ask price", value="$%s" % request["ask"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Current bid-ask spread",
|
||||
value="$%.2f" % (request["bid"] - request["ask"]),
|
||||
inline=False,
|
||||
)
|
||||
|
||||
embed.add_field(
|
||||
name="Day's Range", value=request["regularMarketDayRange"], inline=False
|
||||
)
|
||||
|
||||
if 'marketCap' in request:
|
||||
embed.add_field(
|
||||
name="Market Cap", value="{:,}".format(request["marketCap"]), inline=False
|
||||
)
|
||||
|
||||
if 'sharesOutstanding' in request:
|
||||
embed.add_field(
|
||||
name="Shares Outstanding",
|
||||
value="{:,}".format(request["sharesOutstanding"]),
|
||||
inline=False,
|
||||
)
|
||||
|
||||
return embed
|
||||
|
||||
|
||||
def get_stock(share_name, verbose=False):
|
||||
share_name = share_name.upper()
|
||||
# Fake headers to make yahoo happy
|
||||
headers = {
|
||||
@ -25,9 +92,14 @@ def get_stock(share_name):
|
||||
"https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US®ion=US&corsDomain=finance.yahoo.com&symbols=%s"
|
||||
% share_name
|
||||
)
|
||||
request = requests.get(request_string, headers=headers).json()["quoteResponse"][
|
||||
response = requests.get(request_string, headers=headers).json()["quoteResponse"][
|
||||
"result"
|
||||
][0]
|
||||
]
|
||||
|
||||
if not response:
|
||||
raise ValueError('Invalid symbol %s: empty response from Yahoo' % share_name)
|
||||
|
||||
request = response[0]
|
||||
|
||||
change_symbol = "+"
|
||||
embed_color = 2067276
|
||||
@ -42,7 +114,7 @@ def get_stock(share_name):
|
||||
|
||||
embed = discord.Embed(description="-------", color=embed_color, type="rich")
|
||||
embed.set_thumbnail(url=meme_url)
|
||||
embed.set_author(name=request["longName"])
|
||||
embed.set_author(name=request["shortName"])
|
||||
|
||||
embed.add_field(
|
||||
name="Current price", value="$%s" % request["regularMarketPrice"], inline=False
|
||||
@ -56,40 +128,9 @@ def get_stock(share_name):
|
||||
inline=False,
|
||||
)
|
||||
|
||||
embed.add_field(
|
||||
name="Previous Close",
|
||||
value="$%s" % request["regularMarketPreviousClose"],
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Change since prev. close (as %)",
|
||||
value="$%.2f (%s%%)" % (request["regularMarketChange"], request["regularMarketChangePercent"]),
|
||||
inline=False,
|
||||
)
|
||||
if verbose:
|
||||
embed = _add_verbose_fields(embed, request)
|
||||
|
||||
embed.add_field(
|
||||
name="Current bid price", value="$%s" % request["bid"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Current ask price", value="$%s" % request["ask"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Current bid-ask spread",
|
||||
value="$%.2f" % (request["bid"] - request["ask"]),
|
||||
inline=False,
|
||||
)
|
||||
|
||||
embed.add_field(
|
||||
name="Day's Range", value=request["regularMarketDayRange"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Market Cap", value="{:,}".format(request["marketCap"]), inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Shares Outstanding",
|
||||
value="{:,}".format(request["sharesOutstanding"]),
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Link to stock price",
|
||||
value="https://finance.yahoo.com/quote/%s" % share_name,
|
||||
|
Loading…
x
Reference in New Issue
Block a user