/
main.go
297 lines (252 loc) · 7.12 KB
/
main.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"compress/gzip"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"os"
)
const (
RANKED_URL = "https://cdn.wes.cloud/beatstar/bssb/v2-all.json.gz"
)
type RankedEntry struct {
Bpm int `json:"bpm"`
Diffs []RankedEntryDiff `json:"diffs"`
DownVotes int `json:"downVotes"`
DownloadCount int `json:"downloadCount"`
Heat float64 `json:"heat"`
Key string `json:"key"`
Mapper string `json:"mapper"`
Rating float64 `json:"rating"`
Song string `json:"song"`
UpVotes int `json:"upVotes"`
}
type RankedEntryDiff struct {
Pp float64 `json:"pp,string"`
Star float64 `json:"star,string"`
Scores int `json:"scores,string"`
Diff string `json:"diff"`
Type int `json:"type"`
Len int `json:"len"`
Njs int `json:"njs"`
}
type Playlist struct {
Title string `json:"playlistTitle"`
Author string `json:"playlistAuthor"`
Description string `json:"playlistDescription"`
Songs []*PlaylistSong `json:"songs"`
Image string `json:"image,omitempty"`
}
type PlaylistSong struct {
SongName string `json:"songName,omitempty"`
LevelAuthorName string `json:"levelAuthorName,omitempty"`
Hash string `json:"hash"`
Difficulties []*PlaylistSongDifficulty `json:"difficulties"`
}
type PlaylistSongDifficulty struct {
Characteristic string `json:"characteristic"`
Name string `json:"name"`
}
func main() {
var (
imageDir string
outputDir string
)
flag.StringVar(&imageDir, "image-dir", "images", "Image Directory")
flag.StringVar(&outputDir, "output-dir", "dist", "Output Directory")
flag.Parse()
if _, err := os.Stat(outputDir); err != nil {
if err := os.MkdirAll(outputDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "failed to create output directory: %v", err)
os.Exit(1)
return
}
}
characteristicNames := []string{"Unknown", "Standard", "OneSaber", "NoArrows", "Lightshow", "Degree90", "Degree360", "Lawless"}
entries, err := downloadRankedList()
if err != nil {
panic(err)
}
pps := []float64{200, 300, 400, 500}
songByPP := make(map[float64]map[string]*PlaylistSong)
for _, pp := range pps {
songByPP[pp] = make(map[string]*PlaylistSong, 0)
}
songByStar := make(map[int]map[string]*PlaylistSong)
for hash, entry := range entries {
for _, diff := range entry.Diffs {
if diff.Star == 0 {
continue
}
star := int(math.Trunc(diff.Star))
if _, ok := songByStar[star]; !ok {
songByStar[star] = make(map[string]*PlaylistSong, 0)
}
song, ok := songByStar[star][hash]
if !ok {
song = &PlaylistSong{
Hash: hash,
Difficulties: make([]*PlaylistSongDifficulty, 0),
}
songByStar[star][hash] = song
}
if diff.Type > len(characteristicNames) -1 {
continue
}
characteristicName := characteristicNames[diff.Type]
diffName := diff.Diff
if diffName == "Expert+" {
diffName = "ExpertPlus"
}
song.Difficulties = append(song.Difficulties, &PlaylistSongDifficulty{
Characteristic: characteristicName,
Name: diffName,
})
}
for _, pp := range pps {
for _, diff := range entry.Diffs {
if diff.Pp >= pp {
song, ok := songByPP[pp][hash]
if !ok {
song = &PlaylistSong{
Hash: hash,
Difficulties: make([]*PlaylistSongDifficulty, 0),
}
songByPP[pp][hash] = song
}
if diff.Type > len(characteristicNames) -1 {
continue
}
characteristicName := characteristicNames[diff.Type]
diffName := diff.Diff
if diffName == "Expert+" {
diffName = "ExpertPlus"
}
song.Difficulties = append(song.Difficulties, &PlaylistSongDifficulty{
Characteristic: characteristicName,
Name: diffName,
})
break
}
}
}
}
// by star
for star, songMap := range songByStar {
image, err := getImageByStar(imageDir, star)
if err != nil {
panic(err)
}
songs := make([]*PlaylistSong, 0)
for _, s := range songMap {
songs = append(songs, s)
}
of := fmt.Sprintf("%s/ranked_star_%02d.json", outputDir, star)
if err := writePlaylist(of, fmt.Sprintf("Ranked Songs ★%d", star), "", image, songs); err != nil {
panic(err)
}
}
// by performance point
for pp, songMap := range songByPP {
image, err := getImageByPP(imageDir, int(pp))
if err != nil {
panic(err)
}
songs := make([]*PlaylistSong, 0)
for _, s := range songMap {
songs = append(songs, s)
}
of := fmt.Sprintf("%s/ranked_pp_%02d.json", outputDir, int(pp))
if err := writePlaylist(of, fmt.Sprintf("Ranked Songs %dpp+", int(pp)), "", image, songs); err != nil {
panic(err)
}
}
}
func downloadRankedList() (map[string]RankedEntry, error) {
log.Printf("Downloading %s...\n", RANKED_URL)
req, err := http.NewRequest("GET", RANKED_URL, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("got response code %d: %w", resp.StatusCode, err)
}
reader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
var entries map[string]RankedEntry
if err := json.NewDecoder(reader).Decode(&entries); err != nil {
return nil, err
}
log.Printf("%d songs found.\n", len(entries))
return entries, nil
}
func getImageByStar(imageDir string, star int) (string, error) {
imageFile := fmt.Sprintf("%s/%d.png", imageDir, star)
if _, err := os.Stat(imageFile); err == nil {
b, err := ioutil.ReadFile(imageFile)
if err != nil {
return "", err
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(b), nil
} else {
imageFile = imageDir + "/n.png"
if _, err := os.Stat(imageFile); err == nil {
b, err := ioutil.ReadFile(imageFile)
if err != nil {
return "", err
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(b), nil
}
}
return "", nil
}
func getImageByPP(imageDir string, pp int) (string, error) {
imageFile := fmt.Sprintf("%s/pp_%d.png", imageDir, pp)
if _, err := os.Stat(imageFile); err == nil {
b, err := ioutil.ReadFile(imageFile)
if err != nil {
return "", err
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(b), nil
} else {
imageFile = imageDir + "/n.png"
if _, err := os.Stat(imageFile); err == nil {
b, err := ioutil.ReadFile(imageFile)
if err != nil {
return "", err
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(b), nil
}
}
return "", nil
}
func writePlaylist(fileName string, title string, description string, image string, songs []*PlaylistSong) error {
playlist := Playlist{
Title: title,
Author: "",
Description: description,
Image: image,
Songs: songs,
}
b, err := json.Marshal(playlist)
if err != nil {
return err
}
log.Printf( "Writing %s... (%d songs)\n", fileName, len(songs))
if err := ioutil.WriteFile(fileName, b, 0644); err != nil {
return err
}
return nil
}