-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (84 loc) · 2.15 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"log"
"os"
"strings"
)
const tgpath = "/training/7/chapter/46"
func processAll() {
log.Println("dowloading chapter list at", tgpath)
body := getBody(tgpath)
list := getList(body, "chapter-link")
for _, path := range list {
log.Println("processing chapter at", path)
processChapter(path)
}
}
func processChapter(path string) {
body := getBody(path)
title := getTitle(body)
log.Println("title =", title)
list := getList(body, "chapter-link")
for _, path := range list {
log.Println("processing subchapter at", path)
processSubChapter(path)
}
}
func processSubChapter(path string) {
body := getBody(path)
title := getTitle(body)
log.Println("title =", title)
dirname := escape(title)
cwd := getCurrentDir()
makeDirp(dirname)
chdir(dirname)
repl := strings.NewReplacer("problem", "submission")
exist := false
// TODO: check another page, maybe ?page=x with x > 2. But how!
list := getList(body, "problem-link")
if _, ok := grep(body, `class="next"`, false); ok {
p2body := getBody(path + "?page=2")
p2list := getList(p2body, "problem-link")
list = append(list, p2list...)
}
for _, path := range list {
submpath := repl.Replace(path)
log.Println("processing problem submission at", submpath)
ac := processSubmission(submpath)
if !ac {
log.Println("no accepted submission")
}
exist = exist || ac
}
chdir(cwd)
if !exist {
remove(dirname)
}
}
func processSubmission(path string) bool {
body := getBody(path)
submpath, ok := getSubmissionPath(body)
if !ok {
return false
}
log.Println("submission path =", submpath)
submbody := getBody(submpath)
name := getProp(submbody, "Soal", `^.*<a [^>]+> *([^<]+) *</a>.*$`)
lang := getProp(submbody, "Bahasa Pemrograman", `^.*<span> *([^<]+) *</span>.*$`)
dlpath := submpath + "?action=download"
filename := escape(name) + "." + lang
log.Printf("downloading %s (%s)\n", name, filename)
dlresp := get(dlpath)
defer dlresp.Body.Close()
writeToFile(dlresp.Body, filename)
return true
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [PHPSESSID]\n", os.Args[0])
os.Exit(1)
}
phpsessid = os.Args[1]
processAll()
}