mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 07:16:40 +00:00
Fixes #55
This commit is contained in:
parent
1d884110e6
commit
aba7120889
@ -69,8 +69,9 @@ class IEX_Symbol:
|
|||||||
|
|
||||||
symbols["description"] = "$" + symbols["symbol"] + ": " + symbols["name"]
|
symbols["description"] = "$" + symbols["symbol"] + ": " + symbols["name"]
|
||||||
symbols["id"] = symbols["symbol"]
|
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
|
self.symbol_list = symbols
|
||||||
if return_df:
|
if return_df:
|
||||||
return symbols, datetime.now()
|
return symbols, datetime.now()
|
||||||
|
5
bot.py
5
bot.py
@ -422,12 +422,11 @@ def inline_query(update: Update, context: CallbackContext):
|
|||||||
Does a fuzzy search on input and returns stocks that are close.
|
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])
|
symbols = " ".join([match[1].split(":")[0] for match in matches])
|
||||||
prices = s.batch_price_reply(s.find_symbols(symbols))
|
prices = s.batch_price_reply(s.find_symbols(symbols))
|
||||||
# print(len(matches), len(prices))
|
|
||||||
# print(prices)
|
|
||||||
results = []
|
results = []
|
||||||
print(update.inline_query.query)
|
print(update.inline_query.query)
|
||||||
for match, price in zip(matches, prices):
|
for match, price in zip(matches, prices):
|
||||||
|
25
cg_Crypto.py
25
cg_Crypto.py
@ -47,8 +47,11 @@ class cg_Crypto:
|
|||||||
raw_symbols = r.get("https://api.coingecko.com/api/v3/coins/list").json()
|
raw_symbols = r.get("https://api.coingecko.com/api/v3/coins/list").json()
|
||||||
symbols = pd.DataFrame(data=raw_symbols)
|
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 = symbols[["id", "symbol", "name", "description"]]
|
||||||
|
symbols["type_id"] = "$$" + symbols["id"]
|
||||||
|
|
||||||
self.symbol_list = symbols
|
self.symbol_list = symbols
|
||||||
if return_df:
|
if return_df:
|
||||||
@ -287,19 +290,15 @@ class cg_Crypto:
|
|||||||
).json()
|
).json()
|
||||||
|
|
||||||
replies = []
|
replies = []
|
||||||
for name, val in prices.items():
|
for coin in coins:
|
||||||
if price := val.get("usd"):
|
if coin.id in prices:
|
||||||
price = val.get("usd")
|
p = prices[coin.id]
|
||||||
else:
|
|
||||||
replies.append(f"{name} price data unavailable.")
|
|
||||||
break
|
|
||||||
|
|
||||||
change = 0
|
if p.get("usd_24h_change") is None:
|
||||||
if val.get("usd_24h_change") is not None:
|
p["usd_24h_change"] = 0
|
||||||
change = val.get("usd_24h_change")
|
|
||||||
|
|
||||||
replies.append(
|
replies.append(
|
||||||
f"{name}: ${price:,} and has moved {change:.2f}% in the past 24 hours."
|
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
|
return replies
|
||||||
|
@ -113,6 +113,34 @@ class Router:
|
|||||||
self.searched_symbols[search] = symbol_list
|
self.searched_symbols[search] = symbol_list
|
||||||
return 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]:
|
def price_reply(self, symbols: list[Symbol]) -> List[str]:
|
||||||
"""Returns current market price or after hours if its available for a given stock symbol.
|
"""Returns current market price or after hours if its available for a given stock symbol.
|
||||||
|
|
||||||
@ -347,11 +375,10 @@ class Router:
|
|||||||
print(f"{symbol} is not a Stock or Coin")
|
print(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
if stocks:
|
if stocks:
|
||||||
for (
|
# IEX batch endpoint doesnt seem to be working right now
|
||||||
stock
|
for stock in stocks:
|
||||||
) in stocks: # IEX batch endpoint doesnt seem to be working right now
|
|
||||||
replies.append(self.stock.price_reply(stock))
|
replies.append(self.stock.price_reply(stock))
|
||||||
if coins:
|
if coins:
|
||||||
replies.append(self.crypto.batch_price(coins))
|
replies = replies + self.crypto.batch_price(coins)
|
||||||
|
|
||||||
return replies
|
return replies
|
||||||
|
Loading…
x
Reference in New Issue
Block a user