Skip to content

Commit

Permalink
feat: support gym
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanna-Neko committed Dec 9, 2022
1 parent f6f8ea6 commit 99cc1f7
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var envSet = map[string]bool{
"proxy": true,
"lang": true,
}
var envSlice = []string{"handle", "password", "rating", "problem", "race", "proxy"}
var envSlice = []string{"handle", "password", "rating", "problem", "race", "proxy", "apikey", "secret"}

func init() {
env.PersistentFlags().BoolVarP(&modify, "write", "w", false, `you can modify env like "env -w proxy=http://127.0.0.1:20245" when you use flag -w or --write`)
Expand Down
27 changes: 26 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"time"

"github.com/briandowns/spinner"
Expand Down Expand Up @@ -58,6 +59,7 @@ const testFilesDir = "./codeforces/testFiles/"

func init() {
rootCmd.AddCommand(InitCmd)
InitCmd.AddCommand(InitApikeyCmd)
}

var InitCmd = &cobra.Command{
Expand All @@ -68,6 +70,29 @@ var InitCmd = &cobra.Command{
updateFunc()
},
}
var InitApikeyCmd = &cobra.Command{
Use: "apikey",
Short: "init apikey and secret",
PreRun: func(cmd *cobra.Command, args []string) {
ReadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("please type the apikey")
var apikey string
fmt.Scanf("%s", &apikey)
fmt.Println("please type secret")
var secret string
fmt.Scanf("%s", &secret)
apikey = strings.Trim(apikey, " ")
secret = strings.Trim(secret, " ")
viper.Set("apikey", apikey)
viper.Set("secret", secret)
err := viper.WriteConfig()
if err != nil {
log.Fatal(err)
}
},
}

func configFunc() {
if !checkConfigFile() {
Expand Down Expand Up @@ -144,7 +169,7 @@ func initContestTemplate() {
ProblemConditions: div2Diffculty,
}},
}
data, err := json.MarshalIndent(template,""," ")
data, err := json.MarshalIndent(template, "", " ")
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ var problemCmd = &cobra.Command{
}
}
contest, index := splitProblem(problemInfo)
OpenWebsite(codeforcesDomain + "/contest/" + contest + "/problem/" + index)
if isGym(contest) {
OpenWebsite(codeforcesDomain + "/gym/" + contest + "/problem/" + index)
} else {
OpenWebsite(codeforcesDomain + "/contest/" + contest + "/problem/" + index)
}
GetTestcases(problemInfo)
},
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ var raceCmd = &cobra.Command{
fmt.Println("testcases download complete")
},
}

func isGym(contest string) bool {
return len(contest) >= 6
}
15 changes: 12 additions & 3 deletions cmd/stand.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ var standCmd = &cobra.Command{
log.Fatal("please use race command to specify a contest first")
}
if standAll {
OpenWebsite(fmt.Sprintf("https://codeforces.com/contest/%s/standings", contest))
}else {
OpenWebsite(fmt.Sprintf("https://codeforces.com/contest/%s/standings/friends/true", contest))
if isGym(contest) {
OpenWebsite(fmt.Sprintf("https://codeforces.com/gym/%s/standings", contest))

} else {
OpenWebsite(fmt.Sprintf("https://codeforces.com/contest/%s/standings", contest))
}
} else {
if isGym(contest) {
OpenWebsite(fmt.Sprintf("https://codeforces.com/gym/%s/standings/friends/true", contest))
} else {
OpenWebsite(fmt.Sprintf("https://codeforces.com/contest/%s/standings/friends/true", contest))
}
}
},
}
6 changes: 5 additions & 1 deletion cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ var submitCommand = &cobra.Command{
log.Fatal("don't support language: " + language)
}
link.SubmitCode(contest, index, code, lan.ProgramTypeId)
OpenWebsite(codeforcesDomain + "/contest/" + contest + "/my")
if isGym(contest) {
OpenWebsite(codeforcesDomain + "/gym/" + contest + "/my")
}else {
OpenWebsite(codeforcesDomain + "/contest/" + contest + "/my")
}
},
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

const VERSION = "v1.2.8"
const VERSION = "v1.2.9"

func init() {
rootCmd.AddCommand(versionCmd)
Expand Down
30 changes: 28 additions & 2 deletions link/contest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package link

import (
"bytes"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -13,7 +15,9 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/briandowns/spinner"
"github.com/go-resty/resty/v2"
"github.com/jaxleof/uispinner"
"github.com/spf13/viper"
)

func CloneContest(title string, id string, duration string) {
Expand Down Expand Up @@ -116,7 +120,23 @@ func CreateContest(title string, duration string, problems []string) {

func GetContestInfo(contestId string) ContestStandingResult {
var res ContestStandingInterface
response, err := me.R().Get("https://codeforces.com/api/contest.standings?contestId=" + contestId + "&from=1&handles=jaxleof&showUnofficial=true")
var response *resty.Response
var err error
if isGym(contestId) {
apikey := viper.GetString("apikey")
secret := viper.GetString("secret")
if apikey == "" || secret == "" {
log.Fatal("please use init apikey command first")
}
tim := time.Now().Unix()
url := "contest.standings?apiKey=%s&contestId=%s&count=5&from=1&showUnofficial=true&time=%d"
data := "987654/" + fmt.Sprintf(url, apikey,contestId, tim) + "#" + secret
hash := sha512.Sum512([]byte(data))
path := "https://codeforces.com/api/" + fmt.Sprintf(url, apikey,contestId, tim) + "&apiSig=987654" + hex.EncodeToString(hash[:])
response, err = me.R().Get(path)
} else {
response, err = me.R().Get("https://codeforces.com/api/contest.standings?contestId=" + contestId + "&from=1&handles=jaxleof&showUnofficial=true")
}
if err != nil {
log.Fatal(err)
}
Expand All @@ -129,7 +149,13 @@ func GetContestInfo(contestId string) ContestStandingResult {

func GetContestCountdown(contestId string) int {

res, err := me.R().Get(fmt.Sprintf("https://codeforces.com/contest/%s/countdown", contestId))
var res *resty.Response
var err error
if isGym(contestId) {
res, err = me.R().Get(fmt.Sprintf("https://codeforces.com/gym/%s/countdown", contestId))
} else {
res, err = me.R().Get(fmt.Sprintf("https://codeforces.com/contest/%s/countdown", contestId))
}
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 7 additions & 1 deletion link/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func SubmitCode(contest string, index string, code []byte, programTypeId string)
sp.Prefix = "AC!!! "
sp.Start()
sp.FinalMSG = "submission complete\n"
var path string
if isGym(contest) {
path = fmt.Sprintf(codeforcesDomain+"/gym/%s/submit?csrf_token=%s", contest, csrf)
} else {
path = fmt.Sprintf(codeforcesDomain+"/contest/%s/submit?csrf_token=%s", contest, csrf)
}
res, err := me.R().SetFormData(map[string]string{
"csrf_token": csrf,
"action": "submitSolutionFormSubmitted",
Expand All @@ -24,7 +30,7 @@ func SubmitCode(contest string, index string, code []byte, programTypeId string)
"programTypeId": programTypeId,
"source": string(code),
"tabSize": "4",
}).Post(fmt.Sprintf(codeforcesDomain+"/contest/%s/submit?csrf_token=%s", contest, csrf))
}).Post(path)
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 12 additions & 1 deletion link/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import (
"log"

"github.com/PuerkitoBio/goquery"
"github.com/go-resty/resty/v2"
)

func GetSample(contest string, index string) ([]string, []string) {
setProxy()
res, err := me.R().SetDoNotParseResponse(true).Get(codeforcesDomain + "/contest/" + contest + "/problem/" + index)
var res *resty.Response
var err error
if !isGym(contest) {
res, err = me.R().SetDoNotParseResponse(true).Get(codeforcesDomain + "/contest/" + contest + "/problem/" + index)
} else {
res, err = me.R().SetDoNotParseResponse(true).Get(codeforcesDomain + "/gym/" + contest + "/problem/" + index)
}
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -44,3 +51,7 @@ func GetSample(contest string, index string) ([]string, []string) {
})
return input, output
}

func isGym(contest string) bool {
return len(contest) >= 6
}

0 comments on commit 99cc1f7

Please sign in to comment.