|
@@ -57,16 +57,19 @@ 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) {
|
|
func getCode(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie) {
|
|
//先用免费,为识别再用收费
|
|
//先用免费,为识别再用收费
|
|
coderesp := &req.Response{}
|
|
coderesp := &req.Response{}
|
|
- code, respheader, respcookie, coderesp = getCodeByFree(path, stype, head, cookie) //自己的服务
|
|
|
|
- util.Debug("Get Code By Free Result:", code)
|
|
|
|
- if util.IntAll(stype) > 0 && len(code) < 4 { //免费的未能识别使用超级鹰
|
|
|
|
|
|
+ ok := false
|
|
|
|
+ code, respheader, respcookie, coderesp, ok = getCodeByFree(path, stype, head, cookie) //自己的服务
|
|
|
|
+ util.Debug("Get Code By Free Result:", ok, code)
|
|
|
|
+ //if util.IntAll(stype) > 0 && len(code) < 4 { //免费的未能识别使用超级鹰
|
|
|
|
+ if util.IntAll(stype) > 0 && !ok {
|
|
savefile(path, coderesp) //保存图片
|
|
savefile(path, coderesp) //保存图片
|
|
code, respheader, respcookie = getCodeByPay(path, stype, head, cookie) //超级鹰收费
|
|
code, respheader, respcookie = getCodeByPay(path, stype, head, cookie) //超级鹰收费
|
|
}
|
|
}
|
|
|
|
+ //}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-func getCodeByFree(path, stype, 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, ok bool) {
|
|
defer util.Catch()
|
|
defer util.Catch()
|
|
client := req.C().
|
|
client := req.C().
|
|
SetTimeout(spiderutil.Config.TimeOut * time.Second).
|
|
SetTimeout(spiderutil.Config.TimeOut * time.Second).
|
|
@@ -101,10 +104,14 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
|
|
//下载验证码图片
|
|
//下载验证码图片
|
|
var err error
|
|
var err error
|
|
var resultByte []byte
|
|
var resultByte []byte
|
|
|
|
+ address := spiderutil.Config.ServerCodefreeAddressOcr
|
|
if stype == "-1" { //传base64的图片
|
|
if stype == "-1" { //传base64的图片
|
|
resultByte = []byte(path)
|
|
resultByte = []byte(path)
|
|
- } else { //通过请求图片地址返回的byte
|
|
|
|
- getCodeResp, err = request.Get(path)
|
|
|
|
|
|
+ } else {
|
|
|
|
+ if stype == "6001" { //计算类验证码解析接口地址
|
|
|
|
+ address = spiderutil.Config.ServerCodefreeAddressArithmetic
|
|
|
|
+ }
|
|
|
|
+ getCodeResp, err = request.Get(path) //通过请求图片地址返回的byte
|
|
resultByte = getCodeResp.Bytes()
|
|
resultByte = getCodeResp.Bytes()
|
|
}
|
|
}
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -115,7 +122,7 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
|
|
codeResp, err := client.R().
|
|
codeResp, err := client.R().
|
|
SetHeader("accept", "application/json").
|
|
SetHeader("accept", "application/json").
|
|
SetFileReader("file", "1", bytes.NewReader(resultByte)).
|
|
SetFileReader("file", "1", bytes.NewReader(resultByte)).
|
|
- Post(spiderutil.Config.ServerCodefreeAddress)
|
|
|
|
|
|
+ Post(address)
|
|
if err != nil {
|
|
if err != nil {
|
|
util.Debug("analysis code by path err: ", err)
|
|
util.Debug("analysis code by path err: ", err)
|
|
continue
|
|
continue
|
|
@@ -123,14 +130,18 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
|
|
yzmResult := map[string]interface{}{}
|
|
yzmResult := map[string]interface{}{}
|
|
json.Unmarshal(codeResp.Bytes(), &yzmResult)
|
|
json.Unmarshal(codeResp.Bytes(), &yzmResult)
|
|
util.Debug(yzmResult)
|
|
util.Debug(yzmResult)
|
|
- yzm := util.ObjToString(yzmResult["r"].(map[string]interface{})["code"])
|
|
|
|
- if yzm != "" && len(yzm) >= 4 {
|
|
|
|
- code = yzm //长度小于4的视为识别错误
|
|
|
|
- if getCodeResp != nil {
|
|
|
|
- respheader = getCodeResp.Header
|
|
|
|
- respcookie = getCodeResp.Cookies()
|
|
|
|
|
|
+ result := yzmResult["r"].(map[string]interface{})
|
|
|
|
+ yzm := fmt.Sprint(result["code"])
|
|
|
|
+ if yzm != "" {
|
|
|
|
+ if stype == "6001" || len(yzm) >= 4 {
|
|
|
|
+ code = yzm //长度小于4的视为识别错误
|
|
|
|
+ if getCodeResp != nil {
|
|
|
|
+ respheader = getCodeResp.Header
|
|
|
|
+ respcookie = getCodeResp.Cookies()
|
|
|
|
+ }
|
|
|
|
+ ok = true
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- return
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
return
|
|
@@ -152,8 +163,8 @@ func getCodeByPay(path, stype, head, cookie string) (code string, respheader htt
|
|
if cookie != "" {
|
|
if cookie != "" {
|
|
json.Unmarshal([]byte(cookie), &cookies)
|
|
json.Unmarshal([]byte(cookie), &cookies)
|
|
}
|
|
}
|
|
- for times := 1; times <= 6; times++ { //重试三次
|
|
|
|
- if times > 4 { //重试第4次开始,使用代理ip
|
|
|
|
|
|
+ for times := 1; times <= 2; times++ { //重试三次
|
|
|
|
+ if times > 1 { //重试第2次开始,使用代理ip
|
|
proxyIp := spiderutil.GetProxyAddr(spiderutil.Config.ProxyAddr, spiderutil.Config.ProxyAuthor) //获取代理地址
|
|
proxyIp := spiderutil.GetProxyAddr(spiderutil.Config.ProxyAddr, spiderutil.Config.ProxyAuthor) //获取代理地址
|
|
util.Debug("proxy:", proxyIp)
|
|
util.Debug("proxy:", proxyIp)
|
|
client.SetProxyURL(proxyIp) //设置代理IP
|
|
client.SetProxyURL(proxyIp) //设置代理IP
|