-
Notifications
You must be signed in to change notification settings - Fork 57
/
page.go
98 lines (88 loc) · 1.52 KB
/
page.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
package creeper
import (
"io/ioutil"
"net/http"
"strconv"
)
type Page struct {
Raw string
Node *Node
Town *Town
Ref string
NextMode bool
NextUrl string
NextPendingUrl string
NextReady bool
NextNoMore bool
Index int
}
func (p *Page) Inc() {
p.Index++
}
func (p *Page) Url() (string, error) {
if p.NextMode {
return p.NextUrl, nil
}
if p.Town != nil {
p.Town.Attach()
if p.Index > -1 {
p.Town.Set("@page", strconv.Itoa(p.Index))
} else {
i, e := p.Town.Get("@page")
if e {
i64, _ := strconv.ParseInt(i, 10, 32)
p.Index = int(i64)
}
}
for k, v := range p.Town.Params {
if len(v) > 0 && v[0] == '_' {
n := p.Node.SearchRef(v)
v, err := n.Value()
if err != nil {
return "", err
}
p.Town.Set(k, v)
}
}
return p.Town.Value(), nil
} else {
return p.Node.SearchRef(p.Ref).Value()
}
}
func (p *Page) Body() (string, error) {
u, err := p.Url()
if err != nil {
return "", err
}
if v, e := p.Node.Creeper.CacheGet(u); e {
return v, nil
}
res, err := http.Get(u)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
sb := string(body)
p.Node.Creeper.CacheSet(u, sb)
return sb, nil
}
func (p *Page) IsDynamic() bool {
return p.Town != nil
}
func ParsePage(n *Node, s string) *Page {
page := new(Page)
page.Node = n
page.Raw = s
if s[0] == '_' {
page.Ref = s
} else {
page.Town = ParseTownLine(s)
page.Town.Node = n
}
page.Index = -1
return page
}