-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
394 lines (367 loc) · 11.8 KB
/
endpoints.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package osu
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"regexp"
"time"
)
// BeatmapsIncludeConverted includes converted beatmaps (only valid when mode is not Osu)
func BeatmapsIncludeConverted() BeatmapOption {
return func(s string) string {
return s + fmt.Sprint("&a=1")
}
}
// BeatmapsLimit specifies how many beatmaps to return (default and maximum value is 500)
func BeatmapsLimit(limit int) BeatmapOption {
if limit < 1 {
limit = 1
} else if limit > 500 {
limit = 500
}
return func(s string) string {
return s + fmt.Sprintf("&limit=%d", limit)
}
}
// BeatmapsWithMode specifies which mode to confine results to
func BeatmapsWithMode(mode mode) BeatmapOption {
return func(s string) string {
return s + fmt.Sprintf("&m=%d", mode)
}
}
// BeatmapsWithHash confines the search to beatmaps with a specific BeatmapID
func BeatmapsWithHash(ID string) BeatmapOption {
return func(s string) string {
return s + fmt.Sprintf("&h=%v", url.QueryEscape(ID))
}
}
// BeatmapsWithID confines the search to beatmaps with a specific BeatmapID
func BeatmapsWithID(ID string) BeatmapOption {
return func(s string) string {
return s + fmt.Sprintf("&b=%v", url.QueryEscape(ID))
}
}
// BeatmapsWithSetID confines the search to beatmaps with a specific BeatmapsetID
func BeatmapsWithSetID(ID string) BeatmapOption {
return func(s string) string {
return s + fmt.Sprintf("&s=%v", url.QueryEscape(ID))
}
}
// BeatmapsByCreator confines the search to beatmaps created by a specific user
func BeatmapsByCreator(ID string, IDType usernameType) BeatmapOption {
return func(s string) string {
return s + fmt.Sprintf("&u=%v&type=%v", url.QueryEscape(ID), url.QueryEscape(string(IDType)))
}
}
// BeatmapsSince confines the search to beatmaps ranked or loved since date
func BeatmapsSince(date time.Time) BeatmapOption {
return func(s string) string {
return s + fmt.Sprintf("&since=%v", date.Format("2006-01-02"))
}
}
// Beatmaps fetches a list of beatmaps
func (client *Client) Beatmaps(opts ...BeatmapOption) ([]*Beatmap, error) {
query := apiURL + "get_beatmaps?k=" + client.key
for _, opt := range opts {
query = opt(query)
}
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.Beatmaps: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.Beatmaps: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.Beatmaps: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.Beatmaps: " + err.Error())
}
var regx = regexp.MustCompile(`(date"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
maps := make([]*Beatmap, 0)
err = json.Unmarshal(body, &maps)
return maps, err
}
// UserMode specifies which mode to show info for in the User struct (default is Osu)
func UserMode(mode mode) UserOption {
return func(s string) string {
return s + fmt.Sprintf("&m=%d", mode)
}
}
// UserEventsSince specifies the max number of days between now and last event date. Range of 1-31 (default is 1)
func UserEventsSince(days int) UserOption {
if days < 1 {
days = 1
} else if days > 31 {
days = 31
}
return func(s string) string {
return s + fmt.Sprintf("&event_days=%d", days)
}
}
// User fetches information for a specific user
func (client *Client) User(ID string, IDType usernameType, opts ...UserOption) (*User, error) {
query := apiURL + "get_user?k=" + client.key
query = BeatmapsByCreator(ID, IDType)(query)
for _, opt := range opts {
query = opt(query)
}
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.User: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.User: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.User: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.User: " + err.Error())
}
var regx = regexp.MustCompile(`(date"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
user := make([]*User, 0)
err = json.Unmarshal(body, &user)
if len(user) == 1 {
return user[0], err
}
return nil, err
}
// ScoresWithMode confines results to those with the specified mode
func ScoresWithMode(m mode) ScoresOption {
return func(s string) string {
return s + fmt.Sprintf("&m=%d", m)
}
}
// ScoresByUser confines results to just scores from the specific user
func ScoresByUser(ID string, IDType usernameType) ScoresOption {
return func(s string) string {
return s + fmt.Sprintf("&u=%v&type=%v", url.QueryEscape(ID), url.QueryEscape(string(IDType)))
}
}
// ScoresWithMods confines results to those that have the given mods
func ScoresWithMods(mods ...Mod) ScoresOption {
m := 0
for _, v := range mods {
m |= int(v)
}
return func(s string) string {
return s + fmt.Sprintf("&mods=%d", m)
}
}
// ScoresLimit specifies how many beatmaps to return (default 50, max 100)
func ScoresLimit(limit int) ScoresOption {
if limit < 1 {
limit = 1
} else if limit > 100 {
limit = 100
}
return func(s string) string {
return s + fmt.Sprintf("&limit=%d", limit)
}
}
// Scores fetches a list of Scores for a specific beatmap
func (client *Client) Scores(ID string, opts ...ScoresOption) ([]*Score, error) {
query := apiURL + "get_scores?k=" + client.key
query = BeatmapsWithID(ID)(query)
for _, opt := range opts {
query = opt(query)
}
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.Scores: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.Scores: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.Scores: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.Scores: " + err.Error())
}
var regx = regexp.MustCompile(`(date"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
scores := make([]*Score, 0)
err = json.Unmarshal(body, &scores)
return scores, err
}
// UserBestLimit specifies how many beatmaps to return (default 10, max 100)
func UserBestLimit(limit int) UserBestOption {
if limit < 1 {
limit = 1
} else if limit > 100 {
limit = 100
}
return func(s string) string {
return s + fmt.Sprintf("&limit=%d", limit)
}
}
// UserBestWithMode confines results to those with the specified mode
func UserBestWithMode(m mode) UserBestOption {
return func(s string) string {
return s + fmt.Sprintf("&m=%d", m)
}
}
// UserBest returns a list of the top scores for the specified user
func (client *Client) UserBest(ID string, IDType usernameType, opts ...UserBestOption) ([]*BestScore, error) {
query := apiURL + "get_user_best?k=" + client.key
query = BeatmapsByCreator(ID, IDType)(query)
for _, opt := range opts {
query = opt(query)
}
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.UserBest: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.UserBest: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.UserBest: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.UserBest: " + err.Error())
}
var regx = regexp.MustCompile(`(date"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
scores := make([]*BestScore, 0)
err = json.Unmarshal(body, &scores)
return scores, err
}
// UserRecentLimit specifies how many beatmaps to return (default 10, max 50)
func UserRecentLimit(limit int) UserRecentOption {
if limit < 1 {
limit = 1
} else if limit > 50 {
limit = 100
}
return func(s string) string {
return s + fmt.Sprintf("&limit=%d", limit)
}
}
// UserRecentWithMode confines results to those with the specified mode
func UserRecentWithMode(m mode) UserRecentOption {
return func(s string) string {
return s + fmt.Sprintf("&m=%d", m)
}
}
// UserRecent returns a list of the top scores for the specified user
func (client *Client) UserRecent(ID string, IDType usernameType, opts ...UserRecentOption) ([]*RecentScore, error) {
query := apiURL + "get_user_recent?k=" + client.key
query = BeatmapsByCreator(ID, IDType)(query)
for _, opt := range opts {
query = opt(query)
}
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.UserRecent: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.UserRecent: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.UserRecent: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.UserRecent: " + err.Error())
}
var regx = regexp.MustCompile(`(date"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
scores := make([]*RecentScore, 0)
err = json.Unmarshal(body, &scores)
return scores, err
}
// ReplayWithMods confines the result to a replay with the given mods
func ReplayWithMods(mods ...Mod) ReplayOption {
m := 0
for _, v := range mods {
m |= int(v)
}
return func(s string) string {
return s + fmt.Sprintf("&mods=%d", m)
}
}
// Replay returns the data for the given beatmap, played by the specified user in the specified mode
func (client *Client) Replay(mode mode, beatmapID string, userID string, opts ...ReplayOption) (*Replay, error) {
query := apiURL + "get_replay?k=" + client.key
query = BeatmapsWithMode(mode)(query)
query = BeatmapsWithID(beatmapID)(query)
query += "&u=" + userID
for _, opt := range opts {
query = opt(query)
}
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.Replay: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.Replay: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.Replay: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.Replay: " + err.Error())
}
var regx = regexp.MustCompile(`(date"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
var replay Replay
err = json.Unmarshal(body, &replay)
return &replay, err
}
// Match fetches a multiplayer match with the given ID
func (client *Client) Match(matchID string) (*Match, error) {
query := apiURL + "get_match?k=" + client.key + "&mp=" + matchID
resp, err := client.c.Get(query)
if err != nil {
return nil, errors.New("osu.Client.Match: " + err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("osu.Client.Match: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("osu.Client.Match: " + err.Error())
}
err = errorCheck(body)
if err != nil {
return nil, errors.New("osu.Client.Match: " + err.Error())
}
var regx = regexp.MustCompile(`(time"[[:space:]]*:[[:space:]]*"[0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2})"`)
body = regx.ReplaceAll(body, []byte(`${1}T${2}-00:00"`))
match := make([]*Match, 0)
fmt.Println(string(body))
err = json.Unmarshal(body, &match)
if err != nil {
return nil, errors.New("osu.Client.Match: No matches found")
}
if len(match) == 1 {
return match[0], err
}
return nil, errors.New("osu.Client.Match: No matches found")
}