From b716ef3bfbe4b88fca09876bcf1ce7ddc1f28a9c Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 02:04:50 -0700 Subject: [PATCH 1/7] cleaned up dividend code --- functions.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/functions.py b/functions.py index fef6fd3..1877d8e 100644 --- a/functions.py +++ b/functions.py @@ -1,8 +1,8 @@ import json import os import re -import time import urllib.request +import requests from datetime import datetime IEX_TOKEN = os.environ["IEX"] @@ -53,27 +53,19 @@ def tickerDividend(tickers: list): messages = {} for ticker in tickers: - IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/dividends/next?token={IEX_TOKEN}" - with urllib.request.urlopen(IEXurl) as url: - data = json.loads(url.read().decode()) - if data: + IEXurl = f"https://cloud.iexapis.com/stable/data-points/{ticker}/NEXTDIVIDENDDATE?token={IEX_TOKEN}" + date = requests.get(IEXurl).json() + if date: # Pattern IEX uses for dividend date. pattern = "%Y-%m-%d" - - # Convert divDate to seconds, and subtract it from current time. - dividendSeconds = datetime.strptime( - data["paymentDate"], pattern - ).timestamp() - difference = dividendSeconds - int(time.time()) - - # Calculate (d)ays, (h)ours, (m)inutes, and (s)econds - d, h = divmod(difference, 86400) - h, m = divmod(h, 3600) - m, s = divmod(m, 60) + divDate = datetime.strptime(date, pattern) + # + daysDelta = divDate - datetime.now() + datePretty = divDate.strftime("%A, %B %w") messages[ ticker - ] = f"{data['description']}\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds." + ] = f"{ticker.upper()} dividend is on {datePretty} which is in {daysDelta} Days." else: messages[ticker] = f"{ticker} either doesn't exist or pays no dividend." From 38fcf6d05478a0b2323e025e9a5b1a9df1b28a96 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 02:14:20 -0700 Subject: [PATCH 2/7] added requests to requirements --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9f4c97c..38bc2dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ telegram==0.0.1 -python-telegram-bot==11.1.0 \ No newline at end of file +python-telegram-bot==11.1.0 +requests==2.21.0 \ No newline at end of file From 33c80bd75b1795797def7cd03007fccd5c762000 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 12:03:59 -0700 Subject: [PATCH 3/7] renamed ticker to symbol everywhere --- bot.py | 40 ++++++++++++------------- functions.py | 85 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 69 insertions(+), 56 deletions(-) diff --git a/bot.py b/bot.py index d0b3954..db17a5f 100644 --- a/bot.py +++ b/bot.py @@ -7,7 +7,7 @@ from functions import * from telegram.ext import CommandHandler, Filters, MessageHandler, Updater TELEGRAM_TOKEN = os.environ["TELEGRAM"] -TICKER_REGEX = "[$]([a-zA-Z]{1,4})" +symbol_REGEX = "[$]([a-zA-Z]{1,4})" # Enable logging logging.basicConfig( @@ -31,19 +31,19 @@ def help(bot, update): update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) -def tickerDetect(bot, update): +def symbolDetect(bot, update): """ - Runs on any message that doesn't have a command and searches for tickers, then returns the prices of any tickers found. + Runs on any message that doesn't have a command and searches for symbols, then returns the prices of any symbols found. """ message = update.message.text chat_id = update.message.chat_id - tickers = getTickers(message) + symbols = getSymbols(message) - if tickers: + if symbols: # Let user know bot is working bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in tickerDataReply(tickers).items(): + for symbol, reply in symbolDataReply(symbols).items(): update.message.reply_text( text=reply, parse_mode=telegram.ParseMode.MARKDOWN @@ -52,16 +52,16 @@ def tickerDetect(bot, update): def dividend(bot, update): """ - waits for /dividend or /div command and then finds dividend info on that ticker. + waits for /dividend or /div command and then finds dividend info on that symbol. """ message = update.message.text chat_id = update.message.chat_id - tickers = getTickers(message) + symbols = getSymbols(message) - if tickers: + if symbols: bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in tickerDividend(tickers).items(): + for symbol, reply in symbolDividend(symbols).items(): update.message.reply_text( text=reply, parse_mode=telegram.ParseMode.MARKDOWN @@ -70,16 +70,16 @@ def dividend(bot, update): def news(bot, update): """ - waits for /news command and then finds news info on that ticker. + waits for /news command and then finds news info on that symbol. """ message = update.message.text chat_id = update.message.chat_id - tickers = getTickers(message) + symbols = getSymbols(message) - if tickers: + if symbols: bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in tickerNews(tickers).items(): + for symbol, reply in symbolNews(symbols).items(): update.message.reply_text( text=reply, parse_mode=telegram.ParseMode.MARKDOWN @@ -88,16 +88,16 @@ def news(bot, update): def info(bot, update): """ - waits for /info command and then finds info on that ticker. + waits for /info command and then finds info on that symbol. """ message = update.message.text chat_id = update.message.chat_id - tickers = getTickers(message) + symbols = getSymbols(message) - if tickers: + if symbols: bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in tickerInfo(tickers).items(): + for symbol, reply in symbolInfo(symbols).items(): update.message.reply_text( text=reply, parse_mode=telegram.ParseMode.MARKDOWN @@ -126,7 +126,7 @@ def main(): dp.add_handler(CommandHandler("info", info)) # on noncommand i.e message - echo the message on Telegram - dp.add_handler(MessageHandler(Filters.text, tickerDetect)) + dp.add_handler(MessageHandler(Filters.text, symbolDetect)) # log all errors dp.add_error_handler(error) @@ -141,4 +141,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/functions.py b/functions.py index 1877d8e..3e6c0dc 100644 --- a/functions.py +++ b/functions.py @@ -5,27 +5,27 @@ import urllib.request import requests from datetime import datetime -IEX_TOKEN = os.environ["IEX"] +IEX_TOKEN = "sk_130b8e8f75ba4e14a5683ff37a655584" # os.environ["IEX"] -def getTickers(text: str): +def getSymbols(text: str): """ - Takes a blob of text and returns any stock tickers found. + Takes a blob of text and returns any stock symbols found. """ - TICKER_REGEX = "[$]([a-zA-Z]{1,4})" + SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})" - return list(set(re.findall(TICKER_REGEX, text))) + return list(set(re.findall(SYMBOL_REGEX, text))) -def tickerDataReply(tickers: list): +def symbolDataReply(symbols: list): """ - Takes a list of tickers and returns a list of strings with information about the ticker. + Takes a list of symbols and returns a list of strings with information about the symbol. """ - tickerReplies = {} - for ticker in tickers: + symbolReplies = {} + for symbol in symbols: IEXURL = ( - f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={IEX_TOKEN}" + f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX_TOKEN}" ) try: with urllib.request.urlopen(IEXURL) as url: @@ -42,74 +42,87 @@ def tickerDataReply(tickers: list): else: reply += ", the stock hasn't shown any movement today." except: - reply = f"The ticker: {ticker} was not found." + reply = f"The symbol: {symbol} was not found." - tickerReplies[ticker] = reply + symbolReplies[symbol] = reply - return tickerReplies + return symbolReplies -def tickerDividend(tickers: list): +def symbolDividend(symbols: list): messages = {} - for ticker in tickers: - IEXurl = f"https://cloud.iexapis.com/stable/data-points/{ticker}/NEXTDIVIDENDDATE?token={IEX_TOKEN}" - date = requests.get(IEXurl).json() - if date: + for symbol in symbols: + IEXurl = f"https://cloud.iexapis.com/stable/data-points/{symbol}/NEXTDIVIDENDDATE?token={IEX_TOKEN}" + response = requests.get(IEXurl) + if response.status_code is 200: + + date = response.json() # Pattern IEX uses for dividend date. pattern = "%Y-%m-%d" divDate = datetime.strptime(date, pattern) # - daysDelta = divDate - datetime.now() + daysDelta = (divDate - datetime.now()).days datePretty = divDate.strftime("%A, %B %w") + if daysDelta < 0: + messages[ + symbol + ] = f"{symbol.upper()} dividend was on {datePretty} and a new date hasn't been announced yet." + elif daysDelta > 0: + messages[ + symbol + ] = f"{symbol.upper()} dividend is on {datePretty} which is in {daysDelta} Days." + else: + messages[symbol] = f"{symbol.upper()} is today." - messages[ - ticker - ] = f"{ticker.upper()} dividend is on {datePretty} which is in {daysDelta} Days." else: - messages[ticker] = f"{ticker} either doesn't exist or pays no dividend." + messages[symbol] = f"{symbol} either doesn't exist or pays no dividend." return messages -def tickerNews(tickers: list): +def symbolNews(symbols: list): messages = {} - for ticker in tickers: - IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/news/last/3?token={IEX_TOKEN}" + for symbol in symbols: + IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/news/last/3?token={IEX_TOKEN}" with urllib.request.urlopen(IEXurl) as url: data = json.loads(url.read().decode()) if data: - messages[ticker] = f"News for **{ticker.upper()}**:\n" + messages[symbol] = f"News for **{symbol.upper()}**:\n" for news in data: message = f"\t[{news['headline']}]({news['url']})\n\n" - messages[ticker] = messages[ticker] + message + messages[symbol] = messages[symbol] + message else: messages[ - ticker - ] = f"No news found for: {ticker}\nEither today is boring or the ticker does not exist." + symbol + ] = f"No news found for: {symbol}\nEither today is boring or the symbol does not exist." return messages -def tickerInfo(tickers: list): +def symbolInfo(symbols: list): messages = {} - for ticker in tickers: + for symbol in symbols: IEXurl = ( - f"https://cloud.iexapis.com/stable/stock/{ticker}/company?token={IEX_TOKEN}" + f"https://cloud.iexapis.com/stable/stock/{symbol}/company?token={IEX_TOKEN}" ) with urllib.request.urlopen(IEXurl) as url: data = json.loads(url.read().decode()) if data: messages[ - ticker + symbol ] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n" else: messages[ - ticker - ] = f"No information found for: {ticker}\nEither today is boring or the ticker does not exist." + symbol + ] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." return messages + +# symbolDividend(["psec", "f", "tsla"]) + +print(symbolDividend(["f"])) From a180e9f36095ed0a48a5fae040c28f333dcab19e Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 12:04:14 -0700 Subject: [PATCH 4/7] removed debugging code --- functions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/functions.py b/functions.py index 3e6c0dc..38675ba 100644 --- a/functions.py +++ b/functions.py @@ -122,7 +122,3 @@ def symbolInfo(symbols: list): return messages - -# symbolDividend(["psec", "f", "tsla"]) - -print(symbolDividend(["f"])) From baf37572067b14c5fd2f557fc00d2d2f8af440c9 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 12:32:55 -0700 Subject: [PATCH 5/7] turns out telegram was never needed --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 38bc2dd..2be6790 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -telegram==0.0.1 python-telegram-bot==11.1.0 requests==2.21.0 \ No newline at end of file From 3c506e124f71f74c4547b108781c50ce8bfc2ff0 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 12:33:11 -0700 Subject: [PATCH 6/7] cleanup and readability changes --- bot.py | 21 +++++++------ functions.py | 83 +++++++++++++++++++++++++++------------------------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/bot.py b/bot.py index db17a5f..b8e0f42 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,4 @@ -# Work with Python 3.7 +# Works with Python 3.7 import logging import os @@ -7,7 +7,6 @@ from functions import * from telegram.ext import CommandHandler, Filters, MessageHandler, Updater TELEGRAM_TOKEN = os.environ["TELEGRAM"] -symbol_REGEX = "[$]([a-zA-Z]{1,4})" # Enable logging logging.basicConfig( @@ -27,7 +26,7 @@ def start(bot, update): def help(bot, update): """Send link to docs when the command /help is issued.""" - message = "[Please see the docs for Bot information](https://simple-stock-bots.gitlab.io/site/telegram/)" + message = "[Please see the documentaion for Bot information](https://simple-stock-bots.gitlab.io/site/telegram/)" update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) @@ -43,10 +42,10 @@ def symbolDetect(bot, update): # Let user know bot is working bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in symbolDataReply(symbols).items(): + for reply in symbolDataReply(symbols).items(): update.message.reply_text( - text=reply, parse_mode=telegram.ParseMode.MARKDOWN + text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN ) @@ -61,10 +60,10 @@ def dividend(bot, update): if symbols: bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in symbolDividend(symbols).items(): + for reply in symbolDividend(symbols).items(): update.message.reply_text( - text=reply, parse_mode=telegram.ParseMode.MARKDOWN + text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN ) @@ -79,10 +78,10 @@ def news(bot, update): if symbols: bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in symbolNews(symbols).items(): + for reply in symbolNews(symbols).items(): update.message.reply_text( - text=reply, parse_mode=telegram.ParseMode.MARKDOWN + text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN ) @@ -97,10 +96,10 @@ def info(bot, update): if symbols: bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for symbol, reply in symbolInfo(symbols).items(): + for reply in symbolInfo(symbols).items(): update.message.reply_text( - text=reply, parse_mode=telegram.ParseMode.MARKDOWN + text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN ) diff --git a/functions.py b/functions.py index 38675ba..2675551 100644 --- a/functions.py +++ b/functions.py @@ -1,16 +1,17 @@ import json import os import re -import urllib.request -import requests from datetime import datetime -IEX_TOKEN = "sk_130b8e8f75ba4e14a5683ff37a655584" # os.environ["IEX"] +import requests + +IEX_TOKEN = os.environ["IEX"] +# "sk_130b8e8f75ba4e14a5683ff37a655584" def getSymbols(text: str): """ - Takes a blob of text and returns any stock symbols found. + Takes a blob of text and returns a list of symbols without any repeats. """ SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})" @@ -20,105 +21,107 @@ def getSymbols(text: str): def symbolDataReply(symbols: list): """ - Takes a list of symbols and returns a list of strings with information about the symbol. + Takes a list of symbols and returns a dictionary of strings with information about the symbol. """ - symbolReplies = {} + dataMessages = {} for symbol in symbols: - IEXURL = ( + IEXurl = ( f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX_TOKEN}" ) - try: - with urllib.request.urlopen(IEXURL) as url: - IEXData = json.loads(url.read().decode()) - reply = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**" + response = requests.get(IEXurl) + if response.status_code is 200: + IEXData = response.json() + message = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**" # Determine wording of change text change = round(IEXData["changePercent"] * 100, 2) if change > 0: - reply += f", the stock is currently **up {change}%**" + message += f", the stock is currently **up {change}%**" elif change < 0: - reply += f", the stock is currently **down {change}%**" + message += f", the stock is currently **down {change}%**" else: - reply += ", the stock hasn't shown any movement today." - except: - reply = f"The symbol: {symbol} was not found." + message += ", the stock hasn't shown any movement today." + else: + message = f"The symbol: {symbol} was not found." - symbolReplies[symbol] = reply + dataMessages[symbol] = message - return symbolReplies + return dataMessages def symbolDividend(symbols: list): - messages = {} + divMessages = {} for symbol in symbols: IEXurl = f"https://cloud.iexapis.com/stable/data-points/{symbol}/NEXTDIVIDENDDATE?token={IEX_TOKEN}" response = requests.get(IEXurl) if response.status_code is 200: + # extract date from json date = response.json() # Pattern IEX uses for dividend date. pattern = "%Y-%m-%d" divDate = datetime.strptime(date, pattern) - # + daysDelta = (divDate - datetime.now()).days datePretty = divDate.strftime("%A, %B %w") if daysDelta < 0: - messages[ + divMessages[ symbol ] = f"{symbol.upper()} dividend was on {datePretty} and a new date hasn't been announced yet." elif daysDelta > 0: - messages[ + divMessages[ symbol ] = f"{symbol.upper()} dividend is on {datePretty} which is in {daysDelta} Days." else: - messages[symbol] = f"{symbol.upper()} is today." + divMessages[symbol] = f"{symbol.upper()} is today." else: - messages[symbol] = f"{symbol} either doesn't exist or pays no dividend." + divMessages[symbol] = f"{symbol} either doesn't exist or pays no dividend." - return messages + return divMessages def symbolNews(symbols: list): - messages = {} + newsMessages = {} for symbol in symbols: IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/news/last/3?token={IEX_TOKEN}" - with urllib.request.urlopen(IEXurl) as url: - data = json.loads(url.read().decode()) - if data: - messages[symbol] = f"News for **{symbol.upper()}**:\n" + response = requests.get(IEXurl) + if response.status_code is 200: + data = response.json() + newsMessages[symbol] = f"News for **{symbol.upper()}**:\n" for news in data: message = f"\t[{news['headline']}]({news['url']})\n\n" - messages[symbol] = messages[symbol] + message + newsMessages[symbol] = newsMessages[symbol] + message else: - messages[ + newsMessages[ symbol ] = f"No news found for: {symbol}\nEither today is boring or the symbol does not exist." - return messages + return newsMessages def symbolInfo(symbols: list): - messages = {} + infoMessages = {} for symbol in symbols: IEXurl = ( f"https://cloud.iexapis.com/stable/stock/{symbol}/company?token={IEX_TOKEN}" ) - with urllib.request.urlopen(IEXurl) as url: - data = json.loads(url.read().decode()) - if data: - messages[ + response = requests.get(IEXurl) + + if response.status_code is 200: + data = response.json() + infoMessages[ symbol ] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n" else: - messages[ + infoMessages[ symbol ] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." - return messages + return infoMessages From 63e5344e86a06cfafa011f59ca015123bc136b86 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 13:21:30 -0700 Subject: [PATCH 7/7] accidentally left a key in the code again --- functions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/functions.py b/functions.py index 2675551..5ddc340 100644 --- a/functions.py +++ b/functions.py @@ -6,7 +6,6 @@ from datetime import datetime import requests IEX_TOKEN = os.environ["IEX"] -# "sk_130b8e8f75ba4e14a5683ff37a655584" def getSymbols(text: str):