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

code cleanup

This commit is contained in:
Anson Biggs 2021-02-06 14:13:24 -07:00
parent a5505ab30c
commit 554928db7e
2 changed files with 52 additions and 35 deletions

61
bot.py
View File

@ -10,7 +10,12 @@ import traceback
import mplfinance as mpf import mplfinance as mpf
import telegram import telegram
from telegram import InlineQueryResultArticle, InputTextMessageContent, LabeledPrice from telegram import (
InlineQueryResultArticle,
InputTextMessageContent,
LabeledPrice,
Update,
)
from telegram.ext import ( from telegram.ext import (
CommandHandler, CommandHandler,
Filters, Filters,
@ -18,6 +23,7 @@ from telegram.ext import (
MessageHandler, MessageHandler,
PreCheckoutQueryHandler, PreCheckoutQueryHandler,
Updater, Updater,
CallbackContext,
) )
from functions import Symbol from functions import Symbol
@ -45,24 +51,24 @@ logger = logging.getLogger(__name__)
print("Bot Online") print("Bot Online")
def start(update, context): def start(update: Update, context: CallbackContext):
"""Send a message when the command /start is issued.""" """Send a message when the command /start is issued."""
update.message.reply_text(text=s.help_text, parse_mode=telegram.ParseMode.MARKDOWN) update.message.reply_text(text=s.help_text, parse_mode=telegram.ParseMode.MARKDOWN)
def help(update, context): def help(update: Update, context: CallbackContext):
"""Send link to docs when the command /help is issued.""" """Send link to docs when the command /help is issued."""
update.message.reply_text(text=s.help_text, parse_mode=telegram.ParseMode.MARKDOWN) update.message.reply_text(text=s.help_text, parse_mode=telegram.ParseMode.MARKDOWN)
def license(update, context): def license(update: Update, context: CallbackContext):
"""Return bots license agreement""" """Return bots license agreement"""
update.message.reply_text(text=s.license, parse_mode=telegram.ParseMode.MARKDOWN) update.message.reply_text(text=s.license, parse_mode=telegram.ParseMode.MARKDOWN)
def status(update, context): def status(update: Update, context: CallbackContext):
message = "" message = ""
try: try:
# Bot Status # Bot Status
@ -77,12 +83,15 @@ def status(update, context):
# Message Status # Message Status
message += s.message_status() message += s.message_status()
except Exception as ex: except Exception as ex:
message += f"*\n\nERROR ENCOUNTERED:*\n{ex}\n\n*The bot encountered an error while attempting to find errors. Please contact the bot admin.*" message += (
f"*\n\nERROR ENCOUNTERED:*\n{ex}\n\n"
+ "*The bot encountered an error while attempting to find errors. Please contact the bot admin.*"
)
update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN)
def donate(update, context): def donate(update: Update, context: CallbackContext):
chat_id = update.message.chat_id chat_id = update.message.chat_id
if update.message.text.strip() == "/donate": if update.message.text.strip() == "/donate":
@ -122,7 +131,7 @@ def donate(update, context):
) )
def precheckout_callback(update, context): def precheckout_callback(update: Update, context: CallbackContext):
query = update.pre_checkout_query query = update.pre_checkout_query
if query.invoice_payload == "simple-stock-bot": if query.invoice_payload == "simple-stock-bot":
@ -132,11 +141,11 @@ def precheckout_callback(update, context):
query.answer(ok=False, error_message="Something went wrong...") query.answer(ok=False, error_message="Something went wrong...")
def successful_payment_callback(update, context): def successful_payment_callback(update: Update, context: CallbackContext):
update.message.reply_text("Thank you for your donation!") update.message.reply_text("Thank you for your donation!")
def symbol_detect(update, context): def symbol_detect(update: Update, context: CallbackContext):
""" """
Runs on any message that doesn't have a command and searches for symbols, Runs on any message that doesn't have a command and searches for symbols,
then returns the prices of any symbols found. then returns the prices of any symbols found.
@ -156,7 +165,7 @@ def symbol_detect(update, context):
) )
def dividend(update, context): def dividend(update: Update, context: CallbackContext):
""" """
waits for /dividend or /div command and then finds dividend info on that symbol. waits for /dividend or /div command and then finds dividend info on that symbol.
""" """
@ -174,7 +183,7 @@ def dividend(update, context):
) )
def news(update, context): def news(update: Update, context: CallbackContext):
""" """
waits for /news command and then finds news info on that symbol. waits for /news command and then finds news info on that symbol.
""" """
@ -191,7 +200,7 @@ def news(update, context):
) )
def info(update, context): def info(update: Update, context: CallbackContext):
""" """
waits for /info command and then finds info on that symbol. waits for /info command and then finds info on that symbol.
""" """
@ -208,7 +217,7 @@ def info(update, context):
) )
def search(update, context): def search(update: Update, context: CallbackContext):
message = update.message.text.replace("/search ", "") message = update.message.text.replace("/search ", "")
queries = s.search_symbols(message)[:6] queries = s.search_symbols(message)[:6]
@ -219,7 +228,7 @@ def search(update, context):
update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN) update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN)
def intra(update, context): def intra(update: Update, context: CallbackContext):
# TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices # TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices
message = update.message.text message = update.message.text
@ -238,8 +247,6 @@ def intra(update, context):
chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO
) )
price = s.price_reply([symbol])
buf = io.BytesIO() buf = io.BytesIO()
mpf.plot( mpf.plot(
df, df,
@ -254,12 +261,14 @@ def intra(update, context):
update.message.reply_photo( update.message.reply_photo(
photo=buf, photo=buf,
caption=f"\nIntraday chart for ${symbol.upper()} from {df.first_valid_index().strftime('%I:%M')} to {df.last_valid_index().strftime('%I:%M')} ET on {datetime.date.today().strftime('%d, %b %Y')}\n\n{price[symbol]}", caption=f"\nIntraday chart for ${symbol.upper()} from {df.first_valid_index().strftime('%I:%M')} to"
+ " {df.last_valid_index().strftime('%I:%M')} ET on"
+ " {datetime.date.today().strftime('%d, %b %Y')}\n\n{s.price_reply([symbol])[symbol]}",
parse_mode=telegram.ParseMode.MARKDOWN, parse_mode=telegram.ParseMode.MARKDOWN,
) )
def chart(update, context): def chart(update: Update, context: CallbackContext):
# TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices # TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices
message = update.message.text message = update.message.text
@ -278,7 +287,6 @@ def chart(update, context):
chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO
) )
price = s.price_reply([symbol])
buf = io.BytesIO() buf = io.BytesIO()
mpf.plot( mpf.plot(
df, df,
@ -292,12 +300,13 @@ def chart(update, context):
update.message.reply_photo( update.message.reply_photo(
photo=buf, photo=buf,
caption=f"\n1 Month chart for ${symbol.upper()} from {df.first_valid_index().strftime('%d, %b %Y')} to {df.last_valid_index().strftime('%d, %b %Y')}\n\n{price[symbol]}", caption=f"\n1 Month chart for ${symbol.upper()} from {df.first_valid_index().strftime('%d, %b %Y')}"
+ " to {df.last_valid_index().strftime('%d, %b %Y')}\n\n{s.price_reply([symbol])[symbol]}",
parse_mode=telegram.ParseMode.MARKDOWN, parse_mode=telegram.ParseMode.MARKDOWN,
) )
def stat(update, context): def stat(update: Update, context: CallbackContext):
""" """
https://iexcloud.io/docs/api/#key-stats https://iexcloud.io/docs/api/#key-stats
""" """
@ -314,7 +323,7 @@ def stat(update, context):
) )
def crypto(update, context): def crypto(update: Update, context: CallbackContext):
""" """
https://iexcloud.io/docs/api/#cryptocurrency-quote https://iexcloud.io/docs/api/#cryptocurrency-quote
""" """
@ -334,7 +343,7 @@ def crypto(update, context):
) )
def inline_query(update, context): def inline_query(update: Update, context: CallbackContext):
""" """
Handles inline query. Handles inline query.
Does a fuzzy search on input and returns stocks that are close. Does a fuzzy search on input and returns stocks that are close.
@ -364,7 +373,7 @@ def inline_query(update, context):
return return
def rand_pick(update, context): def rand_pick(update: Update, context: CallbackContext):
choice = random.choice(list(s.symbol_list["description"])) choice = random.choice(list(s.symbol_list["description"]))
hold = ( hold = (
@ -377,7 +386,7 @@ def rand_pick(update, context):
) )
def error(update, context): def error(update: Update, context: CallbackContext):
"""Log Errors caused by Updates.""" """Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error) logger.warning('Update "%s" caused error "%s"', update, error)

View File

@ -104,7 +104,10 @@ _Donations can only be made in a chat directly with @simplestockbot_
if status["status"]["indicator"] == "none": if status["status"]["indicator"] == "none":
return "IEX Cloud is currently not reporting any issues with its API." return "IEX Cloud is currently not reporting any issues with its API."
else: else:
return f"{['status']['indicator']}: {['status']['description']}. Please check the status page for more information. https://status.iexapis.com" return (
f"{['status']['indicator']}: {['status']['description']}."
+ " Please check the status page for more information. https://status.iexapis.com"
)
def message_status(self): def message_status(self):
usage = r.get( usage = r.get(
@ -194,13 +197,16 @@ _Donations can only be made in a chat directly with @simplestockbot_
if ( if (
IEXData["isUSMarketOpen"] IEXData["isUSMarketOpen"]
or (IEXData["extendedChangePercent"] == None) or (IEXData["extendedChangePercent"] is None)
or (IEXData["extendedPrice"] == None) or (IEXData["extendedPrice"] is None)
): # Check if market is open. ): # Check if market is open.
message = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**" message = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**"
change = round(IEXData["changePercent"] * 100, 2) change = round(IEXData["changePercent"] * 100, 2)
else: else:
message = f"{IEXData['companyName']} closed at $**{IEXData['latestPrice']}**, after hours _(15 minutes delayed)_ stock price is $**{IEXData['extendedPrice']}**" message = (
f"{IEXData['companyName']} closed at $**{IEXData['latestPrice']}**,"
+ " after hours _(15 minutes delayed)_ stock price is $**{IEXData['extendedPrice']}**"
)
change = round(IEXData["extendedChangePercent"] * 100, 2) change = round(IEXData["extendedChangePercent"] * 100, 2)
# Determine wording of change text # Determine wording of change text
@ -286,9 +292,10 @@ _Donations can only be made in a chat directly with @simplestockbot_
if response.status_code == 200: if response.status_code == 200:
data = response.json() data = response.json()
infoMessages[ infoMessages[symbol] = (
symbol f"Company Name: [{data['companyName']}]({data['website']})\nIndustry:"
] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n" + " {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n"
)
else: else:
infoMessages[ infoMessages[
@ -321,8 +328,9 @@ _Donations can only be made in a chat directly with @simplestockbot_
except KeyError: except KeyError:
pass pass
IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/chart/1mm?token={self.IEX_TOKEN}&chartInterval=3&includeToday=false" response = r.get(
response = r.get(IEXurl) "https://cloud.iexapis.com/stable/stock/{symbol}/chart/1mm?token={self.IEX_TOKEN}&chartInterval=3&includeToday=false"
)
if response.status_code == 200: if response.status_code == 200:
df = pd.DataFrame(response.json()) df = pd.DataFrame(response.json())