Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f24409

Browse files
committed
爬虫访问,不增加浏览次数
1 parent 6c1b220 commit 5f24409

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

config/env.sample.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ log_level = DEBUG
55
cookie_secret = 9HFEp6b2DMn^fRduZc
66
data_path = data/max_online_num
77

8+
; 搜索引擎,避免阅读次数非人为增加
9+
spider = spider,bot,nutch,yahoo,gougou,scooter,lilina
10+
811
[listen]
912
host = 127.0.0.1
1013
port = 8088

src/logic/view.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ package logic
99
import (
1010
"net/http"
1111
"strconv"
12+
"strings"
1213
"sync"
1314

1415
. "db"
1516
"model"
1617

18+
"github.com/polaris1119/config"
1719
"github.com/polaris1119/goutils"
1820
"github.com/polaris1119/logger"
1921
)
@@ -80,6 +82,14 @@ func newViews() *views {
8082

8183
// TODO: 用户登录了,应该用用户标识,而不是IP
8284
func (this *views) Incr(req *http.Request, objtype, objid int) {
85+
ua := req.UserAgent()
86+
spiders := config.ConfigFile.MustValueArray("global", "spider", ",")
87+
for _, spider := range spiders {
88+
if strings.Contains(ua, spider) {
89+
return
90+
}
91+
}
92+
8393
user := goutils.Ip2long(goutils.RemoteIp(req))
8494

8595
key := strconv.Itoa(objtype) + strconv.Itoa(objid)

0 commit comments

Comments
 (0)