chore: улучшение работы метода отправки капчи
This commit is contained in:
1 parent
b339d43d53
commit
bb4438b115
1 file changed
+28
-23
@@ -15,13 +15,38 @@ log.setLevel(logging.DEBUG)
|
|||||||
log.addHandler(logging.StreamHandler())
|
log.addHandler(logging.StreamHandler())
|
||||||
|
|
||||||
|
|
||||||
|
JS_CHALLENGE_SUBMIT = """(token) => {
|
||||||
|
const clients = window.___grecaptcha_cfg && window.___grecaptcha_cfg.clients;
|
||||||
|
if (!clients) return false;
|
||||||
|
for (const cid in clients) {
|
||||||
|
for (const key in clients[cid]) {
|
||||||
|
const obj = clients[cid][key];
|
||||||
|
for (const prop in obj) {
|
||||||
|
const cb = obj[prop] && obj[prop].callback;
|
||||||
|
if (typeof cb === 'function') { cb(token); return true; }
|
||||||
|
if (typeof cb === 'string' && typeof window[cb] === 'function') {
|
||||||
|
window[cb](token); return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}"""
|
||||||
|
|
||||||
|
|
||||||
class ReCaptchaSolver(Solver):
|
class ReCaptchaSolver(Solver):
|
||||||
|
page: Page
|
||||||
|
|
||||||
def __init__(self, captcha_solver: Captcha, **kwargs) -> None:
|
def __init__(self, captcha_solver: Captcha, **kwargs) -> None:
|
||||||
super().__init__(captcha_solver=captcha_solver, **kwargs)
|
super().__init__(captcha_solver=captcha_solver, **kwargs)
|
||||||
self.captcha_solver: Captcha = self._opt["captcha_solver"]
|
self.captcha_solver: Captcha = self._opt["captcha_solver"]
|
||||||
|
|
||||||
|
async def form_submit(self, token: str) -> bool:
|
||||||
|
# noinspection UnnecessaryLabelJS
|
||||||
|
return await self.page.evaluate(f"mw:({JS_CHALLENGE_SUBMIT})({json.dumps(token)})")
|
||||||
|
|
||||||
async def solve(self, page: Page, info: ChallengeInfo):
|
async def solve(self, page: Page, info: ChallengeInfo):
|
||||||
|
self.page = page
|
||||||
log.debug("Начинаю решение recaptcha антибот преграды")
|
log.debug("Начинаю решение recaptcha антибот преграды")
|
||||||
if info.stage == Stage.INTERSTITIAL:
|
if info.stage == Stage.INTERSTITIAL:
|
||||||
# TODO: вытащить
|
# TODO: вытащить
|
||||||
@@ -48,29 +73,9 @@ class ReCaptchaSolver(Solver):
|
|||||||
token = await self.captcha_solver.solve(info.detect_locator, page)
|
token = await self.captcha_solver.solve(info.detect_locator, page)
|
||||||
|
|
||||||
log.debug("Капча решена. Возбуждаем JS")
|
log.debug("Капча решена. Возбуждаем JS")
|
||||||
try:
|
async with page.expect_navigation(wait_until='load'):
|
||||||
async with page.expect_navigation(wait_until='load'):
|
found = await self.form_submit(token)
|
||||||
js = """(token) => {
|
assert found, "Recaptcha callback function not found"
|
||||||
const clients = window.___grecaptcha_cfg && window.___grecaptcha_cfg.clients;
|
|
||||||
if (!clients) return false;
|
|
||||||
for (const cid in clients) {
|
|
||||||
for (const key in clients[cid]) {
|
|
||||||
const obj = clients[cid][key];
|
|
||||||
for (const prop in obj) {
|
|
||||||
const cb = obj[prop] && obj[prop].callback;
|
|
||||||
if (typeof cb === 'function') { cb(token); return true; }
|
|
||||||
if (typeof cb === 'string' && typeof window[cb] === 'function') {
|
|
||||||
window[cb](token); return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}"""
|
|
||||||
found = await page.evaluate(f"mw:({js})({json.dumps(token)})")
|
|
||||||
assert found, "Recaptcha callback function not found"
|
|
||||||
except Exception as e:
|
|
||||||
log.warning("JS navigation error", exc_info=True)
|
|
||||||
|
|
||||||
log.debug("Все готово. Проверяем еще раз.")
|
log.debug("Все готово. Проверяем еще раз.")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in new issue
Block a user