mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 15:17:28 +00:00
Added dividend information.
This commit is contained in:
parent
21fde3af80
commit
528e3b4a10
@ -148,6 +148,25 @@ def stockInfo(bot, update):
|
|||||||
pass
|
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):
|
def error(bot, update, error):
|
||||||
"""Log Errors caused by Updates."""
|
"""Log Errors caused by Updates."""
|
||||||
logger.warning('Update "%s" caused error "%s"', update, error)
|
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("start", start))
|
||||||
dp.add_handler(CommandHandler("help", help))
|
dp.add_handler(CommandHandler("help", help))
|
||||||
dp.add_handler(CommandHandler("news", news))
|
dp.add_handler(CommandHandler("news", news))
|
||||||
|
dp.add_handler(CommandHandler("dividend", dividend))
|
||||||
|
|
||||||
# on noncommand i.e message - echo the message on Telegram
|
# on noncommand i.e message - echo the message on Telegram
|
||||||
dp.add_handler(MessageHandler(Filters.text, stockInfo))
|
dp.add_handler(MessageHandler(Filters.text, stockInfo))
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import urllib.request
|
import urllib.request
|
||||||
import json
|
import json
|
||||||
import feedparser
|
import feedparser
|
||||||
|
from datetime import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
def tickerQuote(tickers):
|
def tickerQuote(tickers):
|
||||||
@ -53,3 +55,54 @@ def stockLogo(ticker):
|
|||||||
"""returns a png of an input ticker"""
|
"""returns a png of an input ticker"""
|
||||||
logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png"
|
logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png"
|
||||||
return logoURL
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user