scbank-sync/chaojiying.py

53 lines
1.8 KiB
Python

import requests
from hashlib import md5
class ChaojiyingClient:
"""超级鹰验证码识别 API 客户端"""
def __init__(self, username, password, soft_id):
self.username = username
self.password = password
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def solve_captcha(self, image_bytes, codetype=1902):
"""
上传验证码图片并识别
:param image_bytes: 图片二进制数据
:param codetype: 题目类型 (1902 为 4-6 位英文数字混合)
:return: 识别结果 (dict)
"""
url = 'http://upload.chaojiying.net/Upload/Processing.php'
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('captcha.jpg', image_bytes)}
try:
response = requests.post(url, data=params, files=files, headers=self.headers, timeout=30)
return response.json()
except Exception as e:
return {'err_no': -1, 'err_str': f"网络请求异常: {str(e)}"}
def report_error(self, pic_id):
"""
识别错误时,报错返分
"""
url = 'http://upload.chaojiying.net/Upload/ReportError.php'
params = { 'id': pic_id }
params.update(self.base_params)
try:
response = requests.post(url, data=params, headers=self.headers, timeout=10)
return response.json()
except Exception as e:
return {'err_no': -1, 'err_str': str(e)}