diff --git a/wallamanta/Dockerfile b/wallamanta/Dockerfile index f3ea6dd..5ad9b38 100644 --- a/wallamanta/Dockerfile +++ b/wallamanta/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7 +FROM python:3.11 RUN mkdir /app ADD . /app diff --git a/wallamanta/helpers.py b/wallamanta/helpers.py index f3a4e7b..93eb5c8 100644 --- a/wallamanta/helpers.py +++ b/wallamanta/helpers.py @@ -5,6 +5,7 @@ import logging import constants import pytz import walladb +import asyncio from newrelic_telemetry_sdk import Event, EventClient from PIL import Image, ImageDraw, ImageFont @@ -58,6 +59,9 @@ def is_date_expired(until): def random_wait(): time.sleep(random.random()) +async def random_async_wait(): + await asyncio.sleep(random.random()) + def download_image(article): r = requests.get(article['images'][0]['original']) if r.status_code == 200: diff --git a/wallamanta/requirements.txt b/wallamanta/requirements.txt index 0552074..f9eff1c 100644 --- a/wallamanta/requirements.txt +++ b/wallamanta/requirements.txt @@ -1,6 +1,6 @@ -python-telegram-bot==20.1 -python-telegram-bot[job-queue]==20.1 -requests==2.28.1 +python-telegram-bot==20.4 +python-telegram-bot[job-queue]==20.4 +requests==2.31.0 prettytable==3.6.0 Pillow==9.4.0 mysql-connector-python==8.0.32 diff --git a/wallamanta/wallamanta.py b/wallamanta/wallamanta.py index 9c54b74..7090631 100644 --- a/wallamanta/wallamanta.py +++ b/wallamanta/wallamanta.py @@ -4,6 +4,7 @@ import helpers import walladb import constants import account_checker +import time from worker import Worker from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ForceReply @@ -437,24 +438,37 @@ async def list_threads(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No else: await update.message.reply_markdown_v2(helpers.telegram_escape_characters(f"{threads_string}")) +def count_threads(): + time.sleep(10) + while True: + logging.info(f"=== There are: {threading.active_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=account_checker.account_checker, args=(3600, )) p.start() - """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() + p = threading.Thread(target=count_threads) + p.start() # on different commands - answer in Telegram application.add_handler(CommandHandler("help", help_command)) diff --git a/wallamanta/worker.py b/wallamanta/worker.py index e1479bf..60fc6b8 100644 --- a/wallamanta/worker.py +++ b/wallamanta/worker.py @@ -59,9 +59,9 @@ class Worker: return search_objects def first_run(self, product): - logging.info(f"First run for {product['product_name']} for {walladb.get_user(product['telegram_user_id'])} ({walladb.get_user(product['telegram_user_id'])})") - for i in range(5): + for _ in range(10): helpers.random_wait() + logging.info(f"First run for {product['product_name']} for {walladb.get_user(product['telegram_user_id'])} ({walladb.get_user(product['telegram_user_id'])})") list = [] if not helpers.is_valid_request(product): return list @@ -95,7 +95,7 @@ class Worker: return list async def work(self, product, list): - helpers.random_wait() # Random wait to make requests separated in time in order to prevent API rate limit + await helpers.random_async_wait() # Random wait to make requests separated in time in order to prevent API rate limit exec_times = [] while True: #logging.info(f"List for {product['product_name']} length is: {len(list)}")