forked from osuthailand/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beatmap.go
executable file
·106 lines (90 loc) · 2.46 KB
/
beatmap.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strconv"
"github.com/gin-gonic/gin"
"github.com/osuripple/cheesegull/models"
)
type beatmapPageData struct {
baseTemplateData
Found bool
Beatmap models.Beatmap
Beatmapset models.Set
SetJSON string
}
func beatmapInfo(c *gin.Context) {
data := new(beatmapPageData)
defer resp(c, 200, "beatmap.html", data)
b := c.Param("bid")
if _, err := strconv.Atoi(b); err != nil {
c.Error(err)
} else {
data.Beatmap, err = getBeatmapData(b)
if err != nil {
c.Error(err)
return
}
data.Beatmapset, err = getBeatmapSetData(data.Beatmap)
if err != nil {
c.Error(err)
return
}
sort.Slice(data.Beatmapset.ChildrenBeatmaps, func(i, j int) bool {
if data.Beatmapset.ChildrenBeatmaps[i].Mode != data.Beatmapset.ChildrenBeatmaps[j].Mode {
return data.Beatmapset.ChildrenBeatmaps[i].Mode < data.Beatmapset.ChildrenBeatmaps[j].Mode
}
return data.Beatmapset.ChildrenBeatmaps[i].DifficultyRating < data.Beatmapset.ChildrenBeatmaps[j].DifficultyRating
})
}
if data.Beatmapset.ID == 0 {
data.TitleBar = T(c, "Beatmap not found.")
data.Messages = append(data.Messages, errorMessage{T(c, "Beatmap could not be found.")})
return
}
data.KyutGrill = fmt.Sprintf("https://assets.ppy.sh/beatmaps/%d/covers/cover.jpg?%d", data.Beatmapset.ID, data.Beatmapset.LastUpdate.Unix())
data.KyutGrillAbsolute = true
setJSON, err := json.Marshal(data.Beatmapset)
if err == nil {
data.SetJSON = string(setJSON)
} else {
data.SetJSON = "[]"
}
data.TitleBar = T(c, "%s - %s", data.Beatmapset.Artist, data.Beatmapset.Title)
data.Scripts = append(data.Scripts, "/static/tablesort.js", "/static/beatmap.js")
}
func getBeatmapData(b string) (beatmap models.Beatmap, err error) {
resp, err := http.Get(config.CheesegullAPI + "/b/" + b)
if err != nil {
return beatmap, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return beatmap, err
}
err = json.Unmarshal(body, &beatmap)
if err != nil {
return beatmap, err
}
return beatmap, nil
}
func getBeatmapSetData(beatmap models.Beatmap) (bset models.Set, err error) {
resp, err := http.Get(config.CheesegullAPI + "/s/" + strconv.Itoa(beatmap.ParentSetID))
if err != nil {
return bset, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return bset, err
}
err = json.Unmarshal(body, &bset)
if err != nil {
return bset, err
}
return bset, nil
}