Skip to content

Commit

Permalink
Add the last submission info in ~/.cfsession. Close #10
Browse files Browse the repository at this point in the history
  • Loading branch information
xalanq committed Jul 11, 2019
1 parent a838bcb commit f66ca24
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 33 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -60,7 +60,7 @@ Usage:
cf watch [all] [<contest-id>]
cf open [<contest-id>] [<problem-id>]
cf stand [<contest-id>]
cf sid <submission-id> [<contest-id>]
cf sid [<submission-id>] [<contest-id>]
cf race <contest-id>
cf pull [ac] [<contest-id>] [<problem-id>]
cf upgrade
Expand Down Expand Up @@ -91,6 +91,7 @@ Examples:
cf open 1136 Use default web browser to open the page of contest 1136.
cf stand Use default web browser to open the standing page.
cf sid 52531875 Use default web browser to open the submission 52531875's page.
cf sid Open the last submission's page.
cf race 1136 If the contest 1136 has not started yet, it will countdown. After the
countdown ends, it will run 'cf open 1136 a', 'cf open 1136 b', ...,
'cf open 1136 e', 'cf parse 1136'.
Expand Down
5 changes: 3 additions & 2 deletions README_zh_CN.md
Expand Up @@ -58,7 +58,7 @@ $ go build -ldflags "-s -w" cf.go
cf watch [all] [<contest-id>]
cf open [<contest-id>] [<problem-id>]
cf stand [<contest-id>]
cf sid <submission-id> [<contest-id>]
cf sid [<submission-id>] [<contest-id>]
cf race <contest-id>
cf pull [ac] [<contest-id>] [<problem-id>]
cf upgrade
Expand Down Expand Up @@ -87,7 +87,8 @@ $ go build -ldflags "-s -w" cf.go
cf open 1136 a 用默认的浏览器打开比赛 id 为 1136 的题目 a。
cf open 1136 用默认的浏览器打开比赛 id 为 1136 的总览页面。
cf stand 用默认的浏览器打开当前比赛的榜单。
cf sid 52531875 用默认的浏览器打开 52531875 这个提交页面.
cf sid 52531875 用默认的浏览器打开 52531875 这个提交页面。
cf sid 打开最后一次提交的页面。
cf race 1136 如果比赛还未开始且进入倒计时,则该命令会倒计时。当倒计时完后,会自动执行
'cf open 1136 a', 'cf open 1136 b', ..., 'cf open 1136 e', 'cf parse 1136'
cf pull 100 拉取比赛 id 为 100 每道题的最新代码到文件夹 "./100/<problem-id>" 下。
Expand Down
5 changes: 3 additions & 2 deletions cf.go
Expand Up @@ -12,7 +12,7 @@ import (
docopt "github.com/docopt/docopt-go"
)

const version = "v0.5.5"
const version = "v0.5.6"

func main() {
usage := `Codeforces Tool $%version%$ (cf). https://github.com/xalanq/cf-tool
Expand All @@ -31,7 +31,7 @@ Usage:
cf watch [all] [<contest-id>]
cf open [<contest-id>] [<problem-id>]
cf stand [<contest-id>]
cf sid <submission-id> [<contest-id>]
cf sid [<submission-id>] [<contest-id>]
cf race <contest-id>
cf pull [ac] [<contest-id>] [<problem-id>]
cf upgrade
Expand Down Expand Up @@ -62,6 +62,7 @@ Examples:
cf open 1136 Use default web browser to open the page of contest 1136.
cf stand Use default web browser to open the standing page.
cf sid 52531875 Use default web browser to open the submission 52531875's page.
cf sid Open the last submission's page.
cf race 1136 If the contest 1136 has not started yet, it will countdown. After the
countdown ends, it will run 'cf open 1136 a', 'cf open 1136 b', ...,
'cf open 1136 e', 'cf parse 1136'.
Expand Down
13 changes: 7 additions & 6 deletions client/client.go
Expand Up @@ -11,17 +11,18 @@ import (

// Client codeforces client
type Client struct {
Jar *cookiejar.Jar `json:"cookies"`
Username string `json:"username"`
Ftaa string `json:"ftaa"`
Bfaa string `json:"bfaa"`
path string
Jar *cookiejar.Jar `json:"cookies"`
Username string `json:"username"`
Ftaa string `json:"ftaa"`
Bfaa string `json:"bfaa"`
LastSubmission *SaveSubmission `json:"last_submission"`
path string
}

// New client
func New(path string) *Client {
jar, _ := cookiejar.New(nil)
c := &Client{Jar: jar, path: path}
c := &Client{Jar: jar, path: path, LastSubmission: nil}
c.load()
return c
}
Expand Down
21 changes: 19 additions & 2 deletions client/submit.go
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/fatih/color"
)

// SaveSubmission save it in session
type SaveSubmission struct {
ContestID string `json:"contest_id"`
SubmissionID string `json:"submission_id"`
}

func findErrorSource(body []byte) ([]byte, error) {
reg := regexp.MustCompile(`"error\sfor__source">(.*?)</span>`)
tmp := reg.FindSubmatch(body)
Expand Down Expand Up @@ -61,7 +67,7 @@ func (c *Client) SubmitContest(contestID, problemID, langID, source string) (err
"sourceCodeConfirmed": {"true"},
})
if err != nil {
return err
return
}

defer resp.Body.Close()
Expand All @@ -75,5 +81,16 @@ func (c *Client) SubmitContest(contestID, problemID, langID, source string) (err
}
color.Green("Submitted")

return c.WatchSubmission(fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID), 1, true)
submissions, err := c.WatchSubmission(contestID, 1, true)
if err != nil {
return
}

c.LastSubmission = &SaveSubmission{
ContestID: contestID,
SubmissionID: submissions[0].ParseID(),
}
c.save()

return
}
11 changes: 5 additions & 6 deletions client/watch.go
Expand Up @@ -271,14 +271,15 @@ func (c *Client) getSubmissions(myURL string, n int) (submissions []Submission,
}

// WatchSubmission n is the number of submissions
func (c *Client) WatchSubmission(myURL string, n int, line bool) (err error) {
func (c *Client) WatchSubmission(contestID string, n int, line bool) (submissions []Submission, err error) {
URL := fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID)
maxWidth := 0
first := true
for {
st := time.Now()
submissions, err := c.getSubmissions(myURL, n)
submissions, err = c.getSubmissions(URL, n)
if err != nil {
return err
return
}
display(submissions, first, &maxWidth, line)
first = false
Expand All @@ -289,15 +290,13 @@ func (c *Client) WatchSubmission(myURL string, n int, line bool) (err error) {
}
}
if endCount == len(submissions) {
break
return
}
sub := time.Now().Sub(st)
if sub < time.Second {
time.Sleep(time.Duration(time.Second - sub))
}
}

return nil
}

var colorMap = map[string]color.Attribute{
Expand Down
31 changes: 23 additions & 8 deletions cmd/browser.go
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"

"github.com/skratchdot/open-golang/open"
"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
)

// Open command
Expand Down Expand Up @@ -34,13 +36,26 @@ func Stand(args map[string]interface{}) error {

// Sid command
func Sid(args map[string]interface{}) error {
contestID, err := getContestID(args)
if err != nil {
return err
}
c, _ := args["<submission-id>"].(string)
if _, err := strconv.Atoi(c); err == nil {
return open.Run(fmt.Sprintf("https://codeforces.com/contest/%v/submission/%v", contestID, c))
contestID := ""
submissionID := ""
if args["<submission-id>"] == nil {
cln := client.New(config.SessionPath)
if cln.LastSubmission != nil {
contestID = cln.LastSubmission.ContestID
submissionID = cln.LastSubmission.SubmissionID
} else {
return fmt.Errorf(`You have not submitted any problem yet`)
}
} else {
var err error
contestID, err = getContestID(args)
if err != nil {
return err
}
submissionID, _ = args["<submission-id>"].(string)
if _, err = strconv.Atoi(submissionID); err != nil {
return err
}
}
return fmt.Errorf(`Submission ID should be a number instead of "%v"`, c)
return open.Run(fmt.Sprintf("https://codeforces.com/contest/%v/submission/%v", contestID, submissionID))
}
9 changes: 3 additions & 6 deletions cmd/watch.go
@@ -1,29 +1,26 @@
package cmd

import (
"fmt"

"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
)

// Watch command
func Watch(args map[string]interface{}) error {
contest, err := getContestID(args)
contestID, err := getContestID(args)
if err != nil {
return err
}
cfg := config.New(config.ConfigPath)
cln := client.New(config.SessionPath)
URL := fmt.Sprintf("https://codeforces.com/contest/%v/my", contest)
n := 10
if args["all"].(bool) {
n = -1
}
err = cln.WatchSubmission(URL, n, false)
_, err = cln.WatchSubmission(contestID, n, false)
if err != nil {
if err = loginAgain(cfg, cln, err); err == nil {
err = cln.WatchSubmission(URL, 10, false)
_, err = cln.WatchSubmission(contestID, n, false)
}
}
if err != nil {
Expand Down

0 comments on commit f66ca24

Please sign in to comment.