From 29aa9cc366e7a71e8c7e8c4aa84304ccc1ec9c60 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 23 Dec 2019 01:56:49 +0100 Subject: [PATCH] tidy up --- src/commands/config.py | 55 ++++++++++++++++----------------------- src/commands/other.py | 8 +++--- src/commands/watchlist.py | 4 +-- src/constants.py | 13 +++++++++ src/market.py | 2 +- src/mercatus.py | 5 ++-- src/utils.py | 3 +-- 7 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 src/constants.py diff --git a/src/commands/config.py b/src/commands/config.py index fc5cbf9..8a29e12 100644 --- a/src/commands/config.py +++ b/src/commands/config.py @@ -1,21 +1,17 @@ -from enum import Enum - from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove, ParseMode from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext -MENU, API_KEY, ENABLED = range(3) +from constants import Section + +MENU, API_KEY, ENABLED, DELETE = range(4) -class Section(Enum): - Watchlist = 'watchlist' # The list of Stocks/ETF to watch - Code = 'code' # Market code for a given stock, etf, etc. - API_Key = 'api_key' # Alpha Vantage API Key - Running = 'running' # Currently sending updates. Avoid overloading the API - Enabled = 'enabled' # Whether the bot should send automatic updates - Interval = 'interval' # Time axis of the graph - Frequency = 'frequency' # How ofter updates should be sent - LastRun = 'last_run' # Last time an update was sent to the user - CurrentToEdit = 'current_to_edit' # Current element to edit in the conversation handler +def init(update: Update, context: CallbackContext): + context.bot.delete_message( + chat_id=update.message.chat_id, + message_id=update.message.message_id, + ) + return show_menu(update, context) def show_menu(update: Update, context: CallbackContext): @@ -24,6 +20,7 @@ def show_menu(update: Update, context: CallbackContext): [InlineKeyboardButton( f'Turn {"off" if context.user_data.setdefault(Section.Enabled.value, True) else "on"} global auto updates', callback_data=ENABLED)], + [InlineKeyboardButton('Delete all data', callback_data=DELETE)], [InlineKeyboardButton('Done', callback_data=ConversationHandler.END)], ] update.effective_user.send_message( @@ -38,23 +35,6 @@ def show_menu(update: Update, context: CallbackContext): return MENU -def show_menu_api_key(update: Update, context: CallbackContext): - update.effective_user.send_message( - 'Send me your API Key 🙂' - '\nor /cancel', - reply_markup=ReplyKeyboardRemove() - ) - return API_KEY - - -def init(update: Update, context: CallbackContext): - context.bot.delete_message( - chat_id=update.message.chat_id, - message_id=update.message.message_id, - ) - return show_menu(update, context) - - def menu(update: Update, context: CallbackContext): selected = int(update.callback_query.data) @@ -64,14 +44,23 @@ def menu(update: Update, context: CallbackContext): ) if selected == API_KEY: - return show_menu_api_key(update, context) + return show_api_key(update, context) elif selected == ENABLED: toggle_enabled(update, context) else: return ConversationHandler.END -def set_api_key(update, context): +def show_api_key(update: Update, context: CallbackContext): + update.effective_user.send_message( + 'Send me your API Key 🙂' + '\nor /cancel', + reply_markup=ReplyKeyboardRemove() + ) + return API_KEY + + +def api_key(update, context): reply = update.message.text context.user_data[Section.API_Key.value] = reply update.message.reply_text(f'Saved {reply} 💾', reply_markup=ReplyKeyboardRemove()) @@ -96,7 +85,7 @@ config_handler = ConversationHandler( MENU: [CallbackQueryHandler(menu)], API_KEY: [ CommandHandler('cancel', cancel), - MessageHandler(Filters.all, set_api_key), + MessageHandler(Filters.all, api_key), ], }, fallbacks=[CommandHandler('cancel', cancel)] diff --git a/src/commands/other.py b/src/commands/other.py index 7b28051..6713b4e 100644 --- a/src/commands/other.py +++ b/src/commands/other.py @@ -2,14 +2,14 @@ from asyncio import sleep, run from datetime import datetime from pytimeparse import parse -from telegram import Update, ParseMode, ReplyKeyboardRemove -from telegram.ext import CallbackContext, ConversationHandler +from telegram import Update +from telegram.ext import CallbackContext from telegram.ext.dispatcher import run_async -from commands.config import Section +from constants import Section from market import Market from text import INTRO_TEXT -from utils import persistence, updater, current_timestamp, delta_timestamp, update_updater_data +from utils import persistence, updater, current_timestamp, update_updater_data SENDING = False diff --git a/src/commands/watchlist.py b/src/commands/watchlist.py index 605f2b4..d4ee505 100644 --- a/src/commands/watchlist.py +++ b/src/commands/watchlist.py @@ -1,10 +1,10 @@ from telegram import Update, InlineKeyboardButton, ParseMode, InlineKeyboardMarkup, ReplyKeyboardRemove from telegram.ext import CallbackContext, ConversationHandler, CommandHandler, CallbackQueryHandler, MessageHandler, Filters -from commands.config import Section from commands.other import send_update_to_user +from constants import Section from limited_dict import LimitedDict -from utils import parse_command, config +from utils import config ALL, SINGLE, EDIT, ADD, DELETE, BACK, ENABLED, FREQUENCY, INTERVAL, DATA = map(chr, range(10)) END = str(ConversationHandler.END) diff --git a/src/constants.py b/src/constants.py new file mode 100644 index 0000000..adb2716 --- /dev/null +++ b/src/constants.py @@ -0,0 +1,13 @@ +from enum import Enum + + +class Section(Enum): + Watchlist = 'watchlist' # The list of Stocks/ETF to watch + Code = 'code' # Market code for a given stock, etf, etc. + API_Key = 'api_key' # Alpha Vantage API Key + Running = 'running' # Currently sending updates. Avoid overloading the API + Enabled = 'enabled' # Whether the bot should send automatic updates + Interval = 'interval' # Time axis of the graph + Frequency = 'frequency' # How ofter updates should be sent + LastRun = 'last_run' # Last time an update was sent to the user + CurrentToEdit = 'current_to_edit' # Current element to edit in the conversation handler diff --git a/src/market.py b/src/market.py index c32fcd1..fdfa360 100644 --- a/src/market.py +++ b/src/market.py @@ -1,6 +1,6 @@ +from datetime import datetime from io import BytesIO from typing import BinaryIO -from datetime import datetime import matplotlib.pyplot as plt import pandas as pd diff --git a/src/mercatus.py b/src/mercatus.py index c4d927b..20fa1bb 100644 --- a/src/mercatus.py +++ b/src/mercatus.py @@ -1,10 +1,11 @@ import matplotlib as mpl from telegram.ext import CommandHandler -from utils import updater from commands.config import config_handler -from commands.watchlist import watchlist_handler from commands.other import data, send_updates, start_handler, help_handler, stop_handler, error_handler +from commands.watchlist import watchlist_handler + +from utils import updater def main(): diff --git a/src/utils.py b/src/utils.py index ec962d5..b335fc2 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,9 +1,8 @@ from datetime import datetime, timedelta -from enum import Enum -from yaml import load, Loader from telegram import Update from telegram.ext import PicklePersistence, Updater +from yaml import load, Loader DB_FILE = './data.db' CONFIG_FILE = './config.yml'