forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
race.go
77 lines (69 loc) · 1.68 KB
/
race.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
package client
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"time"
"github.com/fatih/color"
ansi "github.com/k0kubun/go-ansi"
"github.com/skratchdot/open-golang/open"
)
func findCountdown(body []byte) (int, error) {
reg := regexp.MustCompile(`class=["']countdown["'][\s\S]*?(\d+):(\d+):(\d+)`)
tmp := reg.FindSubmatch(body)
if tmp == nil {
return 0, errors.New("Cannot find countdown")
}
h, _ := strconv.Atoi(string(tmp[1]))
m, _ := strconv.Atoi(string(tmp[2]))
s, _ := strconv.Atoi(string(tmp[3]))
return h*60*60 + m*60 + s, nil
}
func raceContest(contestID string) (err error) {
for _, problemID := range []string{"A", "B", "C", "D", "E"} {
open.Run(fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problemID))
}
return nil
}
// RaceContest wait for contest starting
func (c *Client) RaceContest(contestID string) (err error) {
color.Cyan("Race for contest %v\n", contestID)
statisURL := fmt.Sprintf("https://codeforces.com/contest/%v", contestID)
client := &http.Client{Jar: c.Jar}
resp, err := client.Get(statisURL)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = checkLogin(c.Username, body)
if err != nil {
return
}
_, err = findStatisBlock(body)
if err != nil {
count, err := findCountdown(body)
if err != nil {
return err
}
color.Green("Count down: ")
count--
time.Sleep(900 * time.Millisecond)
for count > 0 {
time.Sleep(time.Second)
count--
h := count / 60 / 60
m := count/60 - h*60
s := count - h*60*60 - m*60
fmt.Printf("%02d:%02d:%02d\n", h, m, s)
ansi.CursorUp(1)
}
}
return raceContest(contestID)
}