Skip to content

Commit

Permalink
Add a host option in configuration. Close #28
Browse files Browse the repository at this point in the history
  • Loading branch information
xalanq committed Aug 15, 2019
1 parent 0b8945e commit 8832afe
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 28 deletions.
46 changes: 46 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package client

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"

"github.com/fatih/color"
"github.com/xalanq/cf-tool/cookiejar"
"github.com/xalanq/cf-tool/util"
)

// Client codeforces client
Expand All @@ -17,10 +20,25 @@ type Client struct {
Ftaa string `json:"ftaa"`
Bfaa string `json:"bfaa"`
LastSubmission *SaveSubmission `json:"last_submission"`
Host string `json:"host"`
path string
client *http.Client
}

func formatHost(host string) (string, error) {
if len(host) == 0 {
return "https://codeforces.com", nil
}
reg := regexp.MustCompile(`https?://[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)+/*`)
if !reg.MatchString(host) {
return "", fmt.Errorf(`Invalid host "%v"`, host)
}
for host[len(host)-1:] == "/" {
host = host[:len(host)-1]
}
return host, nil
}

// New client
func New(path string) *Client {
jar, _ := cookiejar.New(nil)
Expand All @@ -29,6 +47,13 @@ func New(path string) *Client {
c.load()
}
c.client = &http.Client{Jar: c.Jar}
var err error
c.Host, err = formatHost(c.Host)
if err != nil {
color.Red(err.Error() + `. Use default host "https://codeforces.com"`)
color.Red(`Please use "cf config" to set a valid host later`)
c.Host = "https://codeforces.com"
}
return c
}

Expand Down Expand Up @@ -60,3 +85,24 @@ func (c *Client) save() (err error) {
}
return
}

// SetHost set host for Codeforces
func (c *Client) SetHost() (err error) {
host, err := formatHost(c.Host)
if err != nil {
host = "https://codeforces.com"
}
color.Green("Current host domain is %v", host)
color.Cyan(`Set a new host domain (e.g. "https://codeforces.com"`)
color.Cyan(`Note: Don't forget the "http://" or "https://"`)
for {
host, err = formatHost(util.ScanlineTrim())
if err == nil {
break
}
color.Red(err.Error())
}
c.Host = host
color.Green("New host domain is %v", host)
return c.save()
}
4 changes: 2 additions & 2 deletions client/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Client) Clone(username, rootPath string, ac bool) (err error) {

jar, _ := cookiejar.New(nil)
if username == c.Username {
resp, err := c.client.Get("https://codeforces.com")
resp, err := c.client.Get(c.Host)
if err != nil {
return err
}
Expand All @@ -42,7 +42,7 @@ func (c *Client) Clone(username, rootPath string, ac bool) (err error) {
jar = c.Jar.Copy()
}

resp, err := c.client.Get(fmt.Sprintf("https://codeforces.com/api/user.status?handle=%v", username))
resp, err := c.client.Get(fmt.Sprintf(c.Host+"/api/user.status?handle=%v", username))
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions client/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Client) Login(username, password string) (err error) {

c.client = &http.Client{Jar: jar}

resp, err := c.client.Get("https://codeforces.com/enter")
resp, err := c.client.Get(c.Host + "/enter")
if err != nil {
return
}
Expand All @@ -69,7 +69,7 @@ func (c *Client) Login(username, password string) (err error) {
ftaa := genFtaa()
bfaa := genBfaa()

resp, err = c.client.PostForm("https://codeforces.com/enter", url.Values{
resp, err = c.client.PostForm(c.Host+"/enter", url.Values{
"csrf_token": {csrf},
"action": {"enter"},
"ftaa": {ftaa},
Expand Down
4 changes: 2 additions & 2 deletions client/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *Client) ParseContestProblem(contestID, problemID, path string) (samples
if err != nil {
return
}
URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problemID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/problem/%v", contestID, problemID), contestID)
samples, err = c.ParseProblem(URL, path)
if err != nil {
return
Expand All @@ -104,7 +104,7 @@ func (c *Client) ParseContest(contestID, rootPath string, race bool) (problems [
}
if race {
for _, problem := range problems {
open.Run(ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problem.ID), contestID))
open.Run(ToGym(fmt.Sprintf(c.Host+"/contest/%v/problem/%v", contestID, problem.ID), contestID))
}
}
wg := sync.WaitGroup{}
Expand Down
4 changes: 2 additions & 2 deletions client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Client) PullCode(contestID, submissionID, path, ext string, rename bool
return "", fmt.Errorf("Exists, skip")
}

URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/submission/%v", contestID, submissionID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/submission/%v", contestID, submissionID), contestID)
client := &http.Client{Jar: c.Jar.Copy()}
resp, err := client.Get(URL)
if err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *Client) PullCode(contestID, submissionID, path, ext string, rename bool
func (c *Client) PullContest(contestID, problemID, rootPath string, ac bool) (err error) {
color.Cyan("Pull code from %v%v, ac: %v", contestID, problemID, ac)

URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/my", contestID), contestID)
submissions, err := c.getSubmissions(URL, -1)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion client/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func findCountdown(body []byte) (int, error) {
func (c *Client) RaceContest(contestID string) (err error) {
color.Cyan(ToGym("Race for contest %v\n", contestID), contestID)

URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/countdown", contestID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/countdown", contestID), contestID)
resp, err := c.client.Get(URL)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion client/statis.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func findProblems(body []byte) ([]StatisInfo, error) {
func (c *Client) StatisContest(contestID string) (problems []StatisInfo, err error) {
color.Cyan(ToGym("Get statis in contest %v\n", contestID), contestID)

URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v", contestID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v", contestID), contestID)
resp, err := c.client.Get(URL)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion client/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func findErrorSource(body []byte) ([]byte, error) {
func (c *Client) SubmitContest(contestID, problemID, langID, source string) (err error) {
color.Cyan("Submit %v %v %v", contestID, problemID, Langs[langID])

URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/submit", contestID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/submit", contestID), contestID)
resp, err := c.client.Get(URL)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion client/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (c *Client) getSubmissions(myURL string, n int) (submissions []Submission,

// WatchSubmission n is the number of submissions
func (c *Client) WatchSubmission(contestID, problemID string, n int, line bool) (submissions []Submission, err error) {
URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID), contestID)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/my", contestID), contestID)
maxWidth := 0
first := true
for {
Expand Down
10 changes: 5 additions & 5 deletions cmd/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func Open(args map[string]interface{}) error {
return err
}
if problemID == contestID {
return open.Run(client.ToGym(fmt.Sprintf("https://codeforces.com/contest/%v", contestID), contestID))
return open.Run(client.ToGym(fmt.Sprintf(client.New(config.SessionPath).Host+"/contest/%v", contestID), contestID))
}
return open.Run(client.ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problemID), contestID))
return open.Run(client.ToGym(fmt.Sprintf(client.New(config.SessionPath).Host+"/contest/%v/problem/%v", contestID, problemID), contestID))
}

// Stand command
Expand All @@ -31,15 +31,15 @@ func Stand(args map[string]interface{}) error {
if err != nil {
return err
}
return open.Run(client.ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/standings", contestID), contestID))
return open.Run(client.ToGym(fmt.Sprintf(client.New(config.SessionPath).Host+"/contest/%v/standings", contestID), contestID))
}

// Sid command
func Sid(args map[string]interface{}) error {
contestID := ""
submissionID := ""
cln := client.New(config.SessionPath)
if args["<submission-id>"] == nil {
cln := client.New(config.SessionPath)
if cln.LastSubmission != nil {
contestID = cln.LastSubmission.ContestID
submissionID = cln.LastSubmission.SubmissionID
Expand All @@ -57,5 +57,5 @@ func Sid(args map[string]interface{}) error {
return err
}
}
return open.Run(client.ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/submission/%v", contestID, submissionID), contestID))
return open.Run(client.ToGym(fmt.Sprintf(cln.Host+"/contest/%v/submission/%v", contestID, submissionID), contestID))
}
17 changes: 10 additions & 7 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ package cmd
import (
"github.com/fatih/color"
ansi "github.com/k0kubun/go-ansi"
"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
"github.com/xalanq/cf-tool/util"
)

// Config command
func Config(args map[string]interface{}) error {
color.Cyan("Configure the tool")
cfg := config.New(config.ConfigPath)
ansi.Println(`0) username and password`)
ansi.Println(`1) add a template`)
ansi.Println(`2) delete a template`)
ansi.Println(`3) set default template`)
ansi.Println(`4) run "cf gen" after "cf parse"`)
index := util.ChooseIndex(5)
ansi.Println(`5) set host domain`)
index := util.ChooseIndex(6)
if index == 0 {
return cfg.Login(config.SessionPath)
return config.New(config.ConfigPath).Login(config.SessionPath)
} else if index == 1 {
return cfg.AddTemplate()
return config.New(config.ConfigPath).AddTemplate()
} else if index == 2 {
return cfg.RemoveTemplate()
return config.New(config.ConfigPath).RemoveTemplate()
} else if index == 3 {
return cfg.SetDefaultTemplate()
return config.New(config.ConfigPath).SetDefaultTemplate()
} else if index == 4 {
return cfg.SetGenAfterParse()
return config.New(config.ConfigPath).SetGenAfterParse()
} else if index == 5 {
return client.New(config.SessionPath).SetHost()
}
return nil
}
10 changes: 6 additions & 4 deletions config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *Config) AddTemplate() (err error) {
color.Red("Invalid index. Please input again")
}

ansi.Println(`Template:
note := `Template:
You can insert some placeholders into your template code. When generate a code from the
template, cf will replace all placeholders by following rules:
Expand All @@ -50,7 +50,8 @@ func (c *Config) AddTemplate() (err error) {
$%D%$ Day (e.g. 09)
$%h%$ Hour (e.g. 08)
$%m%$ Minute (e.g. 05)
$%s%$ Second (e.g. 00)`)
$%s%$ Second (e.g. 00)`
ansi.Println(note)
color.Cyan(`Template absolute path(e.g. "~/template/io.cpp"): `)
path := ""
for {
Expand Down Expand Up @@ -88,7 +89,7 @@ func (c *Config) AddTemplate() (err error) {
}

color.Green("Script in template:")
ansi.Println(`Template will run 3 scripts in sequence when you run "cf test":
note = `Template will run 3 scripts in sequence when you run "cf test":
- before_script (execute once)
- script (execute the number of samples times)
- after_script (execute once)
Expand All @@ -101,7 +102,8 @@ func (c *Config) AddTemplate() (err error) {
$%path%$ Path to source file (Excluding $%full%$, e.g. "/home/xalanq/")
$%full%$ Full name of source file (e.g. "a.cpp")
$%file%$ Name of source file (Excluding suffix, e.g. "a")
$%rand%$ Random string with 8 character (including "a-z" "0-9")`)
$%rand%$ Random string with 8 character (including "a-z" "0-9")`
ansi.Println(note)

color.Cyan(`Before script (e.g. "g++ $%full%$ -o $%file%$.exe -std=c++11"), empty is ok: `)
beforeScript := util.ScanlineTrim()
Expand Down

0 comments on commit 8832afe

Please sign in to comment.