-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.go
148 lines (131 loc) · 3.8 KB
/
changelog.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
package changelog
import (
"fmt"
"html"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/mrmarble/minecraft-update-go/internal/emoji"
)
// Bug holds fixed bug info
type Bug struct {
ID string
Link string
Desc string
}
// Changelog holds parsed minecraft version changelog
type Changelog struct {
URL string
Title string
New []string
Changes []string
Technical []string
Bugs []Bug
}
func (cl *Changelog) String() string {
result := []string{fmt.Sprintf("<a href='%s'>%s</a> <strong>%s</strong>", cl.URL, emoji.Link, html.EscapeString(cl.Title))}
if len(cl.New) > 0 {
result = append(result, "")
result = append(result, fmt.Sprintf("%s <strong>New Features</strong>", emoji.New))
for _, n := range cl.New {
result = append(result, fmt.Sprintf(" - %s", html.EscapeString(n)))
}
}
if len(cl.Changes) > 0 {
result = append(result, "")
result = append(result, fmt.Sprintf("%s <strong>Changes</strong>", emoji.Change))
for _, c := range cl.Changes {
result = append(result, fmt.Sprintf(" - %s", html.EscapeString(c)))
}
}
if len(cl.Technical) > 0 {
result = append(result, "")
result = append(result, fmt.Sprintf("%s <strong>Technical Changes</strong>", emoji.Technical))
for _, t := range cl.Technical {
result = append(result, fmt.Sprintf(" - %s", html.EscapeString(t)))
}
}
if len(cl.Bugs) > 0 {
result = append(result, "")
result = append(result, fmt.Sprintf("%s <strong>Fixed Bugs</strong>", emoji.Bug))
for _, b := range cl.Bugs {
result = append(result, fmt.Sprintf(" - %s: %s", b.ID, html.EscapeString(b.Desc)))
}
}
return strings.Join(result, "\n")
}
func fetch(version string) (*goquery.Document, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", URL(version), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/94.0")
req.Header.Set("Accept", "*/*")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Changelong not found. Error: %s", resp.Status)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
return doc, nil
}
// FromURL creates a Changelog from url
func FromURL(url string) (*Changelog, error) {
doc, err := fetch(url)
if err != nil {
return nil, err
}
cl := Changelog{
URL: URL(url),
Changes: []string{},
}
doc.Find("div.page-section--first div:nth-child(1) > h1:nth-child(1)").Each(func(i int, s *goquery.Selection) {
cl.Title = s.Text()
})
doc.Find("div.page-section--first h1 + ul").Each(func(i int, s *goquery.Selection) {
sectionTitle := s.Prev().Text()
switch {
case strings.HasPrefix(sectionTitle, "New"):
s.Children().Each(func(i int, s *goquery.Selection) {
if i < 5 {
cl.New = append(cl.New, s.Text())
}
})
case strings.HasPrefix(sectionTitle, "Changes"):
s.Children().Each(func(i int, s *goquery.Selection) {
if i < 5 {
cl.Changes = append(cl.Changes, s.Text())
}
})
case strings.HasPrefix(sectionTitle, "Technical"):
s.Children().Each(func(i int, s *goquery.Selection) {
if i < 5 {
cl.Technical = append(cl.Technical, s.Text())
}
})
case strings.HasPrefix(sectionTitle, "Fixed"):
s.Children().Each(func(i int, s *goquery.Selection) {
if i < 5 {
bug := Bug{
ID: s.Find("a").Text(),
Link: s.Find("a").AttrOr("href", ""),
Desc: strings.Replace(s.Contents().Last().Text(), "\u00a0- ", "", 1),
}
cl.Bugs = append(cl.Bugs, bug)
}
})
}
})
return &cl, nil
}
// URL returns a changelog url for a given minecraft version
func URL(versionURL string) string {
return fmt.Sprintf("https://www.minecraft.net/en-us/article/minecraft-%s", versionURL)
}