-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
156 lines (132 loc) · 2.84 KB
/
html.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
package main
import (
"fmt"
"io"
"strconv"
"strings"
"golang.org/x/net/html"
)
type Build struct {
ID int
Name string
Failed bool
}
func parseHTML(r io.Reader) ([]Build, error) {
doc, err := html.Parse(r)
if err != nil {
return nil, err
}
return findBuilds(doc)
}
func findBuilds(doc *html.Node) ([]Build, error) {
nodes := findNodes(doc, isBuildsTable)
if len(nodes) != 1 {
return nil, fmt.Errorf("can't find builds table")
}
trs := findNodes(nodes[0], isBuildTr)
var builds []Build
for _, tr := range trs {
b, err := nodeToBuild(tr)
if err != nil {
return nil, err
}
builds = append(builds, b)
}
return builds, nil
}
func isBuildsTable(node *html.Node) bool {
if !isElem(node, "table") {
return false
}
for _, attr := range node.Attr {
if attr.Key == "id" && attr.Val == "bookshelf" {
return true
}
}
return false
}
func isBuildTr(node *html.Node) bool {
if !isElem(node, "tr") {
return false
}
for _, attr := range node.Attr {
if attr.Key == "class" && strings.Contains(attr.Val, "build_status") {
return true
}
}
return false
}
func isElem(node *html.Node, typ string) bool {
if node.Type != html.ElementNode {
return false
}
return node.Data == typ
}
func findNodes(node *html.Node, pred func(*html.Node) bool) []*html.Node {
var out []*html.Node
stack := []*html.Node{node}
for len(stack) != 0 {
n := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if pred(n) {
out = append(out, n)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
stack = append(stack, c)
}
}
return out
}
func nodeToBuild(node *html.Node) (Build, error) {
var b Build
id, err := buildID(node)
if err != nil {
return Build{}, err
}
b.ID = id
found := false
for _, attr := range node.Attr {
if attr.Key != "class" {
continue
}
b.Failed = strings.Contains(attr.Val, "failed")
found = true
break
}
if !found {
return Build{}, fmt.Errorf("can't find status in %#v", node)
}
nodes := findNodes(node, isH4)
if len(nodes) != 1 {
return Build{}, fmt.Errorf("can't find build name in %#v", node)
}
text := nodes[0].FirstChild
if text == nil {
return Build{}, fmt.Errorf("can't find name in %#v", node)
}
b.Name = strings.TrimSpace(text.Data)
if b.Name == "" {
return Build{}, fmt.Errorf("empty name in %#v", node)
}
return b, nil
}
func isH4(node *html.Node) bool {
return node.Type == html.ElementNode && node.Data == "h4"
}
func buildID(node *html.Node) (int, error) {
for _, attr := range node.Attr {
if attr.Key != "id" {
continue
}
i := strings.LastIndex(attr.Val, "_")
if i == -1 {
return 0, fmt.Errorf("can't find build ID in %q", attr.Val)
}
id, err := strconv.Atoi(attr.Val[i+1:])
if err != nil {
return 0, fmt.Errorf("bad build id in %q: %s", attr.Val, err)
}
return id, nil
}
return 0, fmt.Errorf("can't find build id in %#v", node)
}