mirror of
https://gitlab.com/simple-stock-bots/simple-discord-stock-bot.git
synced 2025-06-16 15:17:29 +00:00
iex 2.0 functions from telegram bot
This commit is contained in:
parent
d610cfc21e
commit
6b7bda757b
167
functions.py
167
functions.py
@ -1,105 +1,80 @@
|
|||||||
import urllib.request
|
|
||||||
import json
|
import json
|
||||||
from datetime import datetime
|
import os
|
||||||
|
import re
|
||||||
import time
|
import time
|
||||||
|
import urllib.request
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
IEX_TOKEN = os.environ["IEX"]
|
||||||
|
|
||||||
|
|
||||||
def tickerQuote(tickers):
|
def getTickers(text: str):
|
||||||
"""Gathers information from IEX api on stock"""
|
"""
|
||||||
stockData = {}
|
Takes a blob of text and returns any stock tickers found.
|
||||||
IEXURL = (
|
"""
|
||||||
"https://api.iextrading.com/1.0/stock/market/batch?symbols="
|
|
||||||
+ ",".join(tickers)
|
TICKER_REGEX = "[$]([a-zA-Z]{1,4})"
|
||||||
+ "&types=quote"
|
|
||||||
)
|
return list(set(re.findall(TICKER_REGEX, text)))
|
||||||
print("Gathering Quote from " + IEXURL)
|
|
||||||
with urllib.request.urlopen(IEXURL) as url:
|
|
||||||
IEXData = json.loads(url.read().decode())
|
def tickerDataReply(tickers: list):
|
||||||
|
"""
|
||||||
|
Takes a list of tickers and returns a list of strings with information about the ticker.
|
||||||
|
"""
|
||||||
|
tickerReplies = {}
|
||||||
|
for ticker in tickers:
|
||||||
|
IEXURL = (
|
||||||
|
f"https://cloud.iexapis.com/stable/stock/{ticker}/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']}**"
|
||||||
|
|
||||||
|
# Determine wording of change text
|
||||||
|
change = round(IEXData["changePercent"] * 100, 2)
|
||||||
|
if change > 0:
|
||||||
|
reply += f", the stock is currently **up {change}%**"
|
||||||
|
elif change < 0:
|
||||||
|
reply += f", the stock is currently **down {change}%**"
|
||||||
|
else:
|
||||||
|
reply += ", the stock hasn't shown any movement today."
|
||||||
|
except:
|
||||||
|
reply = f"The ticker: {ticker} was not found."
|
||||||
|
|
||||||
|
tickerReplies[ticker] = reply
|
||||||
|
|
||||||
|
return tickerReplies
|
||||||
|
|
||||||
|
|
||||||
|
def tickerDividend(tickers: list):
|
||||||
|
messages = {}
|
||||||
|
|
||||||
for ticker in tickers:
|
for ticker in tickers:
|
||||||
ticker = ticker.upper()
|
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:
|
||||||
|
# Pattern IEX uses for dividend date.
|
||||||
|
pattern = "%Y-%m-%d"
|
||||||
|
|
||||||
# Makes sure ticker exists before populating a dictionary
|
# Convert divDate to seconds, and subtract it from current time.
|
||||||
if ticker in IEXData:
|
dividendSeconds = datetime.strptime(
|
||||||
stockData[ticker] = 1
|
data["paymentDate"], pattern
|
||||||
stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"]
|
).timestamp()
|
||||||
stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"]
|
difference = dividendSeconds - int(time.time())
|
||||||
stockData[ticker + "Change"] = round(
|
|
||||||
(IEXData[ticker]["quote"]["changePercent"] * 100), 2
|
# Calculate (d)ays, (h)ours, (m)inutes, and (s)econds
|
||||||
)
|
d, h = divmod(difference, 86400)
|
||||||
stockData[ticker + "Image"] = stockLogo(ticker)
|
h, m = divmod(h, 3600)
|
||||||
print(ticker + " Quote Gathered")
|
m, s = divmod(m, 60)
|
||||||
|
|
||||||
|
messages[
|
||||||
|
ticker
|
||||||
|
] = f"{data['description']}\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
|
||||||
else:
|
else:
|
||||||
stockData[ticker] = 0
|
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
|
||||||
return stockData
|
|
||||||
|
|
||||||
|
return messages
|
||||||
def stockNews(ticker):
|
|
||||||
"""Makes a bunch of strings that are links to news websites for an input ticker"""
|
|
||||||
print("Gather News on " + ticker)
|
|
||||||
|
|
||||||
newsLink = f"https://api.iextrading.com/1.0/stock/{ticker}/news/last/5"
|
|
||||||
print(newsLink)
|
|
||||||
with urllib.request.urlopen(newsLink) as url:
|
|
||||||
data = json.loads(url.read().decode())
|
|
||||||
|
|
||||||
news = {"link": [], "title": []}
|
|
||||||
for i in range(len(data)):
|
|
||||||
news["link"].append(data[i]["url"])
|
|
||||||
news["title"].append(data[i]["headline"])
|
|
||||||
return news
|
|
||||||
|
|
||||||
|
|
||||||
def stockLogo(ticker):
|
|
||||||
"""returns a png of an input ticker"""
|
|
||||||
logoURL = f"https://g.foolcdn.com/art/companylogos/mark/{ticker}.png"
|
|
||||||
return logoURL
|
|
||||||
|
|
||||||
|
|
||||||
def stockInfo(ticker):
|
|
||||||
infoURL = f"https://api.iextrading.com/1.0/stock/{ticker}/stats"
|
|
||||||
|
|
||||||
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 = f"\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
|
|
||||||
|
|
||||||
message = line1 + countdownMessage
|
|
||||||
return message
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user