1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-16 07:16:40 +00:00

Mostly wrapped up #75

This commit is contained in:
Anson 2021-08-31 20:03:59 -07:00
parent dcce2cb95a
commit a583cad741
3 changed files with 56 additions and 2 deletions

View File

@ -36,7 +36,7 @@ class IEX_Symbol:
IEX API Token
"""
try:
self.IEX_TOKEN = os.environ["IEX"]
self.IEX_TOKEN = "pk_3c39d940736e47dabfdd47eb689a65be"
except KeyError:
self.IEX_TOKEN = ""
warning(
@ -485,6 +485,23 @@ class IEX_Symbol:
return pd.DataFrame()
def spark_reply(self, symbol: Stock) -> str:
quote = self.get(f"/stock/{symbol.id}/quote")
open_change = quote.get("changePercent", 0)
after_change = quote.get("extendedChangePercent", 0)
change = 0
if open_change:
change = change + open_change
if after_change:
change = change + after_change
change = change * 100
return f"`{symbol.tag}`: {quote['companyName']}, {change:.2f}%"
def trending(self) -> list[str]:
"""Gets current coins trending on IEX. Only returns when market is open.

View File

@ -338,6 +338,18 @@ class cg_Crypto:
return f"No information found for: {symbol}\nEither today is boring or the symbol does not exist."
def spark_reply(self, symbol: Coin) -> str:
change = self.get(
f"/simple/price",
params={
"ids": symbol.id,
"vs_currencies": self.vs_currency,
"include_24hr_change": "true",
},
)[symbol.id]["usd_24h_change"]
return f"`{symbol.tag}`: {symbol.name}, {change:.2f}%"
def trending(self) -> list[str]:
"""Gets current coins trending on coingecko

View File

@ -374,6 +374,31 @@ class Router:
return replies
def spark_reply(self, symbols: list[Symbol]) -> list[str]:
"""Gets change for each symbol and returns it in a compact format
Parameters
----------
symbols : list[str]
List of stock symbols
Returns
-------
list[str]
List of human readable strings.
"""
replies = []
for symbol in symbols:
if isinstance(symbol, Stock):
replies.append(self.stock.spark_reply(symbol))
elif isinstance(symbol, Coin):
replies.append(self.crypto.spark_reply(symbol))
else:
debug(f"{symbol} is not a Stock or Coin")
return replies
def trending(self) -> str:
"""Checks APIs for trending symbols.
@ -398,7 +423,7 @@ class Router:
][::-1][0:5]
for t in sorted_trending:
reply += self.price_reply(self.find_symbols(t))[0] + "\n"
reply += self.spark_reply(self.find_symbols(t))[0] + "\n"
if stocks:
reply += "\n\n💵Trending Stocks:\n`"