-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmstable.go
More file actions
206 lines (187 loc) · 5.34 KB
/
bmstable.go
File metadata and controls
206 lines (187 loc) · 5.34 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package bmstable
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/Catizard/bmstable/internal"
)
// Stores the meta data of a difficult table
type DifficultTable struct {
HeaderURL string `json:"header_url"`
DataURL string `json:"data_url"`
Name string `json:"name"`
OriginalURL string `json:"original_url"`
Symbol string `json:"symbol"`
LevelOrder []string
Contents []DifficultTableData
Courses []CourseInfo
}
// Reprents one song arranged in a difficult table
type DifficultTableData struct {
Artist string `json:"artist"`
Comment string `json:"comment"`
Level string `json:"level"`
Lr2BmsID string `json:"lr2_bmdid"`
Md5 string `json:"md5"`
NameDiff string `json:"name_diff"`
Title string `json:"title"`
URL string `json:"url"`
URLDiff string `json:"url_diff"`
Sha256 string `json:"sha256"`
}
// Stores the meta data of a course
type CourseInfo struct {
Name string `json:"name"`
Md5 []string `json:"md5"`
Sha256 []string `json:"sha256"`
Constraint []string `json:"constraint"`
}
func ParseFromURL(url string) (DifficultTable, error) {
header, err := parseHeaderFromURL(url)
if err != nil {
return DifficultTable{}, err
}
data, err := parseDataFromURL(header.DataURL)
if err != nil {
return DifficultTable{}, err
}
header.Contents = data
return *header, nil
}
func parseHeaderFromURL(url string) (*DifficultTable, error) {
jsonURL := ""
prefixURL := buildPrefixURL(url)
if !strings.HasSuffix(url, ".json") {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("net: cannot get contents from %s due to %s", url, err)
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("io: failed to read contents: %s", err)
}
line := strings.TrimSpace(scanner.Text())
// TODO: Any other cases?
// Its pattern should be <meta name="bmstable" content="xxx.json" />
if strings.HasPrefix(line, "<meta name=\"bmstable\"") {
startp := strings.Index(line, "content") + len("content=\"") - 1
if startp == -1 {
return nil, fmt.Errorf("unexpected format: cannot find 'content' field in %s", url)
}
endp := -1
// Finds the end position
first := false
for i := startp; i < len(line); i++ {
if line[i] == '"' {
if !first {
first = true
} else {
endp = i
break
}
}
}
if endp == -1 {
return nil, fmt.Errorf("unexpected format: cannot find 'content' field in %s", url)
}
content := line[startp+1 : endp]
if !strings.HasPrefix(content, "http") {
// Construct the json url path
jsonURL = prefixURL + "/" + line[startp+1:endp]
} else {
jsonURL = content
}
break
}
}
} else {
// Okay dokey
jsonURL = url
}
if jsonURL == "" {
return nil, fmt.Errorf("parse: cannot build possible json url from %s", url)
}
var rawHeader internal.ImportHeaderVo
if err := fetchJSON(jsonURL, &rawHeader); err != nil {
return nil, err
}
if !strings.HasPrefix(rawHeader.DataURL, "http") {
rawHeader.DataURL = prefixURL + "/" + rawHeader.DataURL
}
if err := rawHeader.ParseRawCourses(); err != nil {
return nil, err
}
rawHeader.HeaderURL = url
return newDifficultTable(&rawHeader), nil
}
func parseDataFromURL(url string) ([]DifficultTableData, error) {
var data []DifficultTableData
if err := fetchJSON(url, &data); err != nil {
return nil, fmt.Errorf("parse: cannot fetch table data from %s due to %s", url, err)
}
return data, nil
}
func newDifficultTable(rawHeader *internal.ImportHeaderVo) *DifficultTable {
courses := make([]CourseInfo, 0)
for i := range rawHeader.Courses {
courses = append(courses, *newCourseInfo(&rawHeader.Courses[i]))
}
castedLevelOrders := make([]string, 0)
for _, level := range rawHeader.LevelOrders {
castedLevel := ""
if l, ok := level.(string); ok {
castedLevel = l
} else if l, ok := level.(int); ok {
castedLevel = strconv.Itoa(l)
} else {
castedLevel = fmt.Sprintf("%v", level)
}
castedLevelOrders = append(castedLevelOrders, castedLevel)
}
return &DifficultTable{
HeaderURL: rawHeader.HeaderURL,
DataURL: rawHeader.DataURL,
Name: rawHeader.Name,
OriginalURL: rawHeader.OriginalURL,
Symbol: rawHeader.Symbol,
LevelOrder: castedLevelOrders,
Contents: make([]DifficultTableData, 0),
Courses: courses,
}
}
func newCourseInfo(rawCourse *internal.ImportCourseVo) *CourseInfo {
return &CourseInfo{
Name: rawCourse.Name,
Md5: rawCourse.Md5,
Sha256: rawCourse.Sha256,
Constraint: rawCourse.Constraint,
}
}
func fetchJSON(url string, v any) error {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("net: cannot get contents from %s due to %s", url, err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("io: cannot read contents from %s", url)
}
// Hack \ufeff, \r, \n out, especially for PMS tables
replacer := strings.NewReplacer("\r", "", "\n", "", "\ufeff", "")
body = []byte(replacer.Replace(string(body)))
if err := json.Unmarshal(body, v); err != nil {
return fmt.Errorf("json: %s", err)
}
return nil
}
func buildPrefixURL(url string) string {
splitURL := strings.Split(url, "/")
splitURL[len(splitURL)-1] = ""
return strings.Join(splitURL, "/")
}