49 lines
2.8 KiB
Python
49 lines
2.8 KiB
Python
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
|
|
if float(request['bid']) < float(request['regularMarketOpen']):
|
|
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['bid'], inline=False)
|
|
embed.add_field(name='Previous Close', value="$%s" % request['regularMarketPreviousClose'], inline=False)
|
|
embed.add_field(name='Opening price', value="$%s" % request['regularMarketOpen'], inline=False)
|
|
embed.add_field(name='Change', value="$%s" % request['regularMarketChange'], inline=False)
|
|
embed.add_field(name='Change percent', value="%s%%" % request['regularMarketChangePercent'], inline=False)
|
|
embed.add_field(name='Day Low', value="$%s" % request['regularMarketDayLow'], inline=False)
|
|
embed.add_field(name='Day High', value="$%s" % request['regularMarketDayHigh'], 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.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
|