From addf9302c670cf243ca4eca90ab843546de29348 Mon Sep 17 00:00:00 2001 From: Joan Cano Date: Tue, 4 Jul 2023 22:39:26 +0200 Subject: [PATCH] Fixed account_checker --- wallamanta/account_checker.py | 23 +++++++++++++++++++---- wallamanta/walladb.py | 10 +++++----- wallamanta/wallamanta.py | 8 ++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/wallamanta/account_checker.py b/wallamanta/account_checker.py index 37d5d40..c36d8cf 100644 --- a/wallamanta/account_checker.py +++ b/wallamanta/account_checker.py @@ -1,14 +1,29 @@ import helpers import walladb -import time +import asyncio +import logging -def account_checker(sleep_time): +# Enable logging +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO +) + +async def work(sleep_time): while True: user_list = walladb.get_user_list() for user in user_list: if walladb.is_user_valid(user['telegram_user_id']): if walladb.is_user_expired(user['telegram_user_id']): message = "¡Hola!\n\nTu cuenta ha caducado. Si quieres seguir usando @wallamanta_bot ponte en contacto con @jocarduck\n\n¡Gracias!" - helpers.send_message(user['telegram_user_id'], message) + await helpers.send_message(user['telegram_user_id'], message) walladb.deactivate_user(user['telegram_user_id']) - time.sleep(sleep_time) + await asyncio.sleep(sleep_time) + +def account_checker(sleep_time): + logging.info(f"Account checker starting... Checking every {sleep_time} seconds") + while True: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + loop.run_until_complete(work(sleep_time)) + loop.close() + diff --git a/wallamanta/walladb.py b/wallamanta/walladb.py index 06a767d..5409263 100644 --- a/wallamanta/walladb.py +++ b/wallamanta/walladb.py @@ -35,7 +35,7 @@ def setup_db(): " `type` varchar(50) NOT NULL," " `until` date NOT NULL," " `telegram_name` varchar(255) NOT NULL," - " `created_at` timestamp DEFAULT CURRENT_TIMESTAMP" + " `created_at` timestamp DEFAULT CURRENT_TIMESTAMP," " PRIMARY KEY (`telegram_user_id`)" ") ENGINE=InnoDB") @@ -171,9 +171,9 @@ def remove_valid_user(telegram_user_id): def get_user_list(): con = connect_db() - cur = con.cursor(prepared=True) + cur = con.cursor(dictionary=True, prepared=True) res = cur.execute(f"SELECT * FROM users") - ret = res.fetchall() + ret = cur.fetchall() con.close() return ret @@ -222,7 +222,7 @@ def deactivate_user(telegram_user_id): cur.execute(f"UPDATE users SET active=False WHERE telegram_user_id=%s", params) con.commit() con.close() - logging.info(f"De-activated user {helpers.get_telegram_user_name(telegram_user_id)} ({telegram_user_id})") + logging.info(f"De-activated user {get_user(telegram_user_id)} ({telegram_user_id})") def activate_user(telegram_user_id): con = connect_db() @@ -231,7 +231,7 @@ def activate_user(telegram_user_id): cur.execute(f"UPDATE users SET active=True WHERE telegram_user_id=%s", params) con.commit() con.close() - logging.info(f"Activated user {helpers.get_telegram_user_name(telegram_user_id)} ({telegram_user_id})") + logging.info(f"Activated user {get_user(telegram_user_id)} ({telegram_user_id})") def get_product(product): product_name = product.get('product_name').lower() diff --git a/wallamanta/wallamanta.py b/wallamanta/wallamanta.py index c4cc34e..c7a06d9 100644 --- a/wallamanta/wallamanta.py +++ b/wallamanta/wallamanta.py @@ -314,9 +314,9 @@ 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() + #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 @@ -446,7 +446,7 @@ def main()->None: p.start() SEARCH_THREADS_LIST.append((product, p)) - p = threading.Thread(target=account_checker.account_checker, args=(3600)) + p = threading.Thread(target=account_checker.account_checker, args=(3600, )) p.start() """Start the bot."""