-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
175 lines (156 loc) · 4.09 KB
/
collector.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package version
import (
"fmt"
"net/http"
stdurl "net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
const (
// DefaultURL 提供go版本信息的默认网址
DefaultURL = "https://golang.org/dl/"
)
// URLUnreachableError URL不可达错误
type URLUnreachableError struct {
err error
url string
}
// NewURLUnreachableError 返回URL不可达错误实例
func NewURLUnreachableError(url string, err error) error {
return &URLUnreachableError{
err: err,
url: url,
}
}
func (e *URLUnreachableError) Error() string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("URL %q is unreachable", e.url))
if e.err != nil {
buf.WriteString(" ==> " + e.err.Error())
}
return buf.String()
}
// Collector go版本信息采集器
type Collector struct {
url string
pURL *stdurl.URL
doc *goquery.Document
}
// NewCollector 返回采集器实例
func NewCollector(url string) (*Collector, error) {
pURL, err := stdurl.Parse(url)
if err != nil {
return nil, err
}
c := Collector{
url: url,
pURL: pURL,
}
if err := c.loadDocument(); err != nil {
return nil, err
}
return &c, nil
}
func (c *Collector) loadDocument() (err error) {
resp, err := http.Get(c.url)
if err != nil {
return NewURLUnreachableError(c.url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return NewURLUnreachableError(c.url, nil)
}
c.doc, err = goquery.NewDocumentFromReader(resp.Body)
return err
}
func (c *Collector) findPackages(table *goquery.Selection) (pkgs []*Package) {
alg := strings.TrimSuffix(table.Find("thead").Find("th").Last().Text(), " Checksum")
table.Find("tr").Not(".first").Each(func(j int, tr *goquery.Selection) {
td := tr.Find("td")
href := td.Eq(0).Find("a").AttrOr("href", "")
if strings.HasPrefix(href, "/") { // relative paths
href = fmt.Sprintf("%s://%s%s", c.pURL.Scheme, c.pURL.Host, href)
}
pkgs = append(pkgs, &Package{
FileName: td.Eq(0).Find("a").Text(),
URL: href,
Kind: td.Eq(1).Text(),
OS: td.Eq(2).Text(),
Arch: td.Eq(3).Text(),
Size: td.Eq(4).Text(),
Checksum: td.Eq(5).Text(),
Algorithm: alg,
})
})
return pkgs
}
// HasUnstableVersions 返回是否包含非稳定版本的布尔值
func (c *Collector) HasUnstableVersions() bool {
return c.doc.Find("#unstable").Length() > 0
}
// StableVersions 返回所有稳定版本
func (c *Collector) StableVersions() (items []*Version, err error) {
var divs *goquery.Selection
if c.HasUnstableVersions() {
divs = c.doc.Find("#stable").NextUntil("#unstable")
} else {
divs = c.doc.Find("#stable").NextUntil("#archive")
}
divs.Each(func(i int, div *goquery.Selection) {
vname, ok := div.Attr("id")
if !ok {
return
}
items = append(items, &Version{
Name: strings.TrimPrefix(vname, "go"),
Packages: c.findPackages(div.Find("table").First()),
})
})
return items, nil
}
// UnstableVersions 返回所有非稳定版本
func (c *Collector) UnstableVersions() (items []*Version, err error) {
c.doc.Find("#unstable").NextUntil("#archive").Each(func(i int, div *goquery.Selection) {
vname, ok := div.Attr("id")
if !ok {
return
}
items = append(items, &Version{
Name: strings.TrimPrefix(vname, "go"),
Packages: c.findPackages(div.Find("table").First()),
})
})
return items, nil
}
// ArchivedVersions 返回已归档版本
func (c *Collector) ArchivedVersions() (items []*Version, err error) {
c.doc.Find("#archive").Find("div.toggle").Each(func(i int, div *goquery.Selection) {
vname, ok := div.Attr("id")
if !ok {
return
}
items = append(items, &Version{
Name: strings.TrimPrefix(vname, "go"),
Packages: c.findPackages(div.Find("table").First()),
})
})
return items, nil
}
// AllVersions 返回所有已知版本
func (c *Collector) AllVersions() (items []*Version, err error) {
items, err = c.StableVersions()
if err != nil {
return nil, err
}
archives, err := c.ArchivedVersions()
if err != nil {
return nil, err
}
items = append(items, archives...)
unstables, err := c.UnstableVersions()
if err != nil {
return nil, err
}
items = append(items, unstables...)
return items, nil
}