Bläddra i källkod

sendListNum方法修改;HexText提交公共方法

maxiaoshan 2 år sedan
förälder
incheckning
4380c6df7e
5 ändrade filer med 31 tillägg och 57 borttagningar
  1. 1 1
      src/spider/history.go
  2. 1 1
      src/spider/msgservice.go
  3. 21 20
      src/spider/script.go
  4. 7 34
      src/spider/spider.go
  5. 1 1
      src/spider/store.go

+ 1 - 1
src/spider/history.go

@@ -57,7 +57,7 @@ func (s *Spider) DownloadHistoryDetail() {
 	for _, tmp := range *list {
 		id := tmp["_id"]
 		href := qu.ObjToString(tmp["href"])
-		hashHref := HexText(href)
+		hashHref := sputil.HexText(href)
 		isExist := sputil.RedisClusterExists(hashHref) //全量href redis判重
 		if isExist {
 			set := map[string]interface{}{"$set": map[string]interface{}{"state": 1, "exist": "href", "updatetime": time.Now().Unix()}} //已存在state置为1

+ 1 - 1
src/spider/msgservice.go

@@ -278,7 +278,7 @@ func SaveObj(event int, checkAtrr string, data map[string]interface{}, saveredis
 		}
 		href := fmt.Sprint(data["href"])
 		if len(href) > 5 && saveredis { //有效数据
-			hashHref := HexText(href)
+			hashHref := util.HexText(href)
 			isExists := util.RedisClusterExists(hashHref)
 			data["redisexists"] = isExists
 			if arr := strings.Split(idAndColl, "+"); len(arr) == 2 { //保存服务未成功推送的信息(异常、重复等),返回值不是id

+ 21 - 20
src/spider/script.go

@@ -43,20 +43,21 @@ import (
 //脚本
 type Script struct {
 	//Ishttps            bool
-	SCode, ScriptFile  string
-	Encoding           string
-	Userproxy          bool
-	ScriptStop         bool   //记录爬虫是否下架,用于判断心跳
-	ErrorNum           int32  //错误数
-	Downloader         string //下载器
-	TotalRequestNum    int32  //总请求次数
-	ToDayRequestNum    int32  //今日请求次数
-	YestoDayRequestNum int32  //昨日请求次数
-	Timeout            int64  //超时时间秒
-	L                  *lua.LState
-	NoDownloadNum      int32           //未成功下载数
-	LastThreeTimes     []time.Duration //单条信息流程完成的时间,最后三次
-	FileLastThreeTimes []time.Duration //附件下载单条信息流程完成的时间,最后三次
+	SCode, ScriptFile   string
+	Encoding            string
+	Userproxy           bool
+	ScriptStop          bool   //记录爬虫是否下架,用于判断心跳
+	ErrorNum            int32  //错误数
+	Downloader          string //下载器
+	TotalRequestNum     int32  //总请求次数
+	ToDayRequestNum     int32  //今日请求次数
+	YestoDayRequestNum  int32  //昨日请求次数
+	Timeout             int64  //超时时间秒
+	L                   *lua.LState
+	NoDownloadNum       int32           //未成功下载数
+	LastThreeTimes      []time.Duration //单条信息流程完成的时间,最后三次
+	FileLastThreeTimes  []time.Duration //附件下载单条信息流程完成的时间,最后三次
+	AlreadyGetPageHeart map[int]bool    //记录某页已完成心跳记录
 }
 
 const (
@@ -501,13 +502,13 @@ func (s *Script) LoadScript(site, channel, user *string, code, script_file strin
 	//推送列表页下载数据量
 	s.L.SetGlobal("sendListNum", s.L.NewFunction(func(S *lua.LState) int {
 		if !s.ScriptStop { //非下架状态,记录心跳
-			index := S.ToInt(-2)
-			if index == 1 {
-				table := S.ToTable(-1)
-				if table.Len() > 0 {
-					UpdateHeart(*site, *channel, code, *user, "findlist") //记录列表页实际采集数据量心跳
-				}
+			//index := S.ToInt(-2)
+			//if index == 1 {
+			table := S.ToTable(-1)
+			if table.Len() > 0 {
+				UpdateHeart(*site, *channel, code, *user, "findlist") //记录列表页实际采集数据量心跳
 			}
+			//}
 		}
 		return 1
 	}))

+ 7 - 34
src/spider/spider.go

@@ -5,22 +5,19 @@ package spider
 
 import (
 	"crypto/sha1"
-	"crypto/sha256"
 	"fmt"
 	"io"
 	"log"
-	"math/big"
-	"math/rand"
 	mu "mfw/util"
 	mgo "mongodb"
 	qu "qfw/util"
 	"strconv"
+	"strings"
 	"sync"
 
 	es "qfw/util/elastic"
 	"regexp"
 	util "spiderutil"
-	"strings"
 	"sync/atomic"
 	"time"
 
@@ -267,6 +264,7 @@ func (s *Spider) GetLastPublishTime() (errs interface{}) {
 //下载列表
 func (s *Spider) DownListPageItem() (errs interface{}) {
 	defer mu.Catch()
+	s.AlreadyGetPageHeart = map[int]bool{}                                     //重置记录
 	start, max := s.GetIntVar("spiderStartPage"), s.GetIntVar("spiderMaxPage") //起始页、最大页
 	tmpMax := max                                                              //临时记录最大页
 	repeatAllNum := 0                                                          //本轮采集tmpMax页总的重复个数
@@ -345,7 +343,7 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 							//atomic.AddInt32(&s.TotalDowncount, 1)
 							href := fmt.Sprint(tmp["href"])
 							if len(href) > 5 { //有效数据
-								hashHref := HexText(href)
+								hashHref := util.HexText(href)
 								util.RedisClusterSet(hashHref, "", -1) //全量redis
 								list = append(list, tmp)
 							}
@@ -489,7 +487,7 @@ func (s *Spider) HistoricalMendDownloadDetailItem(p interface{}) {
 	if len(href) <= 5 { //无效数据
 		return
 	}
-	hashHref := HexText(href)
+	hashHref := util.HexText(href)
 	isExist := util.RedisClusterExists(hashHref) //全量redis判重
 	//logger.Debug("full href:", href, " isExist:", isExist)
 	if !s.IsMustDownload { //非强制下载
@@ -580,7 +578,7 @@ func (s *Spider) DownloadDetailItem(p interface{}, num *int) {
 		*num++ //视为已采集
 		return
 	}
-	hashHref := HexText(href)
+	hashHref := util.HexText(href)
 	id := ""                                                                                           //记录spider_listdata中保存的数据id,便于下载成功后更新状态
 	if util.Config.Modal == 1 || (util.Config.IsHistoryEvent && s.GetVar("spiderType") == "history") { //除7410、7500、7510、7700节点外所有节点只采集列表页信息
 		isExist := util.RedisClusterExists(hashHref) //全量信息中已采集
@@ -704,7 +702,7 @@ func (s *Spider) DownloadDetailByNames(p interface{}) {
 func (s *Spider) DownloadDetailPage(param map[string]interface{}, data map[string]interface{}) (map[string]interface{}, interface{}) {
 	defer mu.Catch()
 	s.LastHeartbeat = time.Now().Unix()
-	util.TimeSleepFunc((time.Duration(s.SleepBase+GetRandMath(s.SleepRand)))*time.Millisecond, TimeSleepChan)
+	util.TimeSleepFunc((time.Duration(s.SleepBase+util.GetRandMath(s.SleepRand)))*time.Millisecond, TimeSleepChan)
 	tab := s.L.NewTable()
 	for k, v := range param {
 		if val, ok := v.(string); ok {
@@ -892,7 +890,7 @@ func (s *Spider) DownloadDetail(reload bool, isHistory bool) {
 					_id := tmp["_id"]
 					query := map[string]interface{}{"_id": _id}
 					href := qu.ObjToString(tmp["href"])
-					hashHref := HexText(href)
+					hashHref := util.HexText(href)
 					update := []map[string]interface{}{}
 					//由于目前列表页redis判重是href+code可能导致同一条href有多条不同code采集的数据存在
 					//为了避免重复下载,进行全量redis判重
@@ -1093,12 +1091,6 @@ func AllThreadLog() {
 	time.AfterFunc(1*time.Minute, AllThreadLog)
 }
 
-//获取随机数
-func GetRandMath(num int) int {
-	r := rand.New(rand.NewSource(time.Now().UnixNano()))
-	return r.Intn(num)
-}
-
 //获取hascode
 func GetHas1(data string) string {
 	t := sha1.New()
@@ -1109,22 +1101,3 @@ func GetHas1(data string) string {
 	}
 	return hf + fmt.Sprintf("%x", t.Sum(nil))
 }
-
-//对href哈希取模
-func HexToBigIntMod(href string) int {
-	//取哈希值
-	t := sha256.New()
-	io.WriteString(t, href)
-	hex := fmt.Sprintf("%x", t.Sum(nil))
-	//取模
-	n := new(big.Int)
-	n, _ = n.SetString(hex[2:], 16)
-	return int(n.Mod(n, big.NewInt(16)).Int64())
-}
-
-//求hash
-func HexText(href string) string {
-	h := sha256.New()
-	h.Write([]byte(href))
-	return fmt.Sprintf("%x", h.Sum(nil))
-}

+ 1 - 1
src/spider/store.go

@@ -76,7 +76,7 @@ func Store(mode, event int, c, coverAttr string, data map[string]interface{}, fl
 		}
 		href := fmt.Sprint(data["href"])
 		if len(href) > 5 && flag { //有效数据
-			hashHref := HexText(href)
+			hashHref := lu.HexText(href)
 			lu.RedisClusterSet(hashHref, "", -1)
 		}
 	} else if mode == 2 {