-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha_worker.py
More file actions
28 lines (25 loc) · 1.29 KB
/
captcha_worker.py
File metadata and controls
28 lines (25 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import httpx
import asyncio
class CaptchaSolver:
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "http://2captcha.com"
async def solve_recaptcha(self, site_key: str, url: str) -> str:
async with httpx.AsyncClient() as client:
submit_url = f"{self.base_url}/in.php?key={self.api_key}&method=userrecaptcha&googlekey={site_key}&pageurl={url}&json=1"
resp = await client.get(submit_url)
res_data = resp.json()
if res_data.get("status") != 1:
raise Exception(f"Captcha submission failed: {res_data.get('request')}")
task_id = res_data.get("request")
for _ in range(30):
await asyncio.sleep(5)
check_url = f"{self.base_url}/res.php?key={self.api_key}&action=get&id={task_id}&json=1"
check_resp = await client.get(check_url)
check_data = check_resp.json()
if check_data.get("status") == 1:
return check_data.get("request")
if check_data.get("request") == "CAPCHA_NOT_READY":
continue
raise Exception(f"Captcha error: {check_data.get('request')}")
raise Exception("Captcha timeout")