Stop fuckin around with managing the yahoo api myself, just use a moddule that wraps it

This commit is contained in:
Luke Robles 2023-08-08 14:35:34 -07:00
parent 6ea045f0ec
commit 2e7015aa1c
2 changed files with 31 additions and 34 deletions

View File

@ -5,9 +5,10 @@ gTTS
httpx httpx
humanfriendly humanfriendly
lxml lxml
markovify
openai openai
owotext owotext
pandas pandas
requests requests
wolframalpha wolframalpha
markovify yfinance

View File

@ -1,5 +1,6 @@
import discord import discord
import requests import requests
import yfinance as yf
def parse_message(symbols, verbose): def parse_message(symbols, verbose):
@ -34,15 +35,15 @@ def _add_verbose_fields(embed, request):
""" """
embed.add_field( embed.add_field(
name="Previous Close", name="Previous Close",
value="$%s" % request["regularMarketPreviousClose"]["raw"], value="$%s" % request["regularMarketPreviousClose"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Change since prev. close (as %)", name="Change since prev. close (as %)",
value="$%.2f (%s%%)" value="$%.2f (%s%%)"
% ( % (
request["regularMarketChange"]["raw"], request["regularMarketChange"],
request["regularMarketChangePercent"]["raw"], request["regularMarketChangePercent"],
), ),
inline=False, inline=False,
) )
@ -50,17 +51,17 @@ def _add_verbose_fields(embed, request):
if "bid" in request and "ask" in request: if "bid" in request and "ask" in request:
embed.add_field( embed.add_field(
name="Current bid price", name="Current bid price",
value="$%s" % request["bid"]["raw"], value="$%s" % request["bid"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Current ask price", name="Current ask price",
value="$%s" % request["ask"]["raw"], value="$%s" % request["ask"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Current bid-ask spread", name="Current bid-ask spread",
value="$%.2f" % (request["bid"]["raw"] - request["ask"]["raw"]), value="$%.2f" % (request["bid"] - request["ask"]),
inline=False, inline=False,
) )
@ -86,30 +87,32 @@ def _add_verbose_fields(embed, request):
def get_stock(share_name, verbose=False): def get_stock(share_name, verbose=False):
share_name = share_name.upper() share_name = share_name.upper()
# Fake headers to make yahoo happy # Fake headers to make yahoo happy
headers = { # headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
} # }
request_string = ( # request_string = (
"https://query2.finance.yahoo.com/v11/finance/quoteSummary/%s?modules=price" # "https://query2.finance.yahoo.com/v11/finance/quoteSummary/%s?modules=price"
% share_name # % share_name
) # )
response = requests.get(request_string, headers=headers).json()["quoteSummary"][ # response = requests.get(request_string, headers=headers).json()["quoteSummary"][
"result" # "result"
] # ]
if not response: # if not response:
# raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name)
# request = response[0]["price"]
try:
request = yf.Ticker(share_name).info
except requests.exceptions.HTTPError:
raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name) raise ValueError("Invalid symbol %s: empty response from Yahoo" % share_name)
request = response[0]["price"]
change_symbol = "+" change_symbol = "+"
embed_color = 2067276 embed_color = 2067276
meme_url = "https://i.ytimg.com/vi/if-2M3K1tqk/hqdefault.jpg" 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 stock price has gone down since open, use red and a sad stonk meme
current_change = ( current_change = request["currentPrice"] - request["regularMarketOpen"]
request["regularMarketPrice"]["raw"] - request["regularMarketOpen"]["raw"]
)
if current_change < 0: if current_change < 0:
change_symbol = "-" change_symbol = "-"
embed_color = 15158332 embed_color = 15158332
@ -119,22 +122,15 @@ def get_stock(share_name, verbose=False):
embed.set_thumbnail(url=meme_url) embed.set_thumbnail(url=meme_url)
embed.set_author(name=request["shortName"]) embed.set_author(name=request["shortName"])
if "raw" in request["postMarketPrice"]:
embed.add_field(
name="After Hours price",
value="$%s" % request["postMarketPrice"]["raw"],
inline=False,
)
embed.add_field( embed.add_field(
name="Current price", name="Current price",
value="$%s" % request["regularMarketPrice"]["raw"], value="$%s" % request["currentPrice"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
name="Opening price", name="Opening price",
value="$%s" % request["regularMarketOpen"]["raw"], value="$%s" % request["regularMarketOpen"],
inline=False, inline=False,
) )
embed.add_field( embed.add_field(
@ -142,7 +138,7 @@ def get_stock(share_name, verbose=False):
value="$%.2f (%.7f%%)" value="$%.2f (%.7f%%)"
% ( % (
current_change, current_change,
current_change * 100 / request["regularMarketOpen"]["raw"], current_change * 100 / request["regularMarketOpen"],
), ),
inline=False, inline=False,
) )
@ -160,7 +156,7 @@ def get_stock(share_name, verbose=False):
inline=False, inline=False,
) )
embed.set_footer( embed.set_footer(
text="Pulled from https://finance.yahoo.com\nRemember, stocks can go up 100000%, but they can only go down 100%", 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", icon_url="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/emojidex/112/chart-with-downwards-trend_1f4c9.png",
) )