-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrecaptcha.py
103 lines (89 loc) · 2.94 KB
/
recaptcha.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import os
import time
import requests
class CaptchaInterface:
def cap(
self,
websiteURL: str,
websiteKey: str,
type: str = 'ReCaptchaV2TaskProxyless',
isInvisible: bool = False
) -> str:
raise NotImplementedError()
class EzCaptchaImpl(CaptchaInterface):
def __init__(self) -> None:
super().__init__()
self.client_key = os.environ.get("EZCAPTCHA_CLIENT_KEY", default='')
if self.client_key == '':
raise EnvironmentError("缺少环境变量 EZCAPTCHA_CLIENT_KEY")
def __create_task(
self,
websiteURL: str,
websiteKey: str,
pageAction: str,
type: str = 'ReCaptchaV3TaskProxyless',
isInvisible: bool = False,
) -> str:
url = "https://api.ez-captcha.com/createTask"
payload = json.dumps({
"clientKey": self.client_key,
"task": {
"websiteURL": websiteURL,
"websiteKey": websiteKey,
"type": type,
"isInvisible": isInvisible,
"pageAction": pageAction
}
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
data = json.loads(response.text)
if data['errorId']:
raise ValueError(data)
return data['taskId']
def __get_task_result(self, task_id: str) -> str:
url = "https://api.ez-captcha.com/getTaskResult"
payload = json.dumps({
"clientKey": self.client_key,
"taskId": task_id
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
data = json.loads(response.text)
if data['errorId']:
raise ValueError(data)
if data['status'] == 'processing':
print('.', end='')
time.sleep(3)
return self.__get_task_result(task_id)
elif data['status'] == 'ready':
return data['solution']['gRecaptchaResponse']
else:
raise ValueError(data)
def cap(
self,
websiteURL: str,
websiteKey: str,
pageAction: str,
type: str = 'ReCaptchaV3TaskProxyless',
isInvisible: bool = False,
) -> str:
task_id = self.__create_task(websiteURL,websiteKey,pageAction,type,isInvisible)
print('wait cap', end='')
result = self.__get_task_result(task_id)
print('done')
return result
if __name__ == '__main__':
captcha = EzCaptchaImpl()
res = captcha.cap(
websiteURL='https://2dfan.com/',
websiteKey='6LdUG0AgAAAAAAfSmLDXGMM7XKYMTItv87seZUan',
pageAction="checkin",
isInvisible=True,
)
print(res)