Updated to ptb 21.0.1 and added socks5 proxy support

This commit is contained in:
Joan
2024-03-24 19:08:37 +01:00
parent 583cca776f
commit 23e5cfd878
6 changed files with 33 additions and 14 deletions

View File

@@ -1,4 +1,4 @@
FROM python:3.7 FROM python:3.11
# Adding trusting keys to apt for repositories # Adding trusting keys to apt for repositories
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -

View File

@@ -14,7 +14,7 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from selenium import webdriver from selenium import webdriver
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup 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 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 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: try:
msg = update.message.text msg = update.message.text
except AttributeError: 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 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") 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 return
logging.info(f"Product information found: {product_data}") 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) product_id = dbhelper.check_product(amazon_url_with_referer, product_data.price)
if not product_id: 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) 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) helpers.create_image(product_id, product_data.price)
keyboard = [[InlineKeyboardButton("Ir a Amazon", url=f"{amazon_url_with_referer}")]] keyboard = [[InlineKeyboardButton("Ir a Amazon", url=f"{amazon_url_with_referer}")]]
markup = InlineKeyboardMarkup(keyboard) 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: def main() -> None:
dbhelper.setup_db() dbhelper.setup_db()
updater = Updater(constants.TELEGRAM_API_KEY) if constants.telegram_proxy:
dispatcher = updater.dispatcher logging.info("Creating application with socks5 proxy")
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, find_amazon_link)) 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()
updater.start_polling() else:
updater.idle() 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__': if __name__ == '__main__':
main() main()

View File

@@ -8,6 +8,14 @@ HEADERS = ({'User-Agent':
'Accept-Language': 'en-US, en;q=0.5'}) 'Accept-Language': 'en-US, en;q=0.5'})
DB = '/app/data/amazon.db' 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."): if baseURL.startswith("https://www."):
searchURL = baseURL[12:] searchURL = baseURL[12:]
elif baseURL.startswith("http://www."): elif baseURL.startswith("http://www."):

View File

@@ -10,6 +10,9 @@ logging.basicConfig(
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
httpx_logger = logging.getLogger('httpx')
httpx_logger.setLevel(logging.WARNING)
def create_image(product_id, price): def create_image(product_id, price):
width = 1280 width = 1280
height = 800 height = 800

View File

@@ -1,4 +1,4 @@
python-telegram-bot==13.13 python-telegram-bot[socks]==21.0.1
requests==2.28.1 requests==2.28.1
beautifulsoup4==4.11.1 beautifulsoup4==4.11.1
lxml==4.9.1 lxml==4.9.1

View File

@@ -7,8 +7,13 @@ services:
container_name: bot-amazon-telegram container_name: bot-amazon-telegram
volumes: volumes:
- ./data:/app/data - ./data:/app/data
restart: unless-stopped restart: always
environment: environment:
- TELEGRAM_API_KEY=${TELEGRAM_API_KEY} - TELEGRAM_API_KEY=${TELEGRAM_API_KEY}
- baseURL=${baseURL} - baseURL=${baseURL}
- affiliate_tag=${affiliate_tag} - 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}