Stop fuckin around with managing the yahoo api myself, just use a moddule that wraps it
This commit is contained in:
parent
6ea045f0ec
commit
2e7015aa1c
@ -5,9 +5,10 @@ gTTS
|
||||
httpx
|
||||
humanfriendly
|
||||
lxml
|
||||
markovify
|
||||
openai
|
||||
owotext
|
||||
pandas
|
||||
requests
|
||||
wolframalpha
|
||||
markovify
|
||||
yfinance
|
62
app/stock.py
62
app/stock.py
@ -1,5 +1,6 @@
|
||||
import discord
|
||||
import requests
|
||||
import yfinance as yf
|
||||
|
||||
|
||||
def parse_message(symbols, verbose):
|
||||
@ -34,15 +35,15 @@ def _add_verbose_fields(embed, request):
|
||||
"""
|
||||
embed.add_field(
|
||||
name="Previous Close",
|
||||
value="$%s" % request["regularMarketPreviousClose"]["raw"],
|
||||
value="$%s" % request["regularMarketPreviousClose"],
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Change since prev. close (as %)",
|
||||
value="$%.2f (%s%%)"
|
||||
% (
|
||||
request["regularMarketChange"]["raw"],
|
||||
request["regularMarketChangePercent"]["raw"],
|
||||
request["regularMarketChange"],
|
||||
request["regularMarketChangePercent"],
|
||||
),
|
||||
inline=False,
|
||||
)
|
||||
@ -50,17 +51,17 @@ def _add_verbose_fields(embed, request):
|
||||
if "bid" in request and "ask" in request:
|
||||
embed.add_field(
|
||||
name="Current bid price",
|
||||
value="$%s" % request["bid"]["raw"],
|
||||
value="$%s" % request["bid"],
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Current ask price",
|
||||
value="$%s" % request["ask"]["raw"],
|
||||
value="$%s" % request["ask"],
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
name="Current bid-ask spread",
|
||||
value="$%.2f" % (request["bid"]["raw"] - request["ask"]["raw"]),
|
||||
value="$%.2f" % (request["bid"] - request["ask"]),
|
||||
inline=False,
|
||||
)
|
||||
|
||||
@ -86,30 +87,32 @@ def _add_verbose_fields(embed, request):
|
||||
def get_stock(share_name, verbose=False):
|
||||
share_name = share_name.upper()
|
||||
# Fake headers to make yahoo happy
|
||||
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"
|
||||
}
|
||||
request_string = (
|
||||
"https://query2.finance.yahoo.com/v11/finance/quoteSummary/%s?modules=price"
|
||||
% share_name
|
||||
)
|
||||
response = requests.get(request_string, headers=headers).json()["quoteSummary"][
|
||||
"result"
|
||||
]
|
||||
# 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"
|
||||
# }
|
||||
# request_string = (
|
||||
# "https://query2.finance.yahoo.com/v11/finance/quoteSummary/%s?modules=price"
|
||||
# % share_name
|
||||
# )
|
||||
# response = requests.get(request_string, headers=headers).json()["quoteSummary"][
|
||||
# "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)
|
||||
|
||||
request = response[0]["price"]
|
||||
|
||||
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
|
||||
|
||||
current_change = (
|
||||
request["regularMarketPrice"]["raw"] - request["regularMarketOpen"]["raw"]
|
||||
)
|
||||
current_change = request["currentPrice"] - request["regularMarketOpen"]
|
||||
if current_change < 0:
|
||||
change_symbol = "-"
|
||||
embed_color = 15158332
|
||||
@ -119,22 +122,15 @@ def get_stock(share_name, verbose=False):
|
||||
embed.set_thumbnail(url=meme_url)
|
||||
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(
|
||||
name="Current price",
|
||||
value="$%s" % request["regularMarketPrice"]["raw"],
|
||||
value="$%s" % request["currentPrice"],
|
||||
inline=False,
|
||||
)
|
||||
|
||||
embed.add_field(
|
||||
name="Opening price",
|
||||
value="$%s" % request["regularMarketOpen"]["raw"],
|
||||
value="$%s" % request["regularMarketOpen"],
|
||||
inline=False,
|
||||
)
|
||||
embed.add_field(
|
||||
@ -142,7 +138,7 @@ def get_stock(share_name, verbose=False):
|
||||
value="$%.2f (%.7f%%)"
|
||||
% (
|
||||
current_change,
|
||||
current_change * 100 / request["regularMarketOpen"]["raw"],
|
||||
current_change * 100 / request["regularMarketOpen"],
|
||||
),
|
||||
inline=False,
|
||||
)
|
||||
@ -160,7 +156,7 @@ def get_stock(share_name, verbose=False):
|
||||
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%",
|
||||
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",
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user