Jelajahi Sumber

列表页模板和公共方法更新

maxiaoshan 2 tahun lalu
induk
melakukan
45a420d78a
2 mengubah file dengan 30 tambahan dan 2 penghapusan
  1. 2 2
      src/spiderutil/template.go
  2. 28 0
      src/spiderutil/util.go

+ 2 - 2
src/spiderutil/template.go

@@ -71,8 +71,9 @@ function downloadAndParseListPage(pageno)
 				item["title"]="%s"--信息标题
 				item["publishtime"]="%s"--信息时间
 				item=findMap(item,"<table><tr>"..v.."</tr></table>")
-				if item["title"]~=nil and item["title"]~="" then
+				if item["title"]~=nil and item["title"]~="" then --title校验
 					item["title"]=com.trim(item["title"])
+					sendListNum(k,list) --推送下载量
 					local timeType="%s"
 					item["publishtime"]=com.parseDate(item["publishtime"],timeType)--时间格式
 					item["href"]=com.gethref(spiderTargetChannelUrl,item["href"])
@@ -81,7 +82,6 @@ function downloadAndParseListPage(pageno)
 			        table.insert(page,item) 
 				end	
 			end
-			sendListNum(list)--推送下载量
 		end
 		if table.getn(page)>0 then
 			return page

+ 28 - 0
src/spiderutil/util.go

@@ -3,11 +3,14 @@ package spiderutil
 
 import (
 	"crypto/md5"
+	"crypto/sha256"
 	"encoding/base64"
 	"encoding/hex"
 	"encoding/json"
 	"fmt"
 	"github.com/yuin/gopher-lua"
+	"io"
+	"math/big"
 	"math/rand"
 	"net/http"
 	"os"
@@ -177,6 +180,31 @@ func GetRandMath(num int) int {
 	return r.Intn(num)
 }
 
+//对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))
+}
+
+func HexTextByte(text []byte) string {
+	h := sha256.New()
+	h.Write(text)
+	return fmt.Sprintf("%x", h.Sum(nil))
+}
+
 //判断当前时间是否是工作时间,工作时间周一至周五早7点至晚7点
 func IsWorkTime() bool {
 	tt := time.Now()