From 3113c17efb7502aaa3a132fdf1683f93416484ce Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 27 May 2019 09:18:24 -0700 Subject: [PATCH] updated standard reply for tickers in text --- bot.py | 23 ++++-------------- functions.py | 68 ++++++++++++++++------------------------------------ 2 files changed, 26 insertions(+), 65 deletions(-) diff --git a/bot.py b/bot.py index 7ab7238..7e2f389 100644 --- a/bot.py +++ b/bot.py @@ -38,30 +38,17 @@ def tickerDetect(bot, update): """ message = update.message.text chat_id = update.message.chat_id - - # Let user know bot is working - bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - tickers = getTickers(message) - data = tickerData(tickers) if tickers else {} + # Let user know bot is working + if tickers: + bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + replies = tickerDataReply(tickers) - for ticker in data: + for symbol, reply in replies.items(): - # Keep track of which tickers had a return from tickerData() - if ticker.lower() in tickers: - tickers.remove(ticker.lower()) - - reply = tickerDataReply(data[ticker]) update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN) - # For any tickers that didnt have data, return that they don't exist. - for ticker in tickers: - update.message.reply_text( - ticker.upper() - + " does not exist, you should search for a real stock like $PSEC" - ) - def news(bot, update): """ diff --git a/functions.py b/functions.py index 6506a19..ca18068 100644 --- a/functions.py +++ b/functions.py @@ -4,6 +4,8 @@ import time import urllib.request from datetime import datetime +import cred + def getTickers(text: str): """ @@ -15,60 +17,32 @@ def getTickers(text: str): return list(set(re.findall(TICKER_REGEX, text))) -def tickerData(tickers: list): +def tickerDataReply(tickers: list): """ - Takes a list of tickers and returns a dictionary of information on ticker. - example: - input list: ["aapl","tsla"] - returns: - { - "AAPL": {"name": "Apple Inc.", "price": 200.72, "change": -0.01074}, - "TSLA": {"name": "Tesla Inc.", "price": 241.98, "change": -0.01168}, - } + Takes a list of tickers and returns a list of strings with information about the ticker. """ - - stockData = {} - IEXURL = ( - "https://api.iextrading.com/1.0/stock/market/batch?symbols=" - + ",".join(tickers) - + "&types=quote" - ) - with urllib.request.urlopen(IEXURL) as url: - IEXData = json.loads(url.read().decode()) - + tickerReplies = {} for ticker in tickers: - ticker = ticker.upper() + IEXURL = ( + f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={cred.secret}" + ) + with urllib.request.urlopen(IEXURL) as url: + IEXData = json.loads(url.read().decode()) - # Makes sure ticker exists before populating a dictionary - if ticker in IEXData: - stockData[ticker] = {} - stockData[ticker]["name"] = IEXData[ticker]["quote"]["companyName"] - stockData[ticker]["price"] = IEXData[ticker]["quote"]["latestPrice"] - stockData[ticker]["change"] = IEXData[ticker]["quote"]["changePercent"] + reply = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**" - return stockData + # Determine wording of change text + change = round(IEXData["changePercent"] * 100, 2) + if change > 0: + reply += f", the stock is currently **up {change}%**" + elif change < 0: + reply += f", the stock is currently **down {change}%**" + else: + reply += ", the stock hasn't shown any movement today." + tickerReplies[ticker] = reply -def tickerDataReply(ticker: dict): - """ - Takes a dictionary, likely produced from tickerData(), and returns a markdown string with information on the ticker. - example: - input dict: {"AAPL": {"name": "Apple Inc.", "price": 200.72, "change": -0.01074}} - returns: - The current stock price of Apple Inc. is $**200.72**, the stock is currently **down -1.07**% - """ - reply = f"The current stock price of {ticker['name']} is $**{ticker['price']}**" - - # Determine wording of change text - change = round(ticker["change"] * 100, 2) - if change > 0: - changeText = f", the stock is currently **up {change}%**" - elif change < 0: - changeText = f", the stock is currently **down {change}%**" - else: - changeText = ", the stock hasn't shown any movement today." - - return reply + changeText + return tickerReplies def tickerNews(tickers: list):