This repository was archived by the owner on Jun 26, 2023. It is now read-only.
forked from Womsxd/MihoyoBBSTools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha.py
65 lines (57 loc) · 2.47 KB
/
captcha.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
import json
import config
from request import http
from time import sleep
from loghelper import log
def captcha_check(gt: str, challenge: str, pageurl: str):
if config.config["captcha_key"] == "":
log.info(f"尚未配置验证码识别密钥,跳过识别")
return None
captcha_key = config.config["captcha_key"]
api_server = "api.geetest.com"
try:
result = solve_captcha(gt, challenge, pageurl, captcha_key, api_server)
except:
log.warning(f"识别失败,程序出现未知错误")
return None
if "ERROR" in result:
if result == "ERROR_WRONG_USER_KEY":
log.warning(f"验证码识别平台key错误,跳过识别")
return None
if result == "ERROR_CAPTCHA_UNSOLVABLE":
log.warning(f"验证码识别超时或无法正常获取,跳过识别")
return None
log.warning(f"发生错误:{result},跳过识别")
return None
log.info(f"识别成功,正在重新签到")
return result
def solve_captcha(gt: str, challenge: str, pageurl: str, captcha_key: str,
api_server: str):
captcha_api = f"http://2captcha.com/in.php?key={captcha_key}&method=geetest>={gt}&challenge={challenge}&api_server={api_server}&pageurl={pageurl}&json=1"
request = http.post(url=captcha_api)
if str(request.status_code) != "200":
return f"ERROR_{str(request.status_code)}_{request.text}"
request_result = request.json()
if str(request_result["status"]) == "1":
solve_captcha_url = f"http://2captcha.com/res.php?key={captcha_key}&action=get&id={request_result['request']}&json=1"
# 识别结果查询
while True:
sleep(5)
captcha = http.post(url=solve_captcha_url)
captcha_result = captcha.json()
if str(captcha_result["status"]) == "1":
result = captcha_result["request"]["geetest_validate"]
break
if captcha_result["request"] != "CAPCHA_NOT_READY":
result = captcha_result["request"]
break
return result
return request_result["request"]
def game_captcha(gt: str, challenge: str):
pageurl = "https://bbs.mihoyo.com/ys"
result = captcha_check(gt, challenge, pageurl)
return result # 失败返回None 成功返回validate
def bbs_captcha(gt: str, challenge: str):
pageurl = "https://bbs.mihoyo.com/ys"
result = captcha_check(gt, challenge, pageurl)
return result