[stock] Add a flag for verbose information
This commit is contained in:
parent
e6bc33db0e
commit
970ec7b319
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",
|
||||
|
127
app/stock.py
127
app/stock.py
@ -4,58 +4,41 @@ import requests
|
||||
|
||||
|
||||
def parse_message(msg):
|
||||
if len(msg.split()) > 1:
|
||||
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:
|
||||
res = ""
|
||||
for s in msg.split()[1:]:
|
||||
res = get_stock(s)
|
||||
embeds.append(get_stock(s, verbose=verbose))
|
||||
except:
|
||||
res = "```Please input valid shares: !stock [share_name]```"
|
||||
return res
|
||||
return "```Please input at least one valid share: !stock [share_name]```"
|
||||
bad_tickers.append(s)
|
||||
if bad_tickers:
|
||||
embeds.append(_make_error_embed(bad_tickers))
|
||||
return embeds
|
||||
|
||||
|
||||
def get_stock(share_name):
|
||||
share_name = share_name.upper()
|
||||
# Fake headers to make yahoo happy
|
||||
headers = {
|
||||
"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 = (
|
||||
"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"][
|
||||
"result"
|
||||
][0]
|
||||
|
||||
change_symbol = "+"
|
||||
embed_color = 2067276
|
||||
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
|
||||
|
||||
current_change = request["regularMarketPrice"] - request["regularMarketOpen"]
|
||||
if current_change < 0:
|
||||
change_symbol = "-"
|
||||
embed_color = 15158332
|
||||
meme_url = "https://i.kym-cdn.com/photos/images/facebook/002/021/567/635.png"
|
||||
|
||||
embed = discord.Embed(description="-------", color=embed_color, type="rich")
|
||||
embed.set_thumbnail(url=meme_url)
|
||||
embed.set_author(name=request["longName"])
|
||||
|
||||
embed.add_field(
|
||||
name="Current price", value="$%s" % request["regularMarketPrice"], inline=False
|
||||
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="Opening price", value="$%s" % request["regularMarketOpen"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Change since day open (as %)",
|
||||
value="$%.2f (%.7f%%)" % (current_change, current_change * 100 / request["regularMarketOpen"]),
|
||||
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"],
|
||||
@ -67,6 +50,7 @@ def get_stock(share_name):
|
||||
inline=False,
|
||||
)
|
||||
|
||||
if 'bid' in request and 'ask' in request:
|
||||
embed.add_field(
|
||||
name="Current bid price", value="$%s" % request["bid"], inline=False
|
||||
)
|
||||
@ -82,14 +66,71 @@ def get_stock(share_name):
|
||||
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 = {
|
||||
"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 = (
|
||||
"https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US®ion=US&corsDomain=finance.yahoo.com&symbols=%s"
|
||||
% share_name
|
||||
)
|
||||
response = requests.get(request_string, headers=headers).json()["quoteResponse"][
|
||||
"result"
|
||||
]
|
||||
|
||||
if not response:
|
||||
raise ValueError('Invalid symbol %s: empty response from Yahoo' % share_name)
|
||||
|
||||
request = response[0]
|
||||
|
||||
change_symbol = "+"
|
||||
embed_color = 2067276
|
||||
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
|
||||
|
||||
current_change = request["regularMarketPrice"] - request["regularMarketOpen"]
|
||||
if current_change < 0:
|
||||
change_symbol = "-"
|
||||
embed_color = 15158332
|
||||
meme_url = "https://i.kym-cdn.com/photos/images/facebook/002/021/567/635.png"
|
||||
|
||||
embed = discord.Embed(description="-------", color=embed_color, type="rich")
|
||||
embed.set_thumbnail(url=meme_url)
|
||||
embed.set_author(name=request["shortName"])
|
||||
|
||||
embed.add_field(
|
||||
name="Current price", value="$%s" % request["regularMarketPrice"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Opening price", value="$%s" % request["regularMarketOpen"], inline=False
|
||||
)
|
||||
embed.add_field(
|
||||
name="Change since day open (as %)",
|
||||
value="$%.2f (%.7f%%)" % (current_change, current_change * 100 / request["regularMarketOpen"]),
|
||||
inline=False,
|
||||
)
|
||||
|
||||
if verbose:
|
||||
embed = _add_verbose_fields(embed, request)
|
||||
|
||||
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