From a583cad741da13d81b4af4c7d772628c82c83954 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 31 Aug 2021 20:03:59 -0700 Subject: [PATCH] Mostly wrapped up #75 --- IEX_Symbol.py | 19 ++++++++++++++++++- cg_Crypto.py | 12 ++++++++++++ symbol_router.py | 27 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/IEX_Symbol.py b/IEX_Symbol.py index 62a680f..b40c87b 100644 --- a/IEX_Symbol.py +++ b/IEX_Symbol.py @@ -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. diff --git a/cg_Crypto.py b/cg_Crypto.py index 00bcb75..155475a 100644 --- a/cg_Crypto.py +++ b/cg_Crypto.py @@ -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 diff --git a/symbol_router.py b/symbol_router.py index 66cb8be..6af6969 100644 --- a/symbol_router.py +++ b/symbol_router.py @@ -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`"