1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-09-12 16:54:59 +00:00

added /news functionality #22

This commit is contained in:
2019-06-11 05:08:49 -07:00
parent 99f0bf68d1
commit fd1baad811
2 changed files with 43 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ import urllib.request
from datetime import datetime
IEX_TOKEN = os.environ["IEX"]
def getTickers(text: str):
"""
Takes a blob of text and returns any stock tickers found.
@@ -76,3 +78,23 @@ def tickerDividend(tickers: list):
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
return messages
def tickerNews(tickers: list):
messages = {}
for ticker in tickers:
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/news/last/3?token={IEX_TOKEN}"
with urllib.request.urlopen(IEXurl) as url:
data = json.loads(url.read().decode())
if data:
messages[ticker] = f"News for **{ticker.upper()}**:\n"
for news in data:
message = f"\t[{news['headline']}]({news['url']})\n\n"
messages[ticker] = messages[ticker] + message
else:
messages[
ticker
] = f"No news found for: {ticker}\nEither today is boring or the ticker does not exist."
return messages