From 082c42c39ff59da903ac43e594ec0049929b11bc Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 26 Aug 2021 19:12:38 +0000 Subject: [PATCH] Bot now keeps track of symbols for own trending --- Symbol.py | 4 ++++ symbol_router.py | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Symbol.py b/Symbol.py index db04b4a..e0629a3 100644 --- a/Symbol.py +++ b/Symbol.py @@ -8,6 +8,7 @@ class Symbol: symbol: What the user calls it. ie tsla or btc id: What the api expects. ie tsla or bitcoin name: Human readable. ie Tesla or Bitcoin + tag: Uppercase tag to call the symbol. ie $TSLA or $$BTC """ currency = "usd" @@ -17,6 +18,7 @@ class Symbol: self.symbol = symbol self.id = symbol self.name = symbol + self.tag = "$" + symbol def __repr__(self) -> str: return f"<{self.__class__.__name__} instance of {self.id} at {id(self)}>" @@ -32,6 +34,7 @@ class Stock(Symbol): self.symbol = symbol self.id = symbol self.name = "$" + symbol.upper() + self.tag = "$" + symbol.upper() # Used by Coin to change symbols for ids @@ -44,6 +47,7 @@ class Coin(Symbol): @functools.cache def __init__(self, symbol: str) -> None: self.symbol = symbol + self.tag = "$$" + symbol.upper() self.get_data() def get_data(self) -> None: diff --git a/symbol_router.py b/symbol_router.py index 047a2f2..0560e7f 100644 --- a/symbol_router.py +++ b/symbol_router.py @@ -18,6 +18,7 @@ class Router: STOCK_REGEX = "(?:^|[^\\$])\\$([a-zA-Z.]{1,6})" CRYPTO_REGEX = "[$]{2}([a-zA-Z]{1,20})" searched_symbols = {} + trending_count = {} def __init__(self): self.stock = IEX_Symbol() @@ -54,8 +55,12 @@ class Router: if symbols: info(symbols) + for symbol in symbols: + self.trending_count[symbol.tag] = ( + self.trending_count.get(symbol.tag, 0) + 1 + ) - return symbols + return symbols def status(self, bot_resp) -> str: """Checks for any issues with APIs. @@ -364,7 +369,19 @@ class Router: stocks = self.stock.trending() coins = self.crypto.trending() - reply = "Trending Stocks:\n" + reply = "" + + reply += "Trending on the Stock Bot:\n" + reply += "-" * len("Trending on the Stock Bot:") + "\n" + + sorted_trending = [ + s[0] for s in sorted(self.trending_count.items(), key=lambda item: item[1]) + ][::-1][0:5] + + for t in sorted_trending: + reply += self.price_reply(self.find_symbols(t))[0] + "\n" + + reply += "\n\nTrending Stocks:\n" reply += "-" * len("Trending Stocks:") + "\n" for stock in stocks: reply += stock + "\n"