104 lines
3.4 KiB
Python
Executable File
104 lines
3.4 KiB
Python
Executable File
import discord
|
|
import os
|
|
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]```"
|
|
|
|
|
|
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.ytimg.com/vi/E_XlA_IEzwM/hqdefault.jpg"
|
|
|
|
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
|
|
)
|
|
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,
|
|
)
|
|
|
|
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,
|
|
)
|
|
|
|
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,
|
|
inline=False,
|
|
)
|
|
embed.set_footer(
|
|
text="Pulled from https://finance.yahoo.com\nRemember, stocks can go up 100000%, but they can only go down 100%",
|
|
icon_url="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/emojidex/112/chart-with-downwards-trend_1f4c9.png",
|
|
)
|
|
|
|
return embed
|