-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
97 lines (82 loc) · 2.07 KB
/
client.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
package slices
import (
"net/http"
"time"
"encoding/json"
"io/ioutil"
)
const apiEndpoint = "https://scorescraper.herokuapp.com/"
const apiUserPath = "user/"
/*
"play_count": 151,
"rank_global": 18251,
"rank_region": 9070,
"scores": [],
"total_pp": 1386.03,
"total_score": 49711395,
"user_id": 76561198057633470,
"user_name": "tyler.morita"
*/
type User struct {
PlayCount int `json:"play_count"`
RankGlobal int `json:"rank_global"`
RankRegion int `json:"rank_region"`
Scores []UserScore `json:"scores"`
TotalPP float64 `json:"total_pp"`
TotalScore int `json:"total_score"`
UserId int64 `json:"user_id"`
UserName string `json:"user_name"`
}
/*
{
"accuracy": 0.7711,
"author": "Speoghi",
"difficulty": "Expert",
"max_pp": 127.45110074474177,
"net_pp": 88.99,
"raw_pp": 88.99,
"song_id": 11805,
"song_rank": 2966,
"time": "2019-03-02 04:54:47 UTC",
"title": "Katy Perry - California Gurls"
},
*/
type UserScore struct {
Accuracy float64 `json:"accuracy"`
Author string `json:"author"`
Difficulty string `json:"difficulty"`
MaxPP float64 `json:"max_pp"`
NetPP float64 `json:"net_pp"`
RawPP float64 `json:"raw_pp"`
SongId int64 `json:"song_id"`
SongRank int64 `json:"song_rank"`
Time string `json:"time"`
Title string `json:"title"`
}
func GetUser(user string) (*User, error) {
endpoint := apiEndpoint + apiUserPath + user
request, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
client := http.Client { Timeout: time.Duration(30 * time.Second) }
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ret := &User{}
err = json.Unmarshal(body, ret)
if err != nil {
return nil, err
}
return ret, err
}
type ByTotalPP []*User
func (s ByTotalPP) Len() int { return len(s) }
func (s ByTotalPP) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByTotalPP) Less(i, j int) bool { return s[i].TotalPP > s[j].TotalPP }