mirror of
https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git
synced 2025-06-16 23:16:48 +00:00
fixed dividend function for if symbol doesnt exist
This commit is contained in:
parent
14ced9682a
commit
bc09be8ea4
31
functions.py
31
functions.py
@ -52,21 +52,26 @@ def tickerDividend(tickers: list):
|
|||||||
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/dividends/next?token={cred.secret}"
|
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/dividends/next?token={cred.secret}"
|
||||||
with urllib.request.urlopen(IEXurl) as url:
|
with urllib.request.urlopen(IEXurl) as url:
|
||||||
data = json.loads(url.read().decode())
|
data = json.loads(url.read().decode())
|
||||||
|
if data:
|
||||||
|
# Pattern IEX uses for dividend date.
|
||||||
|
pattern = "%Y-%m-%d"
|
||||||
|
|
||||||
# Pattern IEX uses for dividend date.
|
# Convert divDate to seconds, and subtract it from current time.
|
||||||
pattern = "%Y-%m-%d"
|
dividendSeconds = datetime.strptime(
|
||||||
|
data["paymentDate"], pattern
|
||||||
|
).timestamp()
|
||||||
|
difference = dividendSeconds - int(time.time())
|
||||||
|
|
||||||
# Convert divDate to seconds, and subtract it from current time.
|
# Calculate (d)ays, (h)ours, (m)inutes, and (s)econds
|
||||||
dividendSeconds = datetime.strptime(data["paymentDate"], pattern).timestamp()
|
d, h = divmod(difference, 86400)
|
||||||
difference = dividendSeconds - int(time.time())
|
h, m = divmod(h, 3600)
|
||||||
|
m, s = divmod(m, 60)
|
||||||
|
|
||||||
# Calculate (d)ays, (h)ours, (m)inutes, and (s)econds
|
messages[
|
||||||
d, h = divmod(difference, 86400)
|
ticker
|
||||||
h, m = divmod(h, 3600)
|
] = f"{data['description']}\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
|
||||||
m, s = divmod(m, 60)
|
else:
|
||||||
|
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
|
||||||
messages[
|
|
||||||
"ticker"
|
|
||||||
] = f"{data['description']}\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
|
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user