From 74852854260bc118d645475cfed4a3c900061f54 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 19:58:44 -0700 Subject: [PATCH 1/7] Ticker news now uses IEX news api #8 #3 --- bot/stockBot.py | 8 +++++--- bot/tickerInfo.py | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 481d543..346d01b 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -84,9 +84,11 @@ def news(bot, update): message + ", the stock hasn't shown any movement today." ) - news = tickerInfo.stockNewsList(ticker) - for source in news: - message = message + "\n[" + source + "](" + news[source] + ")" + news = tickerInfo.stockNews(ticker) + for i in range(3): + message = "{}\n[{}]({})".format( + message, news["title"][i], news["link"][i] + ) update.message.reply_text( text=message, parse_mode=telegram.ParseMode.MARKDOWN diff --git a/bot/tickerInfo.py b/bot/tickerInfo.py index be36a50..c7a97a8 100644 --- a/bot/tickerInfo.py +++ b/bot/tickerInfo.py @@ -1,5 +1,6 @@ import urllib.request import json +import feedparser def tickerQuote(tickers): @@ -32,19 +33,19 @@ def tickerQuote(tickers): return stockData -def stockNewsList(ticker): +def stockNews(ticker): """Makes a bunch of strings that are links to news websites for an input ticker""" print("Gather News on " + ticker) - news = { - "Bravos": "https://bravos.co/" + ticker, - "Seeking Alpha": "https://seekingalpha.com/symbol/" + ticker, - "MSN Money": "https://www.msn.com/en-us/money/stockdetails?symbol=" + ticker, - "Yahoo Finance": "https://finance.yahoo.com/quote/" + ticker, - "Wall Street Journal": "https://quotes.wsj.com/" + ticker, - "The Street": "https://www.thestreet.com/quote/" + ticker + ".html", - "Zacks": "https://www.zacks.com/stock/quote/" + ticker, - } - print("News gathered.") + + newsLink = "https://api.iextrading.com/1.0/stock/{}/news/last/5".format(ticker) + + with urllib.request.urlopen(newsLink) as url: + data = json.loads(url.read().decode()) + + news = {"link": [], "title": []} + for i in range(3): + news["link"].append(data[i]["url"]) + news["title"].append(data[i]["headline"]) return news From d84324c818c2fa0d4717de4392394380c094b161 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 20:08:59 -0700 Subject: [PATCH 2/7] Formatting --- bot/stockBot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 346d01b..c50fe85 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -86,7 +86,7 @@ def news(bot, update): news = tickerInfo.stockNews(ticker) for i in range(3): - message = "{}\n[{}]({})".format( + message = "{}\n\n[{}]({})".format( message, news["title"][i], news["link"][i] ) From f7c10209c3089e4c9f075a42215d4fb6392ddcc8 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 20:09:41 -0700 Subject: [PATCH 3/7] Bot now provides market news when no ticker is called with /news --- bot/stockBot.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index c50fe85..36f8ece 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -45,8 +45,14 @@ def news(bot, update): ## Checks if a ticker was passed in 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] + ) update.message.reply_text( - "Please type a ticker after your command with a dollar sign: /news $tsla" + text=message, parse_mode=telegram.ParseMode.MARKDOWN ) else: tickerData = tickerInfo.tickerQuote(tickers) From 21fde3af80e64585db8e1a4d364108332866e65e Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 22:06:31 -0700 Subject: [PATCH 4/7] Bot now sends link to docs when /help is used --- bot/stockBot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/stockBot.py b/bot/stockBot.py index 36f8ece..5af50d0 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -29,8 +29,9 @@ def start(bot, update): def help(bot, update): - """Send a message when the command /help is issued.""" - update.message.reply_text("I don't know how to help yet!") + """Send link to docs when the command /help is issued.""" + message = "[Please see the docs for Bot information](https://misterbiggs.gitlab.io/simple-telegram-bot)" + update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) def news(bot, update): From 528e3b4a101166b5bb0203df103e7d0e4d703d87 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 22:06:53 -0700 Subject: [PATCH 5/7] Added dividend information. --- bot/stockBot.py | 20 ++++++++++++++++++ bot/tickerInfo.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/bot/stockBot.py b/bot/stockBot.py index 5af50d0..60608bf 100644 --- a/bot/stockBot.py +++ b/bot/stockBot.py @@ -148,6 +148,25 @@ def stockInfo(bot, update): pass +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) + bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + + for ticker in tickers: + message = tickerInfo.stockDividend(ticker) + update.message.reply_text( + text=message, parse_mode=telegram.ParseMode.MARKDOWN + ) + + except: + pass + + def error(bot, update, error): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, error) @@ -165,6 +184,7 @@ def main(): dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("news", news)) + dp.add_handler(CommandHandler("dividend", dividend)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, stockInfo)) diff --git a/bot/tickerInfo.py b/bot/tickerInfo.py index c7a97a8..58d5ef2 100644 --- a/bot/tickerInfo.py +++ b/bot/tickerInfo.py @@ -1,6 +1,8 @@ import urllib.request import json import feedparser +from datetime import datetime +import time def tickerQuote(tickers): @@ -53,3 +55,54 @@ def stockLogo(ticker): """returns a png of an input ticker""" logoURL = "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) + + with urllib.request.urlopen(infoURL) as url: + data = json.loads(url.read().decode()) + + info = {} + + info["companyName"] = data["companyName"] + info["marketCap"] = data["marketcap"] + info["yearHigh"] = data["week52high"] + info["yearLow"] = data["week52low"] + info["divRate"] = data["dividendRate"] + info["divYield"] = data["dividendYield"] + info["divDate"] = data["exDividendDate"] + + return info + + +def stockDividend(ticker): + data = stockInfo(ticker) + print(data["divDate"]) + if data["divDate"] == 0: + return "{} has no dividend.".format(data["companyName"]) + + line1 = "{} current dividend yield is: {:.3f}%, or ${:.3f} per share.".format( + data["companyName"], data["divRate"], data["divYield"] + ) + + divDate = data["divDate"] + + # Pattern IEX uses for dividend date. + pattern = "%Y-%m-%d %H:%M:%S.%f" + + # Convert divDate to seconds, and subtract it from current time. + divSeconds = datetime.strptime(divDate, pattern).timestamp() + difference = divSeconds - 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) + + countdownMessage = "\n\nThe dividend is in: {:.0f} Days {:.0f} Hours {:.0f} Minutes {:.0f} Seconds.".format( + d, h, m, s + ) + + message = line1 + countdownMessage + return message From 6992d87d83061cc5da6e600d2fe5ac19e5d35ecb Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 22:15:28 -0700 Subject: [PATCH 6/7] needs to be in root directory --- web/.gitlab-ci.yml => .gitlab-ci.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) rename web/.gitlab-ci.yml => .gitlab-ci.yml (62%) diff --git a/web/.gitlab-ci.yml b/.gitlab-ci.yml similarity index 62% rename from web/.gitlab-ci.yml rename to .gitlab-ci.yml index 8458cda..5aa8787 100644 --- a/web/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,10 +7,11 @@ before_script: pages: script: - - mkdocs build - - mv site public + - cd ./web + - mkdocs build + - mv site ../public artifacts: paths: - - public + - public only: - - master + - master From b6eb8b55f0ba50ebcd18a3ffa7b92875fc956578 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 12 Feb 2019 22:37:36 -0700 Subject: [PATCH 7/7] Revert "needs to be in root directory" This reverts commit 6992d87d83061cc5da6e600d2fe5ac19e5d35ecb. --- .gitlab-ci.yml => web/.gitlab-ci.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) rename .gitlab-ci.yml => web/.gitlab-ci.yml (62%) diff --git a/.gitlab-ci.yml b/web/.gitlab-ci.yml similarity index 62% rename from .gitlab-ci.yml rename to web/.gitlab-ci.yml index 5aa8787..8458cda 100644 --- a/.gitlab-ci.yml +++ b/web/.gitlab-ci.yml @@ -7,11 +7,10 @@ before_script: pages: script: - - cd ./web - - mkdocs build - - mv site ../public + - mkdocs build + - mv site public artifacts: paths: - - public + - public only: - - master + - master