Pārlūkot izejas kodu

修改获取验证码方法、新增附件下载方法

maxiaoshan 3 gadi atpakaļ
vecāks
revīzija
5077ae5c1e
2 mainītis faili ar 67 papildinājumiem un 5 dzēšanām
  1. 56 0
      src/spider/download.go
  2. 11 5
      src/spider/script.go

+ 56 - 0
src/spider/download.go

@@ -208,6 +208,62 @@ func DownloadFile(downloaderid, url, method string, reqparam, head map[string]in
 	}
 }
 
+func NewDownloadFile(downloaderid, url, method string, reqparam, head map[string]interface{}, mycookie []*http.Cookie, encoding string, useproxy, ishttps bool, code string, timeout int64, noredirect bool) []byte {
+	defer mu.Catch()
+	timeout = timeout * 10
+	msgid := mu.UUID(8)
+	if len(head) < 1 {
+		l := len(agent.UserAgents["common"])
+		r := rand.New(rand.NewSource(time.Now().UnixNano()))
+		head["User-Agent"] = agent.UserAgents["common"][r.Intn(l)]
+	}
+	var ret []byte
+	var err error
+	if downloaderid == "" {
+		ret, err = MsclientFile.Call("", msgid, mu.SERVICE_DOWNLOAD, mu.SENDTO_TYPE_RAND_RECIVER, map[string]interface{}{
+			"url":        url,
+			"method":     method,
+			"head":       head,
+			"reqparam":   reqparam,
+			"cookie":     mycookie,
+			"encoding":   encoding,
+			"useproxy":   useproxy,
+			"ishttps":    ishttps,
+			"new":        true,
+			"noredirect": noredirect,
+		}, timeout)
+	} else {
+		if isAvailableFile(downloaderid) {
+			ret, err = MsclientFile.Call(downloaderid, msgid, mu.SERVICE_DOWNLOAD, mu.SENDTO_TYPE_P2P, map[string]interface{}{
+				"url":        url,
+				"method":     method,
+				"head":       head,
+				"reqparam":   reqparam,
+				"cookie":     mycookie,
+				"encoding":   encoding,
+				"useproxy":   useproxy,
+				"ishttps":    ishttps,
+				"new":        true,
+				"noredirect": noredirect,
+			}, timeout)
+		} else {
+			return nil
+		}
+	}
+	if err != nil {
+		str := code + "方法DownloadFile,url:" + url + ",err:" + err.Error()
+		log.Println(str, timeout)
+	}
+	tmp := map[string]interface{}{}
+	json.Unmarshal(ret, &tmp)
+	if v, ok := tmp["code"].(string); ok && v == "200" {
+		bs, _ := base64.StdEncoding.DecodeString(tmp["content"].(string))
+		return bs
+	} else {
+		return nil
+	}
+}
+
 func DownloadFile_back(downloaderid, url, method string, reqparam, head map[string]interface{}, mycookie []*http.Cookie, encoding string, useproxy, ishttps bool, code string, timeout int64) []byte {
 	defer mu.Catch()
 	msgid := mu.UUID(8)

+ 11 - 5
src/spider/script.go

@@ -321,7 +321,6 @@ func (s *Script) LoadScript(downloadnode, script string, isfile ...string) {
 		S.Push(lua.LString(ftype))
 		S.Push(lua.LString(fid))
 		return 5
-
 	}))
 	//支持正则
 	s.L.SetGlobal("regexp", s.L.NewFunction(func(S *lua.LState) int {
@@ -549,17 +548,24 @@ func (s *Script) LoadScript(downloadnode, script string, isfile ...string) {
 	//获取验证码
 	s.L.SetGlobal("getCodeByPath", s.L.NewFunction(func(S *lua.LState) int {
 		head := S.ToTable(-1)
-		path := S.ToString(-2)
+		cookie := S.ToString(-2)
+		stype := S.ToString(-3)
+		path := S.ToString(-4)
 		headMap := util.GetTable(head)
-		qu.Debug(headMap)
+		qu.Debug("cookie", cookie)
 		headJsonStr := ""
 		headByte, err := json.Marshal(headMap)
 		if err == nil {
 			headJsonStr = string(headByte)
 		}
-		code := codegrpc.GetCodeByPath(path, headJsonStr)
+		code, respHead, respCookie := codegrpc.GetCodeByPath(path, stype, headJsonStr, cookie)
 		S.Push(lua.LString(code))
-		return 1
+		respHeadMap := map[string]interface{}{}
+		json.Unmarshal([]byte(respHead), &respHeadMap)
+		hTable := util.MapToLuaTable(S, respHeadMap)
+		S.Push(hTable)
+		S.Push(lua.LString(respCookie))
+		return 3
 	}))
 }