|
@@ -56,16 +56,16 @@ func (cs *CodeService) GetCodeByImgPath(ctx context.Context, req *proto.GetCodeb
|
|
|
func getCode(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie) {
|
|
|
//先用免费,为识别再用收费
|
|
|
coderesp := &req.Response{}
|
|
|
- code, respheader, respcookie, coderesp = getCodeByFree(path, head, cookie) //自己的服务
|
|
|
+ code, respheader, respcookie, coderesp = getCodeByFree(path, stype, head, cookie) //自己的服务
|
|
|
util.Debug("Get Code By Free Result:", code)
|
|
|
- if len(code) < 4 { //免费的未能识别使用超级鹰
|
|
|
+ if util.IntAll(stype) > 0 && len(code) < 4 { //免费的未能识别使用超级鹰
|
|
|
savefile(path, coderesp) //保存图片
|
|
|
code, respheader, respcookie = getCodeByPay(path, stype, head, cookie) //超级鹰收费
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func getCodeByFree(path, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie, getCodeResp *req.Response) {
|
|
|
+func getCodeByFree(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie, getCodeResp *req.Response) {
|
|
|
defer util.Catch()
|
|
|
client := req.C().SetTimeout(spiderutil.Config.TimeOut * time.Second)
|
|
|
headers := map[string]string{}
|
|
@@ -86,7 +86,13 @@ func getCodeByFree(path, head, cookie string) (code string, respheader http.Head
|
|
|
}
|
|
|
//下载验证码图片
|
|
|
var err error
|
|
|
- getCodeResp, err = request.Get(path)
|
|
|
+ var resultByte []byte
|
|
|
+ if stype == "-1" { //传base64的图片
|
|
|
+ resultByte = []byte(path)
|
|
|
+ } else { //通过请求图片地址返回的byte
|
|
|
+ getCodeResp, err = request.Get(path)
|
|
|
+ resultByte = getCodeResp.Bytes()
|
|
|
+ }
|
|
|
if err != nil {
|
|
|
util.Debug("Get Code By Path Error: ", err)
|
|
|
continue
|
|
@@ -94,7 +100,7 @@ func getCodeByFree(path, head, cookie string) (code string, respheader http.Head
|
|
|
//解析验证码
|
|
|
codeResp, err := client.R().
|
|
|
SetHeader("accept", "application/json").
|
|
|
- SetFileReader("file", "1", bytes.NewReader(getCodeResp.Bytes())).
|
|
|
+ SetFileReader("file", "1", bytes.NewReader(resultByte)).
|
|
|
Post(spiderutil.Config.ServerCodefreeAddress)
|
|
|
if err != nil {
|
|
|
util.Debug("analysis code by path err: ", err)
|
|
@@ -106,8 +112,10 @@ func getCodeByFree(path, head, cookie string) (code string, respheader http.Head
|
|
|
yzm := util.ObjToString(yzmResult["r"].(map[string]interface{})["code"])
|
|
|
if yzm != "" && len(yzm) >= 4 {
|
|
|
code = yzm //长度小于4的视为识别错误
|
|
|
- respheader = getCodeResp.Header
|
|
|
- respcookie = getCodeResp.Cookies()
|
|
|
+ if getCodeResp != nil {
|
|
|
+ respheader = getCodeResp.Header
|
|
|
+ respcookie = getCodeResp.Cookies()
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
}
|