-
Notifications
You must be signed in to change notification settings - Fork 0
/
atcoder.go
65 lines (53 loc) · 1.44 KB
/
atcoder.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
package future
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
)
type AtCoder struct {
ID string `json:"id"`
Title string `json:"title"`
StartTime int64 `json:"startTimeSeconds"`
Duration int64 `json:"durationSeconds"`
Rated string `json:"ratedRange"`
}
func (module *ContestModule) SetAtCoder() bool {
var request *http.Request = module.Context.Request
context := appengine.NewContext(request)
client := urlfetch.Client(context)
// GETリクエスト
response, err := client.Get("https://atcoder-api.appspot.com/contests")
if err != nil {
log.Print(err)
return false
}
// データをバイナリとして取得
byteArray, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Print(err)
return false
}
// バイナリをAtCoderのデータにパース
var atcoderContests []AtCoder
json.Unmarshal(byteArray, &atcoderContests)
var contests []Contest
for _, contest := range atcoderContests {
if time.Now().Unix() >= contest.StartTime {
continue
}
contests = append(contests, Contest{
ID: contest.ID,
Title: contest.Title,
StartTime: contest.StartTime,
Duration: contest.Duration,
WebSiteName: "atcoder",
})
}
// モジュールに入れる
module.AtCoder = append(module.AtCoder, contests...)
return true
}