1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-16 15:17:28 +00:00

updated dividend function for new API

This commit is contained in:
Anson 2019-05-28 01:00:40 -07:00
parent 6b679963dd
commit 14ced9682a

View File

@ -45,51 +45,28 @@ def tickerDataReply(tickers: list):
return tickerReplies return tickerReplies
# Below Functions are incomplete def tickerDividend(tickers: list):
messages = {}
for ticker in tickers:
def tickerInfo(ticker): IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/dividends/next?token={cred.secret}"
infoURL = f"https://api.iextrading.com/1.0/stock/{ticker}/stats" with urllib.request.urlopen(IEXurl) as url:
with urllib.request.urlopen(infoURL) as url:
data = json.loads(url.read().decode()) 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 tickerDividend(ticker):
data = tickerInfo(ticker)
if data["divDate"] == 0:
return "{} has no dividend.".format(data["companyName"])
dividendInfo = "{} 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 IEX uses for dividend date.
pattern = "%Y-%m-%d %H:%M:%S.%f" pattern = "%Y-%m-%d"
# Convert divDate to seconds, and subtract it from current time. # Convert divDate to seconds, and subtract it from current time.
divSeconds = datetime.strptime(divDate, pattern).timestamp() dividendSeconds = datetime.strptime(data["paymentDate"], pattern).timestamp()
difference = divSeconds - int(time.time()) difference = dividendSeconds - int(time.time())
# Calculate (d)ays, (h)ours, (m)inutes, and (s)econds # Calculate (d)ays, (h)ours, (m)inutes, and (s)econds
d, h = divmod(difference, 86400) d, h = divmod(difference, 86400)
h, m = divmod(h, 3600) h, m = divmod(h, 3600)
m, s = divmod(m, 60) m, s = divmod(m, 60)
countdownMessage = f"\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds." messages[
"ticker"
] = f"{data['description']}\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
return dividendInfo + countdownMessage return messages