-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
103 lines (96 loc) · 2.08 KB
/
cache.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
package main
import (
"encoding/gob"
"net/http"
"net/http/cookiejar"
"os"
"golang.org/x/net/publicsuffix"
)
var (
dataPath = os.Getenv("HOME") + "/.local/share/uva-cli/"
pdfPath = dataPath + "pdf/"
testDataPath = dataPath + "test-data/"
loginInfoFile = dataPath + "login-info.gob"
problemsInfoFile = dataPath + "problems-info.gob"
)
func getProblemInfo(pid int) problemInfo {
var problems map[int]problemInfo
if exists(problemsInfoFile) {
f, err := os.Open(problemsInfoFile)
if err != nil {
panic(err)
}
defer f.Close()
if err := gob.NewDecoder(f).Decode(&problems); err != nil {
panic(err)
}
} else {
problems = crawlProblemsInfo()
f, err := os.Create(problemsInfoFile)
if err != nil {
panic(err)
}
defer f.Close()
if err := gob.NewEncoder(f).Encode(problems); err != nil {
panic(err)
}
}
r, ok := problems[pid]
if !ok {
panic("problem not found")
}
return r
}
func getTestData(pid int) (input string, output string) {
testDataFile := testDataPath + getProblemInfo(pid).getFileName("gob")
if exists(testDataFile) {
f, err := os.Open(testDataFile)
if err != nil {
panic(err)
}
defer f.Close()
dec := gob.NewDecoder(f)
if err = dec.Decode(&input); err != nil {
panic(err)
}
if err = dec.Decode(&output); err != nil {
panic(err)
}
} else {
input, output = crawlTestData(pid)
f, err := os.Create(testDataFile)
if err != nil {
panic(err)
}
defer f.Close()
enc := gob.NewEncoder(f)
if err = enc.Encode(input); err != nil {
panic(err)
}
if err = enc.Encode(output); err != nil {
panic(err)
}
}
return
}
func loadLoginInfo() loginInfo {
if !exists(loginInfoFile) {
panic("you are not logged in yet")
}
f, err := os.Open(loginInfoFile)
if err != nil {
panic(err)
}
defer f.Close()
var info loginInfo
if err := gob.NewDecoder(f).Decode(&info); err != nil {
panic(err)
}
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
panic(err)
}
jar.SetCookies(uvaURL, info.Cookies)
http.DefaultClient.Jar = jar
return info
}