Files
appointment_tool/workers/SolveCaptch.py
T
2022-05-23 13:12:55 +02:00

54 lines
1.9 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]+"
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("\"", '')
print("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=e7e3cd0977aba2dab49e0ea052ca58b1&method=userrecaptcha&googlekey={}&pageurl={}".format(
google_key, self.page.url)
res = requests.get(url_get)
print(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=e7e3cd0977aba2dab49e0ea052ca58b1&action=get&id={}".format(
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])