-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
138 lines (123 loc) · 3.34 KB
/
parser.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
package main
import (
"io"
"regexp"
"strings"
"unicode"
"github.com/PuerkitoBio/goquery"
)
const releaseUrl = "https://go.dev/doc/devel/release"
type Parser struct {
doc *goquery.Document
releases map[string]string
}
// NewParser return a new DOM tree parser
func NewParser(reader io.Reader) *Parser {
doc, _ := goquery.NewDocumentFromReader(reader)
return &Parser{
doc: doc,
releases: make(map[string]string),
}
}
// Archived return all archived versions
func (p *Parser) Archived() map[string]*Version {
result := make(map[string]*Version)
p.doc.Find("#archive").Find("div.toggle").Each(func(i int, selection *goquery.Selection) {
version, ok := selection.Attr("id")
if !ok {
return
}
result[version] = &Version{
Name: version,
Packages: p.findPackages(version, selection),
}
})
return result
}
// Stable return all Stable versions
func (p *Parser) Stable() map[string]*Version {
result := make(map[string]*Version)
p.doc.Find("#stable").NextUntil("#archive,#unstable").Each(func(i int, selection *goquery.Selection) {
version, ok := selection.Attr("id")
if !ok {
return
}
result[version] = &Version{
Name: version,
Packages: p.findPackages(version, selection.Find("table").First()),
}
})
return result
}
// AllVersions return all versions
func (p *Parser) AllVersions() map[string]*Version {
stables := p.Stable()
archives := p.Archived()
for s, version := range archives {
stables[s] = version
}
return stables
}
func (p *Parser) findPackages(tag string, table *goquery.Selection) (pkgs []*Package) {
alg := strings.TrimSuffix(table.Find("thead").Find("th").Last().Text(), " Checksum")
table.Find("tr").Not("first").Each(func(i int, tr *goquery.Selection) {
td := tr.Find("td")
released := p.releases[tag]
pkgs = append(pkgs, &Package{
Name: td.Eq(0).Find("a").Text(),
Tag: tag,
URL: td.Eq(0).Find("a").AttrOr("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,
released: released,
})
})
return
}
func (p *Parser) setReleases(r map[string]string) {
p.releases = r
}
type DateParser struct {
doc *goquery.Document
releases map[string]string
}
// NewDateParser return a new DOM tree parser
func NewDateParser(reader io.Reader) *DateParser {
doc, _ := goquery.NewDocumentFromReader(reader)
return &DateParser{
doc: doc,
releases: make(map[string]string),
}
}
func (p *DateParser) findReleaseDate() map[string]string {
p.doc.Find("article").Find("p:contains(released)").Each(func(i int, selection *goquery.Selection) {
reg, err := regexp.Compile(`go[\s\S]*\)`)
if err != nil {
panic(err)
}
result := reg.FindString(selection.Text())
if len(result) == 0 {
return
}
tmp := strings.Split(result, " ")
var version, date string
if len(tmp) == 3 {
version, date = tmp[0], strings.TrimRight(tmp[2], ")")
} else if len(tmp) == 2 {
version = strings.FieldsFunc(tmp[0], unicode.IsSpace)[0]
date = strings.TrimRight(tmp[1], ")")
}
p.releases[version] = date
})
p.doc.Find("article").Find("h2").Each(func(i int, selection *goquery.Selection) {
tmp := strings.Split(selection.Text(), " ")
if len(tmp) == 3 {
p.releases[tmp[0]] = strings.TrimRight(tmp[2], ")")
}
})
return p.releases
}