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

Merge remote-tracking branch 'origin/master'

This commit is contained in:
Anson 2019-02-03 19:49:09 -07:00
commit 5f3b324742
2 changed files with 65 additions and 51 deletions

View File

@ -1,4 +1,4 @@
# Work with Python 3.6
# Work with Python 3.7
import json
import logging
import re
@ -7,14 +7,14 @@ import urllib.request
import telegram
from telegram.ext import CommandHandler, Filters, MessageHandler, Updater
import credentials
import tickerInfo
TOKEN = "TELEGRAM BOT TOKEN HERE"
TOKEN = "TELEGRAM_TOKEN
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
@ -23,7 +23,7 @@ logger = logging.getLogger(__name__)
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
"""Send a message when the command /start is issued."""
update.message.reply_text('I am started and ready to go!')
update.message.reply_text("I am started and ready to go!")
def help(bot, update):
@ -35,57 +35,70 @@ def stockInfo(bot, update):
message = update.message.text
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)
tickers = re.findall("[$](\w{1,4})", message)
# Checks if !news is called, and prints news embed if it is
if message.startswith('!news'):
if message.startswith("!news"):
tickerData = tickerInfo.tickerQuote(tickers)
for ticker in tickers:
ticker = ticker.upper()
# bot.send_photo(bot.get_updates()
# [-1].message.chat_id, tickerData[ticker + 'Image'])
name = tickerData[ticker + 'Name']
price = tickerData[ticker + 'Price']
change = tickerData[ticker + 'Change']
name = tickerData[ticker + "Name"]
price = tickerData[ticker + "Price"]
change = tickerData[ticker + "Change"]
message = 'The current stock price of ' + \
name + ' is $**' + str(price) + '**'
message = (
"The current stock price of " + name + " is $**" + str(price) + "**"
)
if change > 0:
message = message + \
', the stock is currently **up ' + str(change) + '%**'
message = (
message + ", the stock is currently **up " + str(change) + "%**"
)
elif change < 0:
message = message + \
', the stock is currently **down' + str(change) + '%**'
message = (
message
+ ", the stock is currently **down"
+ str(change)
+ "%**"
)
else:
message = message + ", the stock hasn't shown any movement today."
news = tickerInfo.stockNewsList(ticker)
for source in news:
message = message + \
'\n[' + source + '](' + news[source] + ')'
message = message + "\n[" + source + "](" + news[source] + ")"
update.message.reply_text(
text=message, parse_mode=telegram.ParseMode.MARKDOWN)
text=message, parse_mode=telegram.ParseMode.MARKDOWN
)
else: # If news isnt called, print normal stock price
tickerData = tickerInfo.tickerQuote(tickers)
for ticker in tickers:
ticker = ticker.upper()
name = tickerData[ticker + 'Name']
price = tickerData[ticker + 'Price']
change = tickerData[ticker + 'Change']
message = 'The current stock price of ' + \
name + ' is $**' + str(price) + '**'
name = tickerData[ticker + "Name"]
price = tickerData[ticker + "Price"]
change = tickerData[ticker + "Change"]
message = (
"The current stock price of " + name + " is $**" + str(price) + "**"
)
if change > 0:
message = message + ', the stock is currently **up ' + \
str(change) + '%**'
message = (
message + ", the stock is currently **up " + str(change) + "%**"
)
elif change < 0:
message = message + ', the stock is currently **down ' + \
str(change) + '%**'
message = (
message
+ ", the stock is currently **down "
+ str(change)
+ "%**"
)
else:
move = ", the stock hasn't shown any movement today."
update.message.reply_text(
text=message, parse_mode=telegram.ParseMode.MARKDOWN)
text=message, parse_mode=telegram.ParseMode.MARKDOWN
)
except:
pass
@ -122,5 +135,5 @@ def main():
updater.idle()
if __name__ == '__main__':
if __name__ == "__main__":
main()

View File

@ -4,39 +4,40 @@ import json
def tickerQuote(tickers):
stockData = {}
IEXURL = 'https://api.iextrading.com/1.0/stock/market/batch?symbols=' + \
",".join(tickers) + '&types=quote'
print('Gathering Quote from ' + IEXURL)
IEXURL = (
"https://api.iextrading.com/1.0/stock/market/batch?symbols="
+ ",".join(tickers)
+ "&types=quote"
)
print("Gathering Quote from " + IEXURL)
with urllib.request.urlopen(IEXURL) as url:
IEXData = json.loads(url.read().decode())
for ticker in tickers:
ticker = ticker.upper()
stockData[ticker + 'Name'] = IEXData[ticker]['quote']['companyName']
stockData[ticker + 'Price'] = \
IEXData[ticker]['quote']['latestPrice']
stockData[ticker +
'Change'] = IEXData[ticker]['quote']['changePercent'] * 100
stockData[ticker + 'Image'] = stockLogo(ticker)
print('Quote Gathered')
stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"]
stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"]
stockData[ticker + "Change"] = IEXData[ticker]["quote"]["changePercent"] * 100
stockData[ticker + "Image"] = stockLogo(ticker)
print("Quote Gathered")
return stockData
def stockNewsList(ticker):
print('Gather News on ' + ticker)
print("Gather News on " + ticker)
news = {
'Bravos': 'https://bravos.co/' + ticker,
'Seeking Alpha': 'https://seekingalpha.com/symbol/' + ticker,
'MSN Money': 'https://www.msn.com/en-us/money/stockdetails?symbol=' + ticker,
'Yahoo Finance': 'https://finance.yahoo.com/quote/' + ticker,
'Wall Street Journal': 'https://quotes.wsj.com/' + ticker,
'The Street': 'https://www.thestreet.com/quote/' + ticker + '.html',
'Zacks': 'https://www.zacks.com/stock/quote/' + ticker
"Bravos": "https://bravos.co/" + ticker,
"Seeking Alpha": "https://seekingalpha.com/symbol/" + ticker,
"MSN Money": "https://www.msn.com/en-us/money/stockdetails?symbol=" + ticker,
"Yahoo Finance": "https://finance.yahoo.com/quote/" + ticker,
"Wall Street Journal": "https://quotes.wsj.com/" + ticker,
"The Street": "https://www.thestreet.com/quote/" + ticker + ".html",
"Zacks": "https://www.zacks.com/stock/quote/" + ticker,
}
print('News gathered.')
print("News gathered.")
return news
def stockLogo(ticker):
logoURL = 'https://g.foolcdn.com/art/companylogos/mark/' + ticker + '.png'
logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png"
return logoURL