-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeForces.go
70 lines (59 loc) · 1.76 KB
/
codeForces.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
package future
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
)
// Cedeforcesコンテスト情報
type CodeForces struct {
Status string `json:"status"`
Result []struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Phase string `json:"phase"`
Frozen bool `json:"frozen"`
DurationSeconds int `json:"durationSeconds"`
StartTimeSeconds int `json:"startTimeSeconds"`
RelativeTimeSeconds int `json:"relativeTimeSeconds"`
} `json:"result"`
}
func (module *ContestModule) SetCodeforces() bool {
var request *http.Request = module.Context.Request
context := appengine.NewContext(request)
client := urlfetch.Client(context)
// GETリクエスト
response, err := client.Get("https://codeforces.com/api/contest.list")
if err != nil {
log.Print(err)
return false
}
// データをバイナリとして取得
byteArray, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Print(err)
return false
}
// バイナリをAtCoderのデータにパース
var codeforcesContest CodeForces
json.Unmarshal(byteArray, &codeforcesContest)
var contests []Contest
for _, contest := range codeforcesContest.Result {
if contest.Phase != "BEFORE" {
continue
}
contests = append(contests, Contest{
ID: strconv.Itoa(contest.ID),
Title: contest.Name,
StartTime: int64(contest.StartTimeSeconds),
Duration: int64(contest.DurationSeconds),
WebSiteName: "codeforces",
})
}
module.Codeforces = append(module.Codeforces, contests...)
return true
}