Added captcha solving method
This commit is contained in:
31
bot/bot.py
31
bot/bot.py
@@ -5,8 +5,12 @@ import dbhelper
|
||||
import constants
|
||||
import helpers
|
||||
import parser
|
||||
import time
|
||||
|
||||
from amazoncaptcha import AmazonCaptcha
|
||||
from bs4 import BeautifulSoup
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
from selenium import webdriver
|
||||
from telegram import Update, ForceReply
|
||||
@@ -56,15 +60,36 @@ def find_amazon_link(update: Update, context: CallbackContext) -> None:
|
||||
driver.get(msg)
|
||||
logging.info("Scraping information and closing browser")
|
||||
soup = BeautifulSoup(driver.page_source, "lxml")
|
||||
driver.close()
|
||||
|
||||
logging.info("Getting title...")
|
||||
title, price, image = parser.get_title(soup), parser.get_price(soup), parser.get_image(soup)
|
||||
if title == "":
|
||||
logging.info(f"Title not found, not a valid product")
|
||||
return
|
||||
logging.info(f"Title not found, not a valid product or captcha")
|
||||
captcha = AmazonCaptcha.fromdriver(driver)
|
||||
solution = captcha.solve()
|
||||
if solution == 'Not solved':
|
||||
logging.info("Couldn't solve the captcha, if there was any")
|
||||
else:
|
||||
logging.info(f"Captcha solution is {solution}, redirecting")
|
||||
logging.info("Waiting for 5 seconds, humans are not that fast :)")
|
||||
time.sleep(5)
|
||||
fill_captcha_element = driver.find_element(By.ID, 'captchacharacters')
|
||||
fill_captcha_element.send_keys(solution)
|
||||
fill_captcha_element.send_keys(Keys.RETURN)
|
||||
logging.info("Re-loading Amazon webpage")
|
||||
driver.get(msg)
|
||||
logging.info("Scraping information")
|
||||
soup = BeautifulSoup(driver.page_source, "lxml")
|
||||
logging.info("Getting title...")
|
||||
title, price, image = parser.get_title(soup), parser.get_price(soup), parser.get_image(soup)
|
||||
if title == "":
|
||||
logging.info(f"Title not found, not a valid product or failed captcha")
|
||||
return
|
||||
logging.info(f"Title found: {title}")
|
||||
|
||||
logging.info("Closing browser")
|
||||
driver.close()
|
||||
|
||||
context.bot.deleteMessage(chat_id=chat['id'], message_id=message_id)
|
||||
referurl = helpers.new_refer_url(productCode, extraparams)
|
||||
product_id = dbhelper.check_product(referurl, price)
|
||||
|
||||
Reference in New Issue
Block a user