From 23e5cfd87879fabd090c5ddda06512f6cad40914 Mon Sep 17 00:00:00 2001 From: Joan Date: Sun, 24 Mar 2024 19:08:37 +0100 Subject: [PATCH] Updated to ptb 21.0.1 and added socks5 proxy support --- bot/Dockerfile | 2 +- bot/bot.py | 23 +++++++++++++---------- bot/constants.py | 8 ++++++++ bot/helpers.py | 3 +++ bot/requirements.txt | 2 +- docker-compose.yml | 9 +++++++-- 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/bot/Dockerfile b/bot/Dockerfile index bc9b9c5..09a3b45 100644 --- a/bot/Dockerfile +++ b/bot/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7 +FROM python:3.11 # Adding trusting keys to apt for repositories RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - diff --git a/bot/bot.py b/bot/bot.py index 250b153..3c51937 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -14,7 +14,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium import webdriver from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup -from telegram.ext import Updater, MessageHandler, Filters, CallbackContext +from telegram.ext import ApplicationBuilder, MessageHandler, filters, CallbackContext from amazon_product import AmazonProduct @@ -96,7 +96,7 @@ def scrape_data(page_html: str) -> AmazonProduct: return AmazonProduct(title=title, price=price, image=image) if title != "" else None -def find_amazon_link(update: Update, context: CallbackContext) -> None: +async def find_amazon_link(update: Update, context: CallbackContext) -> None: try: msg = update.message.text except AttributeError: @@ -131,30 +131,33 @@ def find_amazon_link(update: Update, context: CallbackContext) -> None: if not product_data: #if after applying the captcha we don't have any data yet, stop the execution and reply to the user logging.info("Unable to get the product information") - context.bot.send_message(chat_id=chat_id, text="Unable to get product attributes from the provided url", reply_to_message_id=message_id) + await context.bot.send_message(chat_id=chat_id, text="Unable to get product attributes from the provided url", reply_to_message_id=message_id) return logging.info(f"Product information found: {product_data}") - context.bot.deleteMessage(chat_id=chat_id, message_id=message_id) + await context.bot.deleteMessage(chat_id=chat_id, message_id=message_id) product_id = dbhelper.check_product(amazon_url_with_referer, product_data.price) if not product_id: product_id = dbhelper.add_product(user['username'], chat['title'], user['id'], chat_id, msg, amazon_url_with_referer, product_data.title, product_data.price, product_data.image) helpers.create_image(product_id, product_data.price) keyboard = [[InlineKeyboardButton("Ir a Amazon", url=f"{amazon_url_with_referer}")]] markup = InlineKeyboardMarkup(keyboard) - context.bot.send_photo(chat_id=chat_id, photo=open(f"/app/data/images/products/{product_id}_composed.png", 'rb'), caption=f"URL enviada por @{user['username']}: \n\n{product_data.title}{original_message}", reply_markup=markup) + await context.bot.send_photo(chat_id=chat_id, photo=open(f"/app/data/images/products/{product_id}_composed.png", 'rb'), caption=f"URL enviada por @{user['username']}: \n\n{product_data.title}{original_message}", reply_markup=markup) def main() -> None: dbhelper.setup_db() - updater = Updater(constants.TELEGRAM_API_KEY) - dispatcher = updater.dispatcher - dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, find_amazon_link)) - updater.start_polling() - updater.idle() + if constants.telegram_proxy: + logging.info("Creating application with socks5 proxy") + application = ApplicationBuilder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_API_KEY).proxy(constants.proxy_url).get_updates_proxy(constants.proxy_url).build() + else: + logging.info("Creating application without socks5 proxy") + application = ApplicationBuilder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_API_KEY).build() + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, find_amazon_link)) + application.run_polling() if __name__ == '__main__': main() diff --git a/bot/constants.py b/bot/constants.py index a78f5d9..ce5303e 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -8,6 +8,14 @@ HEADERS = ({'User-Agent': 'Accept-Language': 'en-US, en;q=0.5'}) DB = '/app/data/amazon.db' +telegram_proxy = os.environ.get("TELEGRAM_PROXY") == '1' +if telegram_proxy: + telegram_socks_user = os.environ.get("TELEGRAM_SOCKS_USER") + telegram_socks_password = os.environ.get("TELEGRAM_SOCKS_PASSWORD") + telegram_socks_server = os.environ.get("TELEGRAM_SOCKS_SERVER") + telegram_socks_port = os.environ.get("TELEGRAM_SOCKS_PORT") + proxy_url = f"socks5://{telegram_socks_user}:{telegram_socks_password}@{telegram_socks_server}:{telegram_socks_port}" + if baseURL.startswith("https://www."): searchURL = baseURL[12:] elif baseURL.startswith("http://www."): diff --git a/bot/helpers.py b/bot/helpers.py index 930c0f2..2bfcea5 100644 --- a/bot/helpers.py +++ b/bot/helpers.py @@ -10,6 +10,9 @@ logging.basicConfig( logger = logging.getLogger(__name__) +httpx_logger = logging.getLogger('httpx') +httpx_logger.setLevel(logging.WARNING) + def create_image(product_id, price): width = 1280 height = 800 diff --git a/bot/requirements.txt b/bot/requirements.txt index 4d49785..be013ad 100644 --- a/bot/requirements.txt +++ b/bot/requirements.txt @@ -1,4 +1,4 @@ -python-telegram-bot==13.13 +python-telegram-bot[socks]==21.0.1 requests==2.28.1 beautifulsoup4==4.11.1 lxml==4.9.1 diff --git a/docker-compose.yml b/docker-compose.yml index b054de7..b740d23 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,13 @@ services: container_name: bot-amazon-telegram volumes: - ./data:/app/data - restart: unless-stopped + restart: always environment: - TELEGRAM_API_KEY=${TELEGRAM_API_KEY} - baseURL=${baseURL} - - affiliate_tag=${affiliate_tag} \ No newline at end of file + - affiliate_tag=${affiliate_tag} + - TELEGRAM_PROXY=${TELEGRAM_PROXY} + - TELEGRAM_SOCKS_USER=${TELEGRAM_SOCKS_USER} + - TELEGRAM_SOCKS_PASSWORD=${TELEGRAM_SOCKS_PASSWORD} + - TELEGRAM_SOCKS_SERVER=${TELEGRAM_SOCKS_SERVER} + - TELEGRAM_SOCKS_PORT=${TELEGRAM_SOCKS_PORT}