55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
# for recaptcha
|
|
import logging
|
|
import random
|
|
import re
|
|
import time
|
|
import requests
|
|
|
|
CAPCHA_NOT_READY = "CAPCHA_NOT_READY"
|
|
REGEX_DATA_SITE_KEY = "data-sitekey=[\"a-z0-9A-Z]+"
|
|
API_KEY = "d66aaf490d8aa424a5175e1fbd1aadea"
|
|
|
|
|
|
class SolveCaptcha:
|
|
def __init__(self, page):
|
|
self.page = page
|
|
self.logger = logging.getLogger("SolveCaptcha")
|
|
self.main_frame = None
|
|
self.recaptcha = None
|
|
|
|
def delay(self):
|
|
self.page.wait_for_timeout(random.randint(1, 3) * 1000)
|
|
|
|
def start(self, handle_solution_received):
|
|
self.logger.info("start to resolve captcha")
|
|
content = self.page.content()
|
|
data_sitekey = re.findall(REGEX_DATA_SITE_KEY, content)
|
|
self.logger.info(data_sitekey)
|
|
if len(data_sitekey) == 1:
|
|
key_with_comma = data_sitekey[0].split("=")[-1]
|
|
key = key_with_comma.replace("\"", '')
|
|
self.logger.info("key is : " + key)
|
|
self.solve_captcha(key, handle_solution_received)
|
|
|
|
def solve_captcha(self, google_key: str, handle_solution_received):
|
|
self.logger.info("solve_captcha()")
|
|
url_get = "http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(API_KEY,
|
|
google_key,
|
|
self.page.url)
|
|
res = requests.get(url_get)
|
|
self.logger.info(res.text)
|
|
results = res.text.split("|")
|
|
self.captcha_id = results[-1]
|
|
# wait for 15 seconds
|
|
time.sleep(15)
|
|
# get result of the captcha
|
|
url_response = "http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY,
|
|
self.captcha_id)
|
|
solution = CAPCHA_NOT_READY
|
|
while solution == CAPCHA_NOT_READY:
|
|
solution_res = requests.get(url_response)
|
|
time.sleep(5)
|
|
solution = solution_res.text
|
|
print(solution)
|
|
handle_solution_received(solution.split("|")[-1])
|