|
|
|
|
@@ -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()
|
|
|
|
|
|