From baf45e09c8a78237f32ffa6a68380bc9f77c6367 Mon Sep 17 00:00:00 2001 From: MisterBiggs Date: Wed, 23 Jun 2021 14:17:50 -0700 Subject: [PATCH] Closes #69 --- IEX_Symbol.py | 22 ++++++++++++++++++++++ bot.py | 27 +++++++++++++++++++++++++++ cg_Crypto.py | 37 +++++++++++++++++++++++++++++++++++++ symbol_router.py | 25 +++++++++++++++++++++++++ 4 files changed, 111 insertions(+) diff --git a/IEX_Symbol.py b/IEX_Symbol.py index 3463ecd..19798fe 100644 --- a/IEX_Symbol.py +++ b/IEX_Symbol.py @@ -400,6 +400,28 @@ class IEX_Symbol: else: return f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." + def cap_reply(self, stock: Stock) -> str: + """Get the Market Cap of a stock""" + response = r.get( + f"https://cloud.iexapis.com/stable/stock/{stock.id}/stats?token={self.IEX_TOKEN}", + timeout=5, + ) + if response.status_code == 200: + + try: + data = response.json() + + cap = data["marketcap"] + except KeyError: + return f"{stock.id} returned an error." + + message = f"The current market cap of {stock.name} is $**{cap:,.2f}**" + + else: + message = f"The Coin: {stock.name} was not found or returned and error." + + return message + def intra_reply(self, symbol: Stock) -> pd.DataFrame: """Returns price data for a symbol since the last market open. diff --git a/bot.py b/bot.py index e14f42b..600c066 100644 --- a/bot.py +++ b/bot.py @@ -397,6 +397,32 @@ def stat(update: Update, context: CallbackContext): ) +def cap(update: Update, context: CallbackContext): + """ + Market Cap Information + """ + message = update.message.text + chat_id = update.message.chat_id + + if message.strip().split("@")[0] == "/cap": + update.message.reply_text( + "This command returns the market cap for a symbol.\nExample: /cap $tsla" + ) + return + + symbols = s.find_symbols(message) + + if symbols: + context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + + for reply in s.cap_reply(symbols): + update.message.reply_text( + text=reply, + parse_mode=telegram.ParseMode.MARKDOWN, + disable_notification=True, + ) + + def trending(update: Update, context: CallbackContext): """ Trending Symbols @@ -501,6 +527,7 @@ def main(): dp.add_handler(CommandHandler("info", info)) dp.add_handler(CommandHandler("stat", stat)) dp.add_handler(CommandHandler("stats", stat)) + dp.add_handler(CommandHandler("cap", cap)) dp.add_handler(CommandHandler("trending", trending)) dp.add_handler(CommandHandler("search", search)) dp.add_handler(CommandHandler("intraday", intra)) diff --git a/cg_Crypto.py b/cg_Crypto.py index de9e8ea..0d59104 100644 --- a/cg_Crypto.py +++ b/cg_Crypto.py @@ -252,6 +252,43 @@ class cg_Crypto: else: return f"{symbol.symbol} returned an error." + def cap_reply(self, coin: Coin) -> str: + """Gets market Cap for each symbol in the list + + Parameters + ---------- + symbols : List[str] + List of coin symbols + + Returns + ------- + Dict[str, str] + Each symbol passed in is a key with its value being a human readable formatted string of the symbols markey cap. + """ + response = r.get( + f"https://api.coingecko.com/api/v3/simple/price?ids={coin.id}&vs_currencies={self.vs_currency}&include_market_cap=true", + timeout=5, + ) + if response.status_code == 200: + + try: + data = response.json()[coin.id] + + price = data[self.vs_currency] + cap = data[self.vs_currency + "_market_cap"] + except KeyError: + return f"{coin.id} returned an error." + + if cap == 0: + return f"The market cap for {coin.name} is not available for unknown reasons." + + message = f"The current price of {coin.name} is $**{price:,}** and its market cap is $**{cap:,.2f}** {self.vs_currency.upper()}" + + else: + message = f"The Coin: {coin.name} was not found or returned and error." + + return message + def info_reply(self, symbol: Coin) -> str: """Gets information on stock symbols. diff --git a/symbol_router.py b/symbol_router.py index a69f78c..e5a3dd6 100644 --- a/symbol_router.py +++ b/symbol_router.py @@ -314,6 +314,31 @@ class Router: return replies + def cap_reply(self, symbols: List[Symbol]) -> List[str]: + """Gets market cap for each symbol in the list + + Parameters + ---------- + symbols : List[str] + List of stock symbols + + Returns + ------- + Dict[str, str] + Each symbol passed in is a key with its value being a human readable formatted string of the symbols market cap. + """ + replies = [] + + for symbol in symbols: + if isinstance(symbol, Stock): + replies.append(self.stock.cap_reply(symbol)) + elif isinstance(symbol, Coin): + replies.append(self.crypto.cap_reply(symbol)) + else: + print(f"{symbol} is not a Stock or Coin") + + return replies + def trending(self) -> str: """Checks APIs for trending symbols.