43 lines
1.9 KiB
Python
43 lines
1.9 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()
|
|
token = os.getenv('stock_api_key')
|
|
request_string = "https://fmpcloud.io/api/v3/quote/%s?apikey=%s" % (share_name, token)
|
|
request = requests.get(request_string).json()[0]
|
|
|
|
change_symbol = '+'
|
|
embed_color = 2067276
|
|
meme_url = 'https://i.ytimg.com/vi/if-2M3K1tqk/hqdefault.jpg'
|
|
if float(request['price']) < float(request['open']):
|
|
change_symbol = '-'
|
|
embed_color = 15158332
|
|
meme_url = 'https://i.ytimg.com/vi/E_XlA_IEzwM/hqdefault.jpg'
|
|
|
|
embed = discord.Embed(description='Stock info', color=embed_color, type="rich")
|
|
embed.set_thumbnail(url=meme_url)
|
|
embed.set_author(name=request['name'])
|
|
embed.add_field(name='Current Price', value=request['price'], inline=False)
|
|
embed.add_field(name='Previous Close', value=request['previousClose'], inline=False)
|
|
embed.add_field(name='Opening price', value=request['open'], inline=False)
|
|
embed.add_field(name='Change', value=request['change'], inline=False)
|
|
embed.add_field(name='Change percent', value="%s%%" % request['changesPercentage'], inline=False)
|
|
embed.add_field(name='Day Low', value=request['dayLow'], inline=False)
|
|
embed.add_field(name='Day High', value=request['dayHigh'], inline=False)
|
|
embed.set_footer(text="Pulled from https://fmpcloud.io", icon_url='https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/emojidex/112/chart-with-downwards-trend_1f4c9.png')
|
|
|
|
return embed |