Modified a lot of things. Now it works with ThreadPoolExecutor
This commit is contained in:
@@ -5,8 +5,8 @@ import walladb
|
||||
import constants
|
||||
import account_checker
|
||||
import time
|
||||
import search_manager
|
||||
|
||||
from worker import Worker
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ForceReply
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.ext import (
|
||||
@@ -33,6 +33,9 @@ logging.basicConfig(
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
httpx_logger = logging.getLogger('httpx')
|
||||
httpx_logger.setLevel(logging.WARNING)
|
||||
|
||||
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
if walladb.is_user_valid(helpers.get_telegram_user_id(update)):
|
||||
message = "Utiliza el menú de la conversación para añadir un producto y sigue los pasos indicados. Si tienes cualquier duda contacta con @jocarduck para más información."
|
||||
@@ -315,9 +318,6 @@ async def remove_product(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
if walladb.remove_product({'product_name' : product_name, \
|
||||
'telegram_user_id' : telegram_user_id}):
|
||||
message = f"¡{product_name} borrado de la lista de seguimiento!"
|
||||
#product_thread = helpers.get_thread(product_name)
|
||||
#if product_thread != None:
|
||||
# product_thread.stop()
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=message)
|
||||
return ConversationHandler.END
|
||||
|
||||
@@ -361,12 +361,6 @@ async def add_premium_user_command(update: Update, context: ContextTypes.DEFAULT
|
||||
days = update.message.text.split('/add_premium_user ')[1].split(' ')[1]
|
||||
until = helpers.get_date_ahead(int(days))
|
||||
if not walladb.add_premium_user(telegram_user_id, until):
|
||||
products = walladb.get_products_from_user(telegram_user_id)
|
||||
|
||||
for product in products:
|
||||
logging.info(product)
|
||||
p = threading.Thread(target=Worker.run, args=(product, ))
|
||||
p.start()
|
||||
await update.message.reply_markdown_v2(helpers.telegram_escape_characters(f"{telegram_user_id} re-activado hasta {until}. Re-activando productos."))
|
||||
else:
|
||||
await update.message.reply_markdown_v2(helpers.telegram_escape_characters(f"{telegram_user_id} añadido hasta {until}."))
|
||||
@@ -404,12 +398,8 @@ async def test_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
||||
await update.message.reply_markdown_v2(helpers.telegram_escape_characters(message))
|
||||
|
||||
async def add_to_db_and_send(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
global SEARCH_THREADS_LIST
|
||||
logging.info(f"Adding product with context: {context.user_data}")
|
||||
walladb.add_product(context.user_data)
|
||||
p = threading.Thread(target=Worker.run, args=(walladb.get_product(context.user_data), ))
|
||||
p.start()
|
||||
SEARCH_THREADS_LIST.append((context.user_data, p))
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"¡*{helpers.telegram_escape_characters(context.user_data['product_name'])}* añadido correctamente\!", parse_mode=ParseMode.MARKDOWN_V2)
|
||||
|
||||
def error(update, context):
|
||||
@@ -445,24 +435,14 @@ def count_threads():
|
||||
time.sleep(60)
|
||||
|
||||
def main()->None:
|
||||
global SEARCH_THREADS_LIST
|
||||
walladb.setup_db()
|
||||
products = walladb.get_all_products()
|
||||
|
||||
"""Start the bot."""
|
||||
# Create the Application and pass it your bot's token.
|
||||
application = Application.builder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_TOKEN).build()
|
||||
|
||||
logging.info(f"Loading {len(products)} products...")
|
||||
count = 0
|
||||
for product in products:
|
||||
if helpers.is_valid_request(product):
|
||||
count = count + 1
|
||||
logging.info(product)
|
||||
p = threading.Thread(target=Worker.run, args=(product, ))
|
||||
p.start()
|
||||
SEARCH_THREADS_LIST.append((product, p))
|
||||
logging.info(f"{count} products finally loaded")
|
||||
p = threading.Thread(target=search_manager.work)
|
||||
p.start()
|
||||
|
||||
p = threading.Thread(target=account_checker.work, args=(3600, ))
|
||||
p.start()
|
||||
|
||||
Reference in New Issue
Block a user