can resolve captcha with 2captcha
This commit is contained in:
+39
-68
@@ -1,82 +1,53 @@
|
|||||||
import random
|
|
||||||
# for recaptcha
|
# for recaptcha
|
||||||
import urllib
|
import logging
|
||||||
from speech_recognition import Recognizer, AudioFile
|
|
||||||
import random
|
import random
|
||||||
import os
|
import re
|
||||||
|
import time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
CAPCHA_NOT_READY = "CAPCHA_NOT_READY"
|
||||||
|
REGEX_DATA_SITE_KEY = "data-sitekey=[\"a-z0-9A-Z]+"
|
||||||
|
|
||||||
|
|
||||||
class SolveCaptcha:
|
class SolveCaptcha:
|
||||||
def __init__(self, page):
|
def __init__(self, page):
|
||||||
self.page = page
|
self.page = page
|
||||||
|
self.logger = logging.getLogger("SolveCaptcha")
|
||||||
self.main_frame = None
|
self.main_frame = None
|
||||||
self.recaptcha = None
|
self.recaptcha = None
|
||||||
|
|
||||||
def delay(self):
|
def delay(self):
|
||||||
self.page.wait_for_timeout(random.randint(1, 3) * 1000)
|
self.page.wait_for_timeout(random.randint(1, 3) * 1000)
|
||||||
|
|
||||||
def presetup(self):
|
def start(self, handle_solution_received):
|
||||||
name = self.page.locator(
|
self.logger.info("start to resolve captcha")
|
||||||
"//iframe[@title='reCAPTCHA']").get_attribute("name")
|
content = self.page.content()
|
||||||
self.recaptcha = self.page.frame(name=name)
|
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)
|
||||||
|
|
||||||
self.recaptcha.click("//div[@class='recaptcha-checkbox-border']")
|
def solve_captcha(self, google_key: str, handle_solution_received):
|
||||||
self.delay()
|
self.logger.info("solve_captcha()")
|
||||||
s = self.recaptcha.locator("//span[@id='recaptcha-anchor']")
|
url_get = "http://2captcha.com/in.php?key=e7e3cd0977aba2dab49e0ea052ca58b1&method=userrecaptcha&googlekey={}&pageurl={}".format(
|
||||||
if s.get_attribute("aria-checked") != "false": # solved already
|
google_key, self.page.url)
|
||||||
return
|
res = requests.get(url_get)
|
||||||
|
print(res.text)
|
||||||
# self.main_frame = self.page.frame(name=self.page.locator(
|
results = res.text.split("|")
|
||||||
# "//iframe[contains(@src,'https://www.google.com/recaptcha/api2/bframe?')]").get_attribute("name"))
|
self.captcha_id = results[-1]
|
||||||
# self.main_frame.click("id=recaptcha-audio-button")
|
# wait for 15 seconds
|
||||||
|
time.sleep(15)
|
||||||
def start(self):
|
# get result of the captcha
|
||||||
print("start to resolve captcha")
|
url_response = "http://2captcha.com/res.php?key=e7e3cd0977aba2dab49e0ea052ca58b1&action=get&id={}".format(
|
||||||
self.presetup()
|
self.captcha_id)
|
||||||
tries = 0
|
solution = CAPCHA_NOT_READY
|
||||||
while (tries <= 5):
|
while solution == CAPCHA_NOT_READY:
|
||||||
self.delay()
|
solution_res = requests.get(url_response)
|
||||||
try:
|
time.sleep(5)
|
||||||
self.solve_captcha()
|
solution = solution_res.text
|
||||||
except Exception as e:
|
print(solution)
|
||||||
print("exception:")
|
handle_solution_received(solution.split("|")[-1])
|
||||||
print(e)
|
|
||||||
self.main_frame.click("id=recaptcha-reload-button")
|
|
||||||
else:
|
|
||||||
s = self.recaptcha.locator("//span[@id='recaptcha-anchor']")
|
|
||||||
if s.get_attribute("aria-checked") != "false":
|
|
||||||
self.page.click("id=recaptcha-demo-submit")
|
|
||||||
self.delay()
|
|
||||||
break
|
|
||||||
tries += 1
|
|
||||||
|
|
||||||
def solve_captcha(self):
|
|
||||||
print("solve_captcha()")
|
|
||||||
# self.main_frame.click(
|
|
||||||
# "//button[@aria-labelledby='audio-instructions rc-response-label']")
|
|
||||||
# href = self.main_frame.locator(
|
|
||||||
# "//a[@class='rc-audiochallenge-tdownload-link']").get_attribute("href")
|
|
||||||
#
|
|
||||||
# print("retrieve mp3 file")
|
|
||||||
# urllib.request.urlretrieve(href, "audio.mp3")
|
|
||||||
#
|
|
||||||
# sound = pydub.AudioSegment.from_mp3(
|
|
||||||
# "audio.mp3").export("audio.wav", format="wav")
|
|
||||||
#
|
|
||||||
# recognizer = Recognizer()
|
|
||||||
#
|
|
||||||
# recaptcha_audio = AudioFile("audio.wav")
|
|
||||||
# with recaptcha_audio as source:
|
|
||||||
# audio = recognizer.record(source)
|
|
||||||
# print("recognizer mp3 file")
|
|
||||||
#
|
|
||||||
# text = recognizer.recognize_google(audio)
|
|
||||||
# print("recognized text: " + text)
|
|
||||||
# self.main_frame.fill("id=audio-response", text)
|
|
||||||
# self.main_frame.click("id=recaptcha-verify-button")
|
|
||||||
# self.delay()
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
pass
|
|
||||||
# os.remove("audio.mp3")
|
|
||||||
# os.remove("audio.wav")
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import threading
|
|||||||
import time
|
import time
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import requests
|
|
||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
import params
|
import params
|
||||||
@@ -20,8 +19,6 @@ RDV_URL = "https://rendezvousparis.hermes.com/client/register"
|
|||||||
# RDV_URL = "file:///Users/lpan/Downloads/test_appointment.html"
|
# RDV_URL = "file:///Users/lpan/Downloads/test_appointment.html"
|
||||||
# RDV_URL = "https://api.ipify.org"
|
# RDV_URL = "https://api.ipify.org"
|
||||||
# RDV_URL ="https://bot.sannysoft.com/"
|
# RDV_URL ="https://bot.sannysoft.com/"
|
||||||
REGEX_DATA_SITE_KEY = "data-sitekey=[\"a-z0-9A-Z]+"
|
|
||||||
CAPCHA_NOT_READY = "CAPCHA_NOT_READY"
|
|
||||||
REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+"
|
REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+"
|
||||||
otp_value = None
|
otp_value = None
|
||||||
OTP_FIELD_ID = "#sms_code"
|
OTP_FIELD_ID = "#sms_code"
|
||||||
@@ -96,7 +93,7 @@ class CommandorPage:
|
|||||||
}
|
}
|
||||||
self.fill_fields()
|
self.fill_fields()
|
||||||
# wait the captha element
|
# wait the captha element
|
||||||
self.find_data_sitekey()
|
self.resolve_captcha()
|
||||||
# wait for sms_code field
|
# wait for sms_code field
|
||||||
# self.clickOnValidBtn()
|
# self.clickOnValidBtn()
|
||||||
self.thread_event = e
|
self.thread_event = e
|
||||||
@@ -293,14 +290,6 @@ class CommandorPage:
|
|||||||
def clear_app_data(self):
|
def clear_app_data(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def click_recapcha_checkbox(self):
|
|
||||||
captcha_solver = SolveCaptcha(self.page)
|
|
||||||
captcha_solver.start()
|
|
||||||
del captcha_solver
|
|
||||||
# checkbox = self.page.wait_for_selector(
|
|
||||||
# "#recaptcha-anchor")
|
|
||||||
# checkbox.click()
|
|
||||||
|
|
||||||
def fill_otp(self, otp: str):
|
def fill_otp(self, otp: str):
|
||||||
self.page.focus(OTP_FIELD_ID)
|
self.page.focus(OTP_FIELD_ID)
|
||||||
time.sleep(get_random_wait_time())
|
time.sleep(get_random_wait_time())
|
||||||
@@ -323,36 +312,9 @@ class CommandorPage:
|
|||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
self.browser.close()
|
self.browser.close()
|
||||||
|
|
||||||
def find_data_sitekey(self):
|
def resolve_captcha(self):
|
||||||
content = self.page.content()
|
self.captcha_solver = SolveCaptcha(self.page)
|
||||||
data_sitekey = re.findall(REGEX_DATA_SITE_KEY, content)
|
self.captcha_solver.start(self.fill_captcha_solution)
|
||||||
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.send_to_resolve_captcha(key)
|
|
||||||
|
|
||||||
def send_to_resolve_captcha(self, google_key: str):
|
|
||||||
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)
|
|
||||||
print(solution_res.text)
|
|
||||||
time.sleep(5)
|
|
||||||
solution = solution_res.text
|
|
||||||
print(solution)
|
|
||||||
self.fill_captcha_solution(solution.split("|")[-1])
|
|
||||||
|
|
||||||
def fill_captcha_solution(self, solution):
|
def fill_captcha_solution(self, solution):
|
||||||
self.logger.info("will input solution: " + solution)
|
self.logger.info("will input solution: " + solution)
|
||||||
|
|||||||
Reference in New Issue
Block a user