Browse Source

新增下载方法

maxiaoshan 2 năm trước cách đây
mục cha
commit
938b4fae0a
3 tập tin đã thay đổi với 101 bổ sung3 xóa
  1. 2 2
      src/config.json
  2. 69 0
      src/spider/download.go
  3. 30 1
      src/spider/script.go

+ 2 - 2
src/config.json

@@ -1,10 +1,10 @@
 {
     "webport": "8002",
-    "dbaddr": "192.168.3.207:29099",
+    "dbaddr": "192.168.3.71:29099",
     "dbname": "editor",
     "dbname2": "spider",
     "bideditor": {
-        "addr": "192.168.3.207:29099",
+        "addr": "192.168.3.71:29099",
         "db": "editor",
         "size": 5,
         "username": "",

+ 69 - 0
src/spider/download.go

@@ -156,6 +156,75 @@ func DownloadAdv(downloadnode, downloaderid, url, method string, reqparam, head
 		return "", nil, nil
 	}
 }
+
+func DownloadAdvNew(downloadnode, downloaderid, url, method string, reqparam, head map[string]interface{}, mycookie []*http.Cookie, encoding string, useproxy, ishttps bool, code string, timeout int64) (string, []*http.Cookie, map[string]interface{}) {
+	defer mu.Catch()
+	ResultMsclient := MsclientTest
+	if downloadnode == "test" { //805
+		ResultMsclient = MsclientTest
+	} else if downloadnode == "comm" { //801
+		ResultMsclient = Msclient
+	} else if downloadnode == "bid" { //803
+		ResultMsclient = MsclientBid
+	}
+	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)]
+	}
+	isImg := regImg.MatchString(url)
+	var ret []byte
+	var err error
+	if downloaderid == "" {
+		ret, err = ResultMsclient.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,
+		}, timeout)
+	} else {
+		if isAvailable(downloaderid) {
+			ret, err = ResultMsclient.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,
+			}, timeout)
+		} else {
+			return "", nil, nil
+		}
+	}
+	if err != nil {
+		str := "方法DownloadAdv,url:" + url + ",err:" + err.Error()
+		log.Println(str)
+	}
+	tmp := map[string]interface{}{}
+	json.Unmarshal(ret, &tmp)
+	cooks := util.ParseHttpCookie(tmp["cookie"])
+	headers, _ := tmp["header"].(map[string]interface{})
+	if v, ok := tmp["code"].(string); ok && v == "200" {
+		if isImg {
+			bs, _ := tmp["content"].(string)
+			return string(bs), cooks, headers
+		} else {
+			bs, _ := base64.StdEncoding.DecodeString(tmp["content"].(string))
+			return string(bs), cooks, headers
+		}
+	} else {
+		return "", nil, nil
+	}
+}
+
 func DownloadFile(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()
 	timeout = timeout * 10

+ 30 - 1
src/spider/script.go

@@ -127,7 +127,36 @@ func (s *Script) LoadScript(site *string, downloadnode, script string, isfile ..
 		s.Test_luareqcount++
 		return 3
 	}))
-
+	s.L.SetGlobal("downloadAdvNew", s.L.NewFunction(func(S *lua.LState) int {
+		cookie := S.ToString(-1)
+		head := S.ToTable(-2)
+		param := S.ToTable(-3)
+		method := S.ToString(-4)
+		url := S.ToString(-5)
+		ishttps := S.ToBool(-6)
+		charset := S.ToString(-7)
+		if charset == "" {
+			charset = s.Encoding
+		}
+		var mycookie []*http.Cookie
+		json.Unmarshal([]byte(cookie), &mycookie)
+		var ret string
+		var retcookie []*http.Cookie
+		var headers = map[string]interface{}{}
+		if param == nil {
+			ptext := map[string]interface{}{"text": S.ToString(-3)}
+			ret, retcookie, headers = DownloadAdvNew(downloadnode, s.Downloader, url, method, ptext, util.GetTable(head), mycookie, charset, false, ishttps, "", s.Timeout)
+		} else {
+			ret, retcookie, headers = DownloadAdvNew(downloadnode, s.Downloader, url, method, util.GetTable(param), util.GetTable(head), mycookie, charset, false, ishttps, "", s.Timeout)
+		}
+		S.Push(lua.LString(ret))
+		scookie, _ := json.Marshal(retcookie)
+		S.Push(lua.LString(scookie))
+		hTable := util.MapToLuaTable(S, headers)
+		S.Push(hTable)
+		s.Test_luareqcount++
+		return 3
+	}))
 	s.L.SetGlobal("findOneText", s.L.NewFunction(func(S *lua.LState) int {
 		nodetype := S.ToString(-3)
 		gpath := S.ToString(-2)