Skip to content

Commit 79a71b7

Browse files
committed
customTemplate and languageSupport:java,cpp,python,javascript
1 parent f3091c1 commit 79a71b7

File tree

7 files changed

+129
-84
lines changed

7 files changed

+129
-84
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
api.sh
2-
*.cpp
2+
*.task.*
33
notes.txt
4+
template.*
5+
6+
project/*.task.*
7+
project/template.*
8+
project/cses-cli
9+
project/pkg
10+
project/src
411

512
# Binaries for programs and plugins
613
*.exe

make_release.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ then
66
RELEASE_VERSION="latest"
77
fi
88

9-
CDIR=`pwd`
10-
RDIR="${CDIR}/release/${RELEASE_VERSION}"
9+
RDIR="$(pwd)/release/${RELEASE_VERSION}"
1110

1211
mkdir -p "$RDIR"
1312

13+
cd project
14+
CDIR=`pwd`
15+
1416
for goarch in ""amd64 386""; do
1517
for goos in ""linux windows darwin""; do
1618
NAME="${PROJECT_NAME}_${RELEASE_VERSION}_${goos}_${goarch}"
1719
GOPATH="${CDIR}" CGO_ENABLED=0 GOOS=${goos} GOARCH=${goarch} go build -o ${RDIR}/${NAME}
1820
echo ${NAME}
1921
done
2022
done
23+
cd ..
File renamed without changes.
File renamed without changes.

helper.go renamed to project/helper.go

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package main
22

33
import (
44
"bufio"
5-
"bytes"
65
"encoding/json"
76
"fmt"
8-
"io"
97
"io/ioutil"
10-
"mime/multipart"
11-
"net/http"
128
"os"
139
"os/exec"
1410
"path/filepath"
@@ -40,7 +36,7 @@ func UserHomeDir() string {
4036
func updateConfig(sess *Session) {
4137
out, err := json.MarshalIndent(sess, "", " ")
4238
check(err)
43-
cacheSet("login.json", string(out), sess.Root)
39+
cacheSet("config.json", string(out), sess.Root)
4440
}
4541

4642
func updateIfNew(scanner *bufio.Scanner, src *string, text string) {
@@ -79,6 +75,14 @@ func cacheGet(filename string, root string) ([]byte, bool) {
7975
return content, true
8076
}
8177

78+
func getTemplate(ext string) string {
79+
content, err := ioutil.ReadFile("template"+ext)
80+
if err != nil {
81+
return ""
82+
}
83+
return string(content)
84+
}
85+
8286
func getTaskFromCache(task string, root string) string {
8387
path := filepath.Join(root, task+".task.html")
8488

@@ -115,41 +119,23 @@ func writeCodeFile(filename string, text string, template string) bool {
115119
return true
116120
}
117121

118-
func newfileUploadRequest(uri string, params map[string]string, paramName, path string, cookie string) (*http.Request, error) {
119-
file, err := os.Open(path)
120-
if err != nil {
121-
return nil, err
122-
}
123-
defer file.Close()
124-
125-
body := &bytes.Buffer{}
126-
writer := multipart.NewWriter(body)
127-
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
128-
if err != nil {
129-
return nil, err
130-
}
131-
_, err = io.Copy(part, file)
132-
133-
for key, val := range params {
134-
_ = writer.WriteField(key, val)
135-
}
136-
err = writer.Close()
137-
if err != nil {
138-
return nil, err
139-
}
140-
141-
return newfileUploadRequestPost(uri, body, cookie, writer.FormDataContentType())
122+
var extLangMap = map[string]string{
123+
".java": "Java",
124+
".js": "Node.js",
125+
".py": "Python3",
126+
".cpp": "C++",
142127
}
143-
144-
func fileMeta(filename string) (string, string, string, bool) {
145-
parts := strings.Split(filepath.Base(filename), ".")
146-
lang := parts[2]
147-
option := ""
148-
if lang == "cpp" {
149-
lang = "C++"
150-
option = "C++17"
151-
}
152-
return parts[0], lang, option, true
128+
var extOptionMap = map[string]string{
129+
".java": "",
130+
".js": "",
131+
".py": "CPython3",
132+
".cpp": "C++17",
133+
}
134+
var langExtMap = map[string]string {
135+
"java": ".java",
136+
"javascript": ".js",
137+
"python": ".py",
138+
"cpp": ".cpp",
153139
}
154140

155141
var unicodeMap = map[string]string{

main.go renamed to project/main.go

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@ type Session struct {
3838
Cookie string `json:"cookie"`
3939
Root string `json:"root"`
4040
Editor string `json:"editor"`
41+
Lang string `json:"lang"`
4142
Github githubConfig `json:"github"`
4243
}
4344

44-
var cpptemplate = `
45-
#include<bits/stdc++.h>
46-
using namespace std;
45+
// var cpptemplate = `
46+
// #include<bits/stdc++.h>
47+
// using namespace std;
4748

48-
int main(){
49+
// int main(){
4950

50-
return 0;
51-
}
52-
`
51+
// return 0;
52+
// }
53+
// `
5354

5455
func initSess(sess *Session) bool {
55-
sess.Root = filepath.Join(UserHomeDir(), ".cses")
5656
os.MkdirAll(sess.Root, os.ModePerm)
5757

58-
out, ok := cacheGet("login.json", sess.Root)
58+
out, ok := cacheGet("config.json", sess.Root)
5959
if !ok {
6060
return false
6161
}
@@ -145,7 +145,7 @@ func printResult(link string, sess *Session) bool {
145145
status, text, verdict := printResultRequest(link, sess.Cookie)
146146
s.Prefix = status + " "
147147

148-
if status == "READY" || status == "" {
148+
if status == "READY" || status == "COMPILE ERROR" || status == "" {
149149
fmt.Print("\n" + text)
150150
if verdict == "ACCEPTED" {
151151
return true
@@ -156,31 +156,28 @@ func printResult(link string, sess *Session) bool {
156156
return false
157157
}
158158

159-
func submit(filename string, sess *Session) {
159+
func submit(sourceFile string, sess *Session) {
160160

161-
task, lang, option, exist := fileMeta(filename)
162-
if !exist {
163-
fmt.Println("File doesn't exist")
164-
return
165-
}
161+
ext := filepath.Ext(sourceFile)
162+
task := strings.Split(filepath.Base(sourceFile), ".")[0]
166163

167164
opts := map[string]string{
168165
"csrf_token": sess.Csrf,
169166
"task": task,
170-
"lang": lang,
167+
"lang": extLangMap[ext],
171168
"type": "course",
172169
"target": "problemset",
173-
"option": option,
170+
"option": extOptionMap[ext],
174171
}
175172

176-
link := submitRequest(opts, filename, sess.Cookie)
173+
link := submitRequest(opts, sourceFile, sess.Cookie)
177174

178175
if verdict := printResult(link, sess); verdict && validGithubConfig(&sess.Github) {
179176
s := spinner.New(spinner.CharSets[36], 100*time.Millisecond)
180177
s.Prefix = "Comitting to Github"
181178
s.Start()
182179
defer s.Stop()
183-
if ok := updateFile(filename, &sess.Github); ok {
180+
if ok := updateFile(sourceFile, &sess.Github); ok {
184181
fmt.Println("✔")
185182
} else {
186183
fmt.Println("✘")
@@ -226,9 +223,10 @@ func solve(task string, sess *Session) {
226223
fmt.Println("Task Doesn't Exist")
227224
}
228225

229-
filename := task + ".task.cpp"
226+
filename := task + ".task" + langExtMap[sess.Lang]
227+
template := getTemplate(langExtMap[sess.Lang])
230228

231-
writeCodeFile(filename, text, cpptemplate)
229+
writeCodeFile(filename, text, template)
232230

233231
if sess.Editor == "" {
234232
scanner := bufio.NewScanner(os.Stdin)
@@ -255,19 +253,36 @@ func configureGithub(sess *Session) {
255253
updateConfig(sess)
256254
}
257255

258-
func stat(task string) {
259-
fmt.Println("#Todo")
256+
func showHelp(){
257+
fmt.Println("Usage:")
258+
259+
fmt.Println("\tcses-cli login")
260+
fmt.Println("\tcses-cli list")
261+
fmt.Println("\tcses-cli show 1068")
262+
fmt.Println("\tcses-cli solve 1068")
263+
fmt.Println("\tcses-cli submit 1068.task.cpp")
264+
265+
fmt.Println("Optional:")
266+
fmt.Println("\tcses-cli github")
260267
}
261268

269+
// func stat(task string) {
270+
// fmt.Println("#Todo")
271+
// }
272+
262273
func main() {
263274

264275
flag.Parse()
265276

266277
if flag.NArg() == 0 {
267-
os.Exit(1)
278+
showHelp()
279+
return
268280
}
269281

270-
sess := &Session{}
282+
sess := &Session{
283+
Lang: "cpp",
284+
Root: filepath.Join(UserHomeDir(), ".cses"),
285+
}
271286

272287
isLogged := initSess(sess)
273288

@@ -303,11 +318,11 @@ func main() {
303318
case "github":
304319
configureGithub(sess)
305320

306-
case "stat":
307-
fmt.Println("sw stat 1068")
308-
stat("stat 1068")
309-
321+
// case "stat":
322+
// fmt.Println("sw stat 1068")
323+
// stat("stat 1068")
324+
case "help":
310325
default:
311-
fmt.Println("sw default")
326+
showHelp()
312327
}
313328
}

requests.go renamed to project/requests.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import (
55
"io"
66
"net/http"
77
"strings"
8+
"mime/multipart"
9+
"os"
10+
"fmt"
11+
"path/filepath"
812

913
"github.com/PuerkitoBio/goquery"
1014
)
1115

12-
// func printResp(resp *http.Response) {
13-
// body := &bytes.Buffer{}
14-
// _, err := body.ReadFrom(resp.Body)
15-
// if err != nil {
16-
// log.Fatal(err)
17-
// }
18-
// fmt.Println(body)
19-
// fmt.Println(resp.Header)
20-
// fmt.Println(resp.StatusCode)
21-
// }
16+
func printResp(resp *http.Response) {
17+
body := &bytes.Buffer{}
18+
_, err := body.ReadFrom(resp.Body)
19+
check(err)
20+
21+
fmt.Println(body)
22+
fmt.Println(resp.Header)
23+
fmt.Println(resp.StatusCode)
24+
}
2225

2326
func newfileUploadRequestPost(uri string, body *bytes.Buffer, cookie string, contentType string) (*http.Request, error) {
2427

@@ -29,13 +32,44 @@ func newfileUploadRequestPost(uri string, body *bytes.Buffer, cookie string, con
2932
return req, err
3033
}
3134

35+
func newfileUploadRequest(uri string, params map[string]string, path string, cookie string) (*http.Request, error) {
36+
file, err := os.Open(path)
37+
if err != nil {
38+
return nil, err
39+
}
40+
defer file.Close()
41+
42+
body := &bytes.Buffer{}
43+
writer := multipart.NewWriter(body)
44+
filename := filepath.Base(path)
45+
if filepath.Ext(path) == ".java" {
46+
filename = "Solution.java"
47+
}
48+
part, err := writer.CreateFormFile("file", filename)
49+
if err != nil {
50+
return nil, err
51+
}
52+
_, err = io.Copy(part, file)
53+
54+
for key, val := range params {
55+
_ = writer.WriteField(key, val)
56+
}
57+
err = writer.Close()
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
return newfileUploadRequestPost(uri, body, cookie, writer.FormDataContentType())
63+
}
64+
3265
func submitRequest(opts map[string]string, filename string, cookie string) string {
33-
request, err := newfileUploadRequest("https://cses.fi/course/send.php", opts, "file", filename, cookie)
66+
request, err := newfileUploadRequest("https://cses.fi/course/send.php", opts, filename, cookie)
3467
check(err)
3568

3669
client := &http.Client{}
3770
resp, err := client.Do(request)
3871
check(err)
72+
3973
defer resp.Body.Close()
4074

4175
doc, err := goquery.NewDocumentFromReader(resp.Body)

0 commit comments

Comments
 (0)