forked from wspl/creeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
76 lines (67 loc) · 1.18 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
package creeper
import (
"io/ioutil"
"net/http"
"strconv"
)
type Page struct {
Raw string
Node *Node
Town *Town
Ref string
Index int
}
func (p *Page) Inc() {
p.Index++
}
func (p *Page) Url() string {
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)
p.Town.Set(k, n.Value())
}
}
return p.Town.Value()
} else {
return p.Node.SearchRef(p.Ref).Value()
}
}
func (p *Page) Body() string {
if v, e := p.Node.Creeper.Cache_Get(p.Url()); e {
return v
}
u := p.Url()
res, _ := http.Get(u)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
sb := string(body)
p.Node.Creeper.Cache_Set(p.Url(), sb)
return sb
}
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
}