-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
161 lines (137 loc) · 3.35 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package exec
import (
"encoding/gob"
"net/http"
"path/filepath"
"time"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/admpub/color"
"github.com/admpub/marmot/miner"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/library/common"
)
func init() {
gob.Register([]*dbschema.NgingCollectorRule{})
_ = color.Red
miner.UserAgentf = http.Dir(filepath.Join(echo.Wd(), `config`))
}
var (
//{(1-2,2-3:2)} 分为两个部分,用":"分隔,":"左边部分定义页码范围,右边定义步进
pagingFlagLeft = "{("
pagingFlagRight = ")}"
emptyRecv = &Recv{empty: true}
)
type (
Fether func(pageURL string, charset string) (body []byte, transcoded bool, err error)
Result struct { //收集测试结果
Title string
URL string
Result interface{}
Type string //map/slice
StartTime time.Time
EndTime time.Time
Elapsed time.Duration
}
Recv struct { //接收结果
empty bool //是否为空结果
index int //数字索引
result interface{} //采集结果
rule *Rule //页面规则
title string //页面标题
url string //网址
parent *Recv
}
)
func (c *Result) IsSlice() bool {
return c.Type == `array`
}
func (c *Result) IsMap() bool {
return c.Type == `map`
}
func (c *Result) ElapsedString(lang string) string {
duration := com.ParseDuration(c.Elapsed, lang)
return duration.String()
}
func (c *Recv) IsEmpty() bool {
return c.empty
}
func (c *Recv) Index() int {
return c.index
}
func (c *Recv) Result(args ...int) interface{} {
if len(args) > 0 {
index := args[0]
if index < 0 {
return echo.H{}
}
if res, ok := c.result.([]interface{}); ok {
if len(res) > index {
return res[index]
}
}
return echo.H{}
}
return c.result
}
func (c *Recv) Rule() *Rule {
return c.rule
}
func (c *Recv) URL() string {
return c.url
}
func (c *Recv) Title() string {
return c.title
}
func (c *Recv) ParentResult() interface{} {
return c.Parent().Result(c.index)
}
func (c *Recv) ParentsResult(lasts ...int) interface{} {
return c.Parents(lasts...).Result(c.index)
}
func (c *Recv) Parent() *Recv {
if c.parent != nil {
return c.parent
}
return emptyRecv
}
func (c *Recv) Parents(lasts ...int) *Recv {
var last int
if len(lasts) > 0 {
last = lasts[0]
}
if last <= 1 {
return c
}
r := c.parent
if r == nil {
return emptyRecv
}
for i := 2; i <= last; i++ {
if r.parent == nil {
return emptyRecv
}
r = r.parent
}
if r == nil {
return emptyRecv
}
return r
}
func (_ *Recv) UniqueID() (string, error) {
return common.UniqueID()
}