-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser.go
328 lines (289 loc) · 7.61 KB
/
user.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
package goforces
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
//User represents Codeforces User
type User struct {
LastName string `json:"lastName"`
Country string `json:"country"`
LastOnlineTimeSeconds int `json:"lastOnlineTimeSeconds"`
City string `json:"city"`
Rating int `json:"rating"`
FriendOfCount int `json:"friendOfCount"`
TitlePhoto string `json:"titlePhoto"`
Handle string `json:"handle"`
Avatar string `json:"avatar"`
FirstName string `json:"firstName"`
Contribution int `json:"contribution"`
Organization string `json:"organization"`
Rank string `json:"rank"`
MaxRating int `json:"maxRating"`
RegistrationTimeSeconds int `json:"registrationTimeSeconds"`
MaxRank string `json:"maxRank"`
}
const (
divsionRatingThreshold = 1900
)
// Div1 returns boolean whether user belog to div1
func (u User) Div1() bool {
if u.Rating >= divsionRatingThreshold {
return true
}
return false
}
// Div2 returns boolean whether user belog to div2
func (u User) Div2() bool {
if u.Rating < divsionRatingThreshold {
return true
}
return false
}
// Color returns user's color of rating.
// http://codeforces.com/blog/entry/20638
func (u User) Color() string {
//div1
if u.Div1() {
if u.Rating >= 1900 && u.Rating < 2200 {
return "Violet"
}
if u.Rating >= 2200 && u.Rating < 2400 {
return "Orange"
}
// rating >= 2400
return "Red"
}
//div2
if u.Rating < 1200 {
return "Gray"
}
if u.Rating >= 1200 && u.Rating < 1400 {
return "Green"
}
if u.Rating >= 1400 && u.Rating < 1599 {
return "Cyan"
}
//1600 <= rating < 1899
return "Blue"
}
//GetUserBlogEntries implements /user.blogEntries
func (c *Client) GetUserBlogEntries(ctx context.Context, handle string) ([]BlogEntry, error) {
c.Logger.Println("GetUserBlogEntries :", handle)
v := url.Values{}
v.Add("handle", handle)
spath := "/user.blogEntries" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []BlogEntry `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error : %s", resp.Status)
}
return resp.Result, nil
}
//GetUserInfo implements /user.info
func (c *Client) GetUserInfo(ctx context.Context, handles []string) ([]User, error) {
c.Logger.Println("GetUserInfo :", handles)
v := url.Values{}
v.Add("handles", strings.Join(handles, ";"))
spath := "/user.info" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []User `json:"result"`
}
var resp Response
if err := decodeBody(
res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error : %s", resp.Status)
}
return resp.Result, nil
}
//UserRatedListOptions represents the options of /user.ratedlist
type UserRatedListOptions struct {
ActiveOnly bool
}
func (o *UserRatedListOptions) options() interface{} {
if o == nil {
return nil
}
type option struct {
ActiveOnly bool `url:"activeOnly,omitempty"`
}
return &option{ActiveOnly: o.ActiveOnly}
}
//GetUserRatedList implements /user.ratedList
func (c *Client) GetUserRatedList(ctx context.Context, options *UserRatedListOptions) ([]User, error) {
c.Logger.Println("GetUserRatedList :", options)
v := url.Values{}
spath := "/user.ratedList" + "?" + v.Encode() + "&"
req, err := c.newRequest(ctx, "GET", spath, options.options(), nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []User `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error : %s", resp.Status)
}
return resp.Result, nil
}
//GetUserRating implements /user.rating
func (c *Client) GetUserRating(ctx context.Context, handle string) ([]RatingChange, error) {
c.Logger.Println("GetUserRating :", handle)
v := url.Values{}
v.Add("handle", handle)
spath := "/user.rating" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []RatingChange `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error : %s", resp.Status)
}
return resp.Result, nil
}
//UserStatusOptions represents the opetions of /user.status
type UserStatusOptions struct {
From int
Count int
}
func (o *UserStatusOptions) options() interface{} {
if o == nil {
return nil
}
type option struct {
From int `url:"from"`
Count int `url:"count"`
}
return &option{From: o.From, Count: o.Count}
}
//GetUserStatus implements /user.status
func (c *Client) GetUserStatus(ctx context.Context, handle string, options *UserStatusOptions) ([]Submission, error) {
c.Logger.Println("GetUserStatus :", handle)
v := url.Values{}
v.Add("handle", handle)
spath := "/user.status" + "?" + v.Encode() + "&"
req, err := c.newRequest(ctx, "GET", spath, options.options(), nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []Submission `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error : %s", resp.Status)
}
return resp.Result, nil
}
//UserFriendsOptions represents the opetions of /user.friends
type UserFriendsOptions struct {
OnlyOnline bool
}
func (o *UserFriendsOptions) options() interface{} {
if o == nil {
return nil
}
type option struct {
OnlyOnline bool `url:"onlyOnline"`
}
return &option{OnlyOnline: o.OnlyOnline}
}
//GetUserFriends implements /user.friends
//You must your api key and secret key before call this method.
func (c *Client) GetUserFriends(ctx context.Context, options *UserFriendsOptions) ([]string, error) {
c.Logger.Println("GetUserFriends :", options)
v := url.Values{}
//check api key and scret
if c.APIKey == "" || c.APISecret == "" {
return nil, fmt.Errorf("GetUserFriends requires your api key and api secret")
}
v.Add("apiKey", c.APIKey)
v.Add("time", strconv.FormatInt(time.Now().Unix(), 10))
apiSig := generateAPISig("user.friends", c.APISecret, v)
v.Add("apiSig", apiSig)
spath := "/user.friends" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, options.options(), nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []string `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error : %s", resp.Status)
}
return resp.Result, nil
}