|
@@ -6,7 +6,6 @@ import (
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
- "log"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"os"
|
|
@@ -31,7 +30,7 @@ func main1() {
|
|
|
util.Debug("port", spiderutil.Config.ServerPort)
|
|
|
lis, err := net.Listen("tcp", spiderutil.Config.ServerPort)
|
|
|
if err != nil {
|
|
|
- log.Println("failed to listen: ", err)
|
|
|
+ util.Debug("failed to listen: ", err)
|
|
|
}
|
|
|
//create a server
|
|
|
s := grpc.NewServer()
|
|
@@ -55,8 +54,67 @@ 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) {
|
|
|
- // log.Println("cookie---", cookie)
|
|
|
- // log.Println("stype---", stype)
|
|
|
+ //先用免费,为识别再用收费
|
|
|
+ coderesp := &req.Response{}
|
|
|
+ code, respheader, respcookie, coderesp = getCodeByFree(path, head, cookie) //自己的服务
|
|
|
+ util.Debug("Get Code By Free Result:", code)
|
|
|
+ if 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) {
|
|
|
+ defer util.Catch()
|
|
|
+ client := req.C().SetTimeout(spiderutil.Config.TimeOut * time.Second)
|
|
|
+ headers := map[string]string{}
|
|
|
+ if head != "" {
|
|
|
+ json.Unmarshal([]byte(head), &headers)
|
|
|
+ }
|
|
|
+ cookies := []*http.Cookie{}
|
|
|
+ if cookie != "" {
|
|
|
+ json.Unmarshal([]byte(cookie), &cookies)
|
|
|
+ }
|
|
|
+ for times := 1; times <= 3; times++ { //重试三次
|
|
|
+ request := client.R()
|
|
|
+ if len(headers) > 0 {
|
|
|
+ request.SetHeaders(headers)
|
|
|
+ }
|
|
|
+ if len(cookies) > 0 {
|
|
|
+ request.SetCookies(cookies...)
|
|
|
+ }
|
|
|
+ //下载验证码图片
|
|
|
+ var err error
|
|
|
+ getCodeResp, err = request.Get(path)
|
|
|
+ if err != nil {
|
|
|
+ util.Debug("Get Code By Path Error: ", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //解析验证码
|
|
|
+ codeResp, err := client.R().
|
|
|
+ SetHeader("accept", "application/json").
|
|
|
+ SetFileReader("file", "1", bytes.NewReader(getCodeResp.Bytes())).
|
|
|
+ Post(spiderutil.Config.ServerCodefreeAddress)
|
|
|
+ if err != nil {
|
|
|
+ util.Debug("analysis code by path err: ", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ yzmResult := map[string]interface{}{}
|
|
|
+ json.Unmarshal(codeResp.Bytes(), &yzmResult)
|
|
|
+ util.Debug(yzmResult)
|
|
|
+ yzm := util.ObjToString(yzmResult["r"].(map[string]interface{})["code"])
|
|
|
+ if yzm != "" && len(yzm) >= 4 {
|
|
|
+ code = yzm //长度小于4的视为识别错误
|
|
|
+ respheader = getCodeResp.Header
|
|
|
+ respcookie = getCodeResp.Cookies()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getCodeByPay(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie) {
|
|
|
defer util.Catch()
|
|
|
client := req.C().SetTimeout(spiderutil.Config.TimeOut * time.Second)
|
|
|
headers := map[string]string{}
|
|
@@ -67,8 +125,6 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
if cookie != "" {
|
|
|
json.Unmarshal([]byte(cookie), &cookies)
|
|
|
}
|
|
|
- //log.Println("headers:", headers)
|
|
|
- //log.Println("cookies:", cookies)
|
|
|
for times := 1; times <= 3; times++ { //重试三次
|
|
|
request := client.R()
|
|
|
if len(headers) > 0 {
|
|
@@ -82,10 +138,9 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
//log.Println("respHeader---", getCodeResp.Header)
|
|
|
//log.Println("respCookie---", getCodeResp.Cookies())
|
|
|
if err != nil {
|
|
|
- log.Println("Get Code By Path Error: ", err)
|
|
|
+ util.Debug("Get Code By Path Error: ", err)
|
|
|
continue
|
|
|
}
|
|
|
- //savefile(getCodeResp)
|
|
|
//解析验证码
|
|
|
data := map[string]string{
|
|
|
"grant_type": "",
|
|
@@ -103,13 +158,13 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
//SetFile("file", "C:/Users/topnet/Desktop/code.jpg").
|
|
|
// Post(spiderutil.Config.ServerCodeAddress)
|
|
|
if err != nil {
|
|
|
- log.Println("analysis code by path err: ", err)
|
|
|
+ util.Debug("analysis code by path err: ", err)
|
|
|
continue
|
|
|
}
|
|
|
codeResult := map[string]interface{}{}
|
|
|
json.Unmarshal(codeResp.Bytes(), &codeResult)
|
|
|
- log.Println("codeResult:", codeResult)
|
|
|
- if yzm, ok := codeResult["r"].(map[string]interface{})["pic_str"].(string); ok && yzm != "" {
|
|
|
+ util.Debug("codeResult:", codeResult)
|
|
|
+ if yzm, ok := codeResult["r"].(map[string]interface{})["pic_str"].(string); ok && yzm != "" && len(yzm) >= 4 {
|
|
|
code = yzm
|
|
|
respheader = getCodeResp.Header
|
|
|
respcookie = getCodeResp.Cookies()
|
|
@@ -118,11 +173,23 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
-func savefile(resp *req.Response) {
|
|
|
- filename := fmt.Sprint(time.Now().Unix()) + ".jpg"
|
|
|
- file, _ := os.Create(filename)
|
|
|
- defer file.Close()
|
|
|
- file.Write(resp.Bytes())
|
|
|
+
|
|
|
+func savefile(url string, resp *req.Response) {
|
|
|
+ base64Url := spiderutil.EncodeB64(url)
|
|
|
+ path := "img/" + base64Url
|
|
|
+ if resp != nil {
|
|
|
+ _, err := os.Stat(path)
|
|
|
+ if err != nil { //没有文件夹
|
|
|
+ err := os.Mkdir(path, 0777) //创建文件夹
|
|
|
+ if err != nil {
|
|
|
+ util.Debug("Create Dir Error:", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ filename := path + "/" + fmt.Sprint(time.Now().Unix()) + ".jpg"
|
|
|
+ file, _ := os.Create(filename)
|
|
|
+ defer file.Close()
|
|
|
+ file.Write(resp.Bytes())
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//func main() {
|