From 027d45b6f21330a9dd493efd4c605a00c6c55701 Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Sat, 2 Feb 2019 01:52:34 +0000 Subject: [PATCH] Switched to black formatter --- stockBot.py | 75 ++++++++++++++++++++++++++++++--------------------- tickerInfo.py | 41 ++++++++++++++-------------- 2 files changed, 65 insertions(+), 51 deletions(-) diff --git a/stockBot.py b/stockBot.py index 0db6711..a280e3d 100644 --- a/stockBot.py +++ b/stockBot.py @@ -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() diff --git a/tickerInfo.py b/tickerInfo.py index 2622741..8822152 100644 --- a/tickerInfo.py +++ b/tickerInfo.py @@ -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