From e210b9ad0e8abb32ec4c813cd427cf5cf47fbdd5 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 22 Feb 2019 11:57:21 -0700 Subject: [PATCH 1/6] replaced formatting with f strings --- bot/stockBot.py | 47 ++++++++++++----------------------------------- bot/tickerInfo.py | 10 ++++------ 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 60608bf..8b86224 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -65,36 +65,22 @@ def news(bot, update): price = tickerData[ticker + "Price"] change = tickerData[ticker + "Change"] - message = ( - "The current stock price of " - + name - + " is $**" - + str(price) - + "**" - ) + message = f"The current stock price of {name} is $**{price}**" if change > 0: - message = ( - message - + ", the stock is currently **up " - + str(change) - + "%**" - ) + message = f"{message}, the stock is currently **up {change}%**" elif change < 0: message = ( - message - + ", the stock is currently **down" - + str(change) - + "%**" + f"{message}, the stock is currently **down {change}%**" ) else: message = ( - message + ", the stock hasn't shown any movement today." + f"{message}, the stock hasn't shown any movement today." ) news = tickerInfo.stockNews(ticker) for i in range(3): - message = "{}\n\n[{}]({})".format( - message, news["title"][i], news["link"][i] + message = ( + f"{message}\n\n[{news['title'][i]}]({news['link'][i]})" ) update.message.reply_text( @@ -113,9 +99,10 @@ def stockInfo(bot, update): try: # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters tickers = re.findall("[$](\w{1,4})", message) - bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) tickerData = tickerInfo.tickerQuote(tickers) + bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + for ticker in tickers: ticker = ticker.upper() # Makes sure ticker exists @@ -123,22 +110,13 @@ def stockInfo(bot, update): name = tickerData[ticker + "Name"] price = tickerData[ticker + "Price"] change = tickerData[ticker + "Change"] - message = ( - "The current stock price of " + name + " is $**" + str(price) + "**" - ) + message = f"The current stock price of {name} is $**{price}**" if change > 0: - message = ( - message + ", the stock is currently **up " + str(change) + "%**" - ) + message = f"{message}, the stock is currently **up {change}%**" elif change < 0: - message = ( - message - + ", the stock is currently **down " - + str(change) - + "%**" - ) + message = f"{message}, the stock is currently **down {change}%**" else: - message = message + ", the stock hasn't shown any movement today." + message = f"{message}, the stock hasn't shown any movement today." update.message.reply_text( text=message, parse_mode=telegram.ParseMode.MARKDOWN ) @@ -151,7 +129,6 @@ def stockInfo(bot, update): def dividend(bot, update): message = update.message.text chat_id = update.message.chat_id - print("div") try: # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters tickers = re.findall("[$](\w{1,4})", message) diff --git a/bot/tickerInfo.py b/bot/tickerInfo.py index 58d5ef2..89a4595 100644 --- a/bot/tickerInfo.py +++ b/bot/tickerInfo.py @@ -39,7 +39,7 @@ def stockNews(ticker): """Makes a bunch of strings that are links to news websites for an input ticker""" print("Gather News on " + ticker) - newsLink = "https://api.iextrading.com/1.0/stock/{}/news/last/5".format(ticker) + newsLink = f"https://api.iextrading.com/1.0/stock/{ticker}/news/last/5" with urllib.request.urlopen(newsLink) as url: data = json.loads(url.read().decode()) @@ -53,12 +53,12 @@ def stockNews(ticker): def stockLogo(ticker): """returns a png of an input ticker""" - logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png" + logoURL = f"https://g.foolcdn.com/art/companylogos/mark/{ticker}.png" return logoURL def stockInfo(ticker): - infoURL = "https://api.iextrading.com/1.0/stock/{}/stats".format(ticker) + infoURL = f"https://api.iextrading.com/1.0/stock/{ticker}/stats" with urllib.request.urlopen(infoURL) as url: data = json.loads(url.read().decode()) @@ -100,9 +100,7 @@ def stockDividend(ticker): h, m = divmod(h, 3600) m, s = divmod(m, 60) - countdownMessage = "\n\nThe dividend is in: {:.0f} Days {:.0f} Hours {:.0f} Minutes {:.0f} Seconds.".format( - d, h, m, s - ) + countdownMessage = f"\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds." message = line1 + countdownMessage return message From d0505a641d951c7e27e7eaa1c1dac82723dc8616 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 22 Feb 2019 12:34:20 -0700 Subject: [PATCH 2/6] #12 news now prints regardless of how many items api returns. --- bot/stockBot.py | 8 +++----- bot/tickerInfo.py | 5 +++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 8b86224..5ec6eae 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -48,10 +48,8 @@ def news(bot, update): if tickers == []: message = "No Ticker, showing Market News:" news = tickerInfo.stockNews("market") - for i in range(3): - message = "{}\n\n[{}]({})".format( - message, news["title"][i], news["link"][i] - ) + for i in range(len(news["title"])): + message = f"{message}\n\n[{news['title'][i]}]({news['link'][i]})" update.message.reply_text( text=message, parse_mode=telegram.ParseMode.MARKDOWN ) @@ -78,7 +76,7 @@ def news(bot, update): ) news = tickerInfo.stockNews(ticker) - for i in range(3): + for i in range(len(news["title"])): message = ( f"{message}\n\n[{news['title'][i]}]({news['link'][i]})" ) diff --git a/bot/tickerInfo.py b/bot/tickerInfo.py index 89a4595..c6c9307 100644 --- a/bot/tickerInfo.py +++ b/bot/tickerInfo.py @@ -40,14 +40,15 @@ def stockNews(ticker): print("Gather News on " + ticker) newsLink = f"https://api.iextrading.com/1.0/stock/{ticker}/news/last/5" - + print(newsLink) with urllib.request.urlopen(newsLink) as url: data = json.loads(url.read().decode()) news = {"link": [], "title": []} - for i in range(3): + for i in range(len(data)): news["link"].append(data[i]["url"]) news["title"].append(data[i]["headline"]) + print(i) return news From 87448cbe90dcd76ad9eb41c65a57743f1cf945cf Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 22 Feb 2019 12:35:26 -0700 Subject: [PATCH 3/6] leftover code from testing things --- bot/tickerInfo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/tickerInfo.py b/bot/tickerInfo.py index c6c9307..ebb52b7 100644 --- a/bot/tickerInfo.py +++ b/bot/tickerInfo.py @@ -48,7 +48,6 @@ def stockNews(ticker): for i in range(len(data)): news["link"].append(data[i]["url"]) news["title"].append(data[i]["headline"]) - print(i) return news From b7fb4df4a8717517111c2bac175c0a29a3ddee43 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 22 Mar 2019 12:37:43 -0700 Subject: [PATCH 4/6] gave regex for tickers a variable #13 --- bot/stockBot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 5ec6eae..218f74a 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -129,7 +129,7 @@ def dividend(bot, update): chat_id = update.message.chat_id try: # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters - tickers = re.findall("[$](\w{1,4})", message) + tickers = re.findall(TICKER_REGEX, message) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) for ticker in tickers: From 2ebcf5f8d8a9e2249ecc8b71c0a0114740d98c34 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 22 Mar 2019 12:37:57 -0700 Subject: [PATCH 5/6] fixed issues #13 --- bot/stockBot.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 218f74a..7ae03a4 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -11,6 +11,7 @@ import credentials import tickerInfo TOKEN = credentials.secrets["TELEGRAM_TOKEN"] +TICKER_REGEX = "[$]([a-zA-Z]{1,4})" # Enable logging logging.basicConfig( @@ -41,7 +42,7 @@ def news(bot, update): try: # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters - tickers = re.findall("[$](\w{1,4})", message) + tickers = re.findall(TICKER_REGEX, message) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) ## Checks if a ticker was passed in @@ -96,10 +97,11 @@ def stockInfo(bot, update): try: # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters - tickers = re.findall("[$](\w{1,4})", message) + tickers = re.findall(TICKER_REGEX, message) - tickerData = tickerInfo.tickerQuote(tickers) - bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + if len(tickers) > 0: + tickerData = tickerInfo.tickerQuote(tickers) + bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) for ticker in tickers: ticker = ticker.upper() From 604b7c7e1cfb569544c977bd62a23a17e6b85d88 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 22 Mar 2019 17:52:36 -0700 Subject: [PATCH 6/6] making CI also build docker image --- bot/credentials.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/credentials.py b/bot/credentials.py index c790bb0..e389713 100644 --- a/bot/credentials.py +++ b/bot/credentials.py @@ -1,5 +1,5 @@ secrets = { - "TELEGRAM_TOKEN": "TELEGRAM BOT TOKEN HERE", + "TELEGRAM_TOKEN": "724630968:AAG3-J26jID34AK_SJg5nJCgr7atiVOZc6A", "TWITTER_CONSUMER_API": "CONSUMER_API", "TWITTER_CONSUMER_SECRET": "CONSUMER_SECRET", "TWITTER_ACCESS_TOKEN": "ACCESS_TOKEN",