-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatcoder.go
More file actions
388 lines (327 loc) · 10.3 KB
/
atcoder.go
File metadata and controls
388 lines (327 loc) · 10.3 KB
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
// package main
package hello
import (
"bytes"
"encoding/gob"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strconv"
"strings"
"time"
"cloud.google.com/go/storage"
"google.golang.org/appengine/file"
"github.com/gin-gonic/gin"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
"github.com/PuerkitoBio/goquery"
)
// AtCoderのコンテスト情報を管理するモジュール
type AtCoder struct {
Contests []AtCoderContest
RawContests []RawAtCoderContest
HttpClient *http.Client
Context *gin.Context
}
type RawAtCoderContest struct {
ID string
Title string
StartTime string
Duration string
Rated string
}
// AtCoderのコンテスト情報
type AtCoderContest struct {
ID string `json:"id"`
Title string `json:"title"`
StartTime int64 `json:"startTimeSeconds"`
Duration int64 `json:"durationSeconds"`
Rated string `json:"ratedRange"`
}
func (atcoder *AtCoder) FileIO(operate string) {
var r *http.Request = atcoder.Context.Request
var w http.ResponseWriter = atcoder.Context.Writer
ctx := appengine.NewContext(r)
// デフォルトのバケットを指定する(App Engineのコンテストから取得できる)
bucket, err := file.DefaultBucketName(ctx)
if err != nil {
log.Errorf(ctx, "Faild to get default GCS bucket name: %v", err)
}
// clientをつくる
client, err := storage.NewClient(ctx)
if err != nil {
log.Errorf(ctx, "Faild to create client: %v", err)
}
defer client.Close()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
buf := &bytes.Buffer{}
d := &demo{
w: buf,
ctx: ctx,
client: client,
bucket: client.Bucket(bucket),
bucketName: bucket,
}
fileName := "demo-testfile-go"
if operate == "write" {
// コンテストデータをバイナリにエンコード
var binaryData []byte
Encode(atcoder.Contests, &binaryData)
// ファイルに書き込む
d.createFile(fileName, binaryData)
log.Infof(ctx, "Write to file")
} else if operate == "read" {
// ファイルをバイナリで読み込む
var binaryData []byte
d.readFile(fileName, &binaryData)
// バイナリをコンテストデータにデコード
Decode(binaryData, &atcoder.Contests)
log.Infof(ctx, "Read file")
} else {
log.Infof(ctx, "operation is not read and write")
}
if d.failed {
w.WriteHeader(http.StatusInternalServerError)
buf.WriteTo(w)
} else {
w.WriteHeader(http.StatusOK)
buf.WriteTo(w)
}
}
//[START write]
func (d *demo) createFile(fileName string, byteDataToWrite []byte) {
wc := d.bucket.Object(fileName).NewWriter(d.ctx)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"x-goog-meta-foo": "foo",
"x-goog-meta-bar": "bar",
}
d.cleanUp = append(d.cleanUp, fileName)
// 書き込む
if _, err := wc.Write(byteDataToWrite); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
// ファイル閉じてるのかな?これがないと書き込めない
if err := wc.Close(); err != nil {
d.errorf("createFile: unable to close bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
}
//[END write]
//[START read]
func (d *demo) readFile(fileName string, data *[]byte) {
// ファイルを開く
rc, err := d.bucket.Object(fileName).NewReader(d.ctx)
if err != nil {
d.errorf("readFile: unable to open file from bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
defer rc.Close()
// データを読み込む
slurp, err := ioutil.ReadAll(rc)
if err != nil {
d.errorf("readFile: unable to read data from bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
*data = slurp
}
//[END read]
// dataをbyteArrayにエンコードする
func Encode(data interface{}, byteArray *[]byte) {
buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(data)
if err != nil {
// log.Print("encode: ", err)
}
*byteArray = buffer.Bytes()
}
// byteArrayをdataにデコードする(dataはポインタ型)
func Decode(byteArray []byte, data interface{}) {
buffer := bytes.NewBuffer(byteArray)
dec := gob.NewDecoder(buffer)
err := dec.Decode(data)
if err != nil {
// log.Print("decode: ", err)
}
}
func (d *demo) errorf(format string, args ...interface{}) {
d.failed = true
fmt.Fprintln(d.w, fmt.Sprintf(format, args...))
log.Errorf(d.ctx, format, args...)
}
func (atcoder *AtCoder) SetContestData(context *gin.Context) {
// atcoder.HttpClient = client
atcoder.Context = context
// 予定されたコンテストの生データを取得
atcoder.GetFutureContest()
// 過去のコンテストの生データを取得
atcoder.GetPastContest()
// 5. 生のコンテストデータをパースする
for _, rawContest := range atcoder.RawContests {
atcoder.Contests = append(atcoder.Contests, ParseSum(rawContest))
}
sort.Slice(atcoder.Contests, func(i, j int) bool { return atcoder.Contests[i].StartTime < atcoder.Contests[j].StartTime })
// ファイルに書き込む
atcoder.FileIO("write")
// atcoder.StoreGob("contests")
}
// 予定されたコンテストデータを取得する
func (atcoder *AtCoder) GetFutureContest() {
// var responseWriter http.ResponseWriter = atcoder.Context.Writer
var request *http.Request = atcoder.Context.Request
context := appengine.NewContext(request)
client := urlfetch.Client(context)
// 1. GETリクエスト
response, err := client.Get("https://beta.atcoder.jp/contests/?lang=ja")
time.Sleep(2 * time.Second)
if err != nil {
fmt.Print(err)
return
}
// 2. goqueryを使えるようにする
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
fmt.Print(err)
}
// 3. 予定されたコンテストのテーブルを取得
var tableSelection *goquery.Selection
doc.Find("h3").Each(func(i int, s *goquery.Selection) {
if h3 := s.Text(); h3 == "予定されたコンテスト" {
tableSelection = s.Next()
}
})
// 4. 生のコンテストデータを取得
atcoder.GetRawContestFromTable(tableSelection)
// 5. 生のコンテストデータをパースする
// for _, rawContest := range atcoder.RawContests {
// atcoder.Contests = append(atcoder.Contests, ParseSum(rawContest))
// }
}
// 過去のコンテストデータを取得する
func (atcoder *AtCoder) GetPastContest() {
baseURL := "https://beta.atcoder.jp/contests/archive?lang=ja"
// 準備
var request *http.Request = atcoder.Context.Request
context := appengine.NewContext(request)
client := urlfetch.Client(context)
numberOfPage, ok := atcoder.GetNumberOfPage(baseURL)
// ページ番号の取得に失敗
if !ok {
log.Infof(context, "Faild to get number of page!!")
return
}
for page := 1; page <= numberOfPage; page++ {
// 1. GETリクエスト
response, err := client.Get(baseURL + "&page=" + strconv.Itoa(page))
time.Sleep(2 * time.Second)
if err != nil {
fmt.Print(err)
return
}
// 2. goqueryを使えるようにする
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
fmt.Print(err)
}
// 3. 予定されたコンテストのテーブルを取得
var tableSelection *goquery.Selection = doc.Find("table")
// 4. 生のコンテストデータを取得
atcoder.GetRawContestFromTable(tableSelection)
// 5. 生のコンテストデータをパースする
// for _, rawContest := range atcoder.RawContests {
// atcoder.Contests = append(atcoder.Contests, ParseSum(rawContest))
// }
}
}
func (atcoder *AtCoder) GetNumberOfPage(baseURL string) (int, bool) {
var request *http.Request = atcoder.Context.Request
context := appengine.NewContext(request)
client := urlfetch.Client(context)
// 1. Getリクエスト
response, err := client.Get(baseURL)
time.Sleep(2 * time.Second)
if err != nil {
fmt.Print(err)
return 0, false
}
// 2. goqueryを使えるようにする
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
fmt.Print(err)
return 0, false
}
// 3. 番号を取得
numberOfPage := 1
doc.Find("ul > li > a").Each(func(i int, s *goquery.Selection) {
href, exists := s.Attr("href")
// aタグにhrefが存在しない
if !exists {
return
}
// hrefに[page=]が含まれない
if !strings.Contains(href, "page=") {
return
}
// タグの中身を数値にできる
if page, err := strconv.Atoi(s.Text()); err == nil {
// log.Infof(context, "pageNumger(nomikura): %+v", page)
if page > numberOfPage {
numberOfPage = page
}
}
})
// log.Infof(context, "pageSize(nomikura): %+v", pageSize)
return numberOfPage, true
}
// 生のコンテストデータをパースする
func ParseSum(rawContest RawAtCoderContest) AtCoderContest {
// Durationを求める
str := strings.Replace(rawContest.Duration, ":", "h", 1) + "m"
tim, _ := time.ParseDuration(str)
duration := int64(tim.Seconds())
// StartTimeを求める
start := rawContest.StartTime
atoi := func(str string) int {
ret, _ := strconv.Atoi(str)
return ret
}
// [2018-09-22 21:00:00+0900]の形式で抜き出した時間を無理矢理Timeオブジェクトにする
year, month, day, hour, minute := atoi(start[:4]), atoi(start[5:7]), atoi(start[8:10]), atoi(start[11:13]), atoi(start[14:16])
// 取得する時間はJSTなので、日本時間をTimeオブジェクトにするように処理する
jst, _ := time.LoadLocation("Asia/Tokyo")
startTime := time.Date(year, time.Month(month), day, hour, minute, 0, 0, jst)
unix := startTime.Unix()
return AtCoderContest{
Title: rawContest.Title,
ID: rawContest.ID,
StartTime: unix,
Duration: duration,
Rated: rawContest.Rated,
}
}
// 生のコンテストデータをセットする
func (atcoder *AtCoder) GetRawContestFromTable(tableSelection *goquery.Selection) {
tableSelection.Find("div > table > tbody > tr").Each(func(i int, trSelection *goquery.Selection) {
// とりあえず文字列でテーブル情報を取得
var href string
var rawData [4]string
trSelection.Find("td").Each(func(i int, tdSelection *goquery.Selection) {
rawData[i] = tdSelection.Text()
if i == 1 {
href, _ = tdSelection.Find("a").Attr("href")
}
})
rawContest := RawAtCoderContest{
StartTime: rawData[0],
Title: rawData[1],
Duration: rawData[2],
Rated: rawData[3],
ID: href[10:],
}
atcoder.RawContests = append(atcoder.RawContests, rawContest)
})
}