import discord import yfinance as yf import os import pprint def parse_message(symbols, verbose): embeds = [] bad_tickers = [] for s in symbols.split(): try: embeds.append(get_stock(s, verbose=verbose)) except Exception: bad_tickers.append(s) if bad_tickers: embeds.append(_make_error_embed(bad_tickers)) return embeds 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, fast=False): share_name = share_name.upper() try: os.mkdir("/root/.cache/py-yfinance") except OSError: pass try: if fast: request = yf.Ticker(share_name).fast_info current_change = request["lastPrice"] - request["open"] current_price = request["lastPrice"] else: request = yf.Ticker(share_name).info # pprint.pprint(request) current_change = request["ask"] - request["open"] current_price = request["ask"] except Exception as e: # print(e) raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name) # If stock price has gone down since open, use red and a sad stonk meme # change_symbol = "+" embed_color = 2067276 meme_url = "https://i.ytimg.com/vi/if-2M3K1tqk/hqdefault.jpg" 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=share_name) embed.add_field( name="Current price", value="$" + format(current_price, ",.2f"), inline=False, ) embed.add_field( name="Opening price", value="$" + format(request["open"], ",.2f"), inline=False, ) embed.add_field( name="Change since day open (as %)", value="$%.2f (%.7f%%)" % ( current_change, current_change * 100 / request["open"], ), inline=False, ) if verbose: embed = _add_verbose_fields(embed, request) chart_url = "https://finance.yahoo.com/quote" if "-" in share_name: chart_url = "https://coinmarketcap.com/currencies" share_name = share_name.split()[0].lower() embed.add_field( name="Link to stock price", value=f"{chart_url}/{share_name}/", inline=False, ) embed.set_footer( text="Pulled from https://finance.yahoo.com\nRemember, stocks can go up 10000%, 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