dragon-bot/app/stock.py
Luke Robles 99776dbae6
All checks were successful
Build and push / changes (push) Successful in 13s
Build and push / Lint-Python (push) Successful in 2s
Build and push / Build-and-Push-Docker (push) Successful in 1m13s
Build and push / sync-argocd-app (push) Successful in 3s
Build and push / post-failure-to-discord (push) Has been skipped
Build and push / post-success-to-discord (push) Successful in 1s
Use ruff and conert stock to fstrings
2025-05-07 15:23:32 -07:00

152 lines
4.3 KiB
Python
Executable File

import discord
import yfinance as yf
import os
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=f"${request['regularMarketPreviousClose']}",
inline=False,
)
embed.add_field(
name="Change since prev. close (as %)",
value=f"${request['regularMarketChange']:.2f} ({request['regularMarketChangePercent']}%)",
inline=False,
)
if "bid" in request and "ask" in request:
embed.add_field(
name="Current bid price",
value=f"${request['bid']}",
inline=False,
)
embed.add_field(
name="Current ask price",
value=f"${request['ask']}",
inline=False,
)
embed.add_field(
name="Current bid-ask spread",
value=f"${(request['bid'] - request['ask']):.2f}",
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=f"{request['marketCap']:,}", inline=False
)
if "sharesOutstanding" in request:
embed.add_field(
name="Shares Outstanding",
value=f"{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
current_price = request.get("currentPrice", request["ask"])
current_change = current_price - request["open"]
except Exception:
raise ValueError(f"Invalid symbol {share_name}: empty response from Yahoo")
# 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=f"${current_price:,.2f}",
inline=False,
)
embed.add_field(
name="Opening price",
value=f"${request['open']:,.2f}",
inline=False,
)
embed.add_field(
name="Change since day open (as %)",
value=f"${current_change:.2f} ({current_change * 100 / request['open']:.7f}%)",
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="Remember, stocks can go up 1000%, 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