diff --git a/IEX_Symbol.py b/IEX_Symbol.py index b99ce06..0012efd 100644 --- a/IEX_Symbol.py +++ b/IEX_Symbol.py @@ -71,8 +71,9 @@ class IEX_Symbol: symbols["description"] = "$" + symbols["symbol"] + ": " + symbols["name"] symbols["id"] = symbols["symbol"] + symbols["type_id"] = "$" + symbols["symbol"].str.lower() - symbols = symbols[["id", "symbol", "name", "description"]] + symbols = symbols[["id", "symbol", "name", "description", "type_id"]] self.symbol_list = symbols if return_df: return symbols, datetime.now() diff --git a/bot.py b/bot.py index c97436e..e14f42b 100644 --- a/bot.py +++ b/bot.py @@ -419,12 +419,11 @@ def inline_query(update: Update, context: CallbackContext): Does a fuzzy search on input and returns stocks that are close. """ - matches = s.search_symbols(update.inline_query.query)[:] + matches = s.inline_search(update.inline_query.query)[:5] symbols = " ".join([match[1].split(":")[0] for match in matches]) prices = s.batch_price_reply(s.find_symbols(symbols)) - # print(len(matches), len(prices)) - # print(prices) + results = [] print(update.inline_query.query) for match, price in zip(matches, prices): diff --git a/cg_Crypto.py b/cg_Crypto.py index 09fb135..6413040 100644 --- a/cg_Crypto.py +++ b/cg_Crypto.py @@ -50,8 +50,11 @@ class cg_Crypto: ).json() symbols = pd.DataFrame(data=raw_symbols) - symbols["description"] = "$$" + symbols["symbol"] + ": " + symbols["name"] + symbols["description"] = ( + "$$" + symbols["symbol"].str.upper() + ": " + symbols["name"] + ) symbols = symbols[["id", "symbol", "name", "description"]] + symbols["type_id"] = "$$" + symbols["id"] self.symbol_list = symbols if return_df: @@ -301,19 +304,15 @@ class cg_Crypto: ).json() replies = [] - for name, val in prices.items(): - if price := val.get("usd"): - price = val.get("usd") - else: - replies.append(f"{name} price data unavailable.") - break + for coin in coins: + if coin.id in prices: + p = prices[coin.id] - change = 0 - if val.get("usd_24h_change") is not None: - change = val.get("usd_24h_change") + if p.get("usd_24h_change") is None: + p["usd_24h_change"] = 0 - replies.append( - f"{name}: ${price:,} and has moved {change:.2f}% in the past 24 hours." - ) + replies.append( + f"{coin.name}: ${p.get('usd',0):,} and has moved {p.get('usd_24h_change',0.0):.2f}% in the past 24 hours." + ) return replies diff --git a/symbol_router.py b/symbol_router.py index 247daa3..10ca415 100644 --- a/symbol_router.py +++ b/symbol_router.py @@ -113,6 +113,34 @@ class Router: self.searched_symbols[search] = symbol_list return symbol_list + def inline_search(self, search: str) -> List[Tuple[str, str]]: + """Searches based on the shortest symbol that contains the same string as the search. + Should be very fast compared to a fuzzy search. + + Parameters + ---------- + search : str + String used to match against symbols. + + Returns + ------- + List[tuple[str, str]] + Each tuple contains: (Symbol, Issue Name). + """ + + df = pd.concat([self.stock.symbol_list, self.crypto.symbol_list]) + + search = search.lower() + + df = df[df["type_id"].str.contains(search, regex=False)].sort_values( + by="type_id", key=lambda x: x.str.len() + ) + + symbols = df.head(20) + symbol_list = list(zip(list(symbols["symbol"]), list(symbols["description"]))) + self.searched_symbols[search] = symbol_list + return symbol_list + def price_reply(self, symbols: list[Symbol]) -> List[str]: """Returns current market price or after hours if its available for a given stock symbol. @@ -349,11 +377,10 @@ class Router: print(f"{symbol} is not a Stock or Coin") if stocks: - for ( - stock - ) in stocks: # IEX batch endpoint doesnt seem to be working right now + # IEX batch endpoint doesnt seem to be working right now + for stock in stocks: replies.append(self.stock.price_reply(stock)) if coins: - replies.append(self.crypto.batch_price(coins)) + replies = replies + self.crypto.batch_price(coins) return replies