-
Notifications
You must be signed in to change notification settings - Fork 3
/
crawler.go
255 lines (240 loc) · 6.13 KB
/
crawler.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"encoding/gob"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/publicsuffix"
)
const baseURL = "https://onlinejudge.org"
var uvaURL, _ = url.Parse(baseURL)
type problemInfo struct {
Title string
ID int
TrueID int
TotalSubmissions int
Percentage float32
}
func crawlProblemsInfo() map[int]problemInfo {
// First, get all volumes' URL from two categories - "Problem Set Volumes" and "Contest Volumes".
volumesChan := make(chan string)
var volumesWaitGroup sync.WaitGroup
getVolumes := func(category int) {
defer func() {
if err := recover(); err != nil {
cprintf(red, 0, "%s\n", err)
os.Exit(1)
}
}()
resp, err := http.Get(fmt.Sprintf("%s/index.php?option=com_onlinejudge&Itemid=8&category=%d", baseURL, category))
if err != nil {
panic(err)
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
panic(err)
}
doc.Find("#col3_content_wrapper > table:nth-child(4) > tbody > tr > td > a").
Each(func(i int, s *goquery.Selection) {
href, ok := s.Attr("href")
if !ok {
panic("href not exists")
}
volumesChan <- href
})
volumesWaitGroup.Done()
}
volumesWaitGroup.Add(2)
// Problem Set Volumes (100...1999)
go getVolumes(1)
// Contest Volumes (10000...)
go getVolumes(2)
go func() {
volumesWaitGroup.Wait()
close(volumesChan)
}()
// Second, get all problems' information from each volume.
problemsChan := make(chan problemInfo)
var problemsWaitGroup sync.WaitGroup
// \s does not match
titleRegex := regexp.MustCompile("(\\d+)\u00A0-\u00A0(.+)")
trueIDRegex := regexp.MustCompile(`.+problem=(\d+)`)
getProblems := func() {
defer func() {
if err := recover(); err != nil {
cprintf(red, 0, "%s\n", err)
os.Exit(1)
}
}()
for volumeURL := range volumesChan {
resp, err := http.Get(fmt.Sprintf("%s/%s", baseURL, volumeURL))
if err != nil {
panic(err)
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
panic(err)
}
doc.Find("#col3_content_wrapper > table:nth-child(4) > tbody > tr[class!=sectiontableheader]").
Each(func(i int, s *goquery.Selection) {
var problem problemInfo
ele := s.Find("td:nth-child(3) > a")
match := titleRegex.FindStringSubmatch(ele.Text())[1:]
problem.ID, _ = strconv.Atoi(match[0])
problem.Title = string(match[1])
href, ok := ele.Attr("href")
if !ok {
panic("href not exists")
}
problem.TrueID, _ = strconv.Atoi(trueIDRegex.FindStringSubmatch(href)[1])
problem.TotalSubmissions, _ = strconv.Atoi(s.Find("td:nth-child(4)").Text())
text := s.Find("td:nth-child(5) > div > div:nth-child(2)").Text()
p, _ := strconv.ParseFloat(text[:len(text)-1], 32)
problem.Percentage = float32(p)
problemsChan <- problem
})
}
problemsWaitGroup.Done()
}
const WORKERS = 8
problemsWaitGroup.Add(WORKERS)
for i := 0; i < WORKERS; i++ {
go getProblems()
}
go func() {
problemsWaitGroup.Wait()
close(problemsChan)
}()
// Finally, collect all the problems.
problems := make(map[int]problemInfo)
defer spin("Downloading problem list")()
for p := range problemsChan {
problems[p.ID] = p
}
return problems
}
func crawlTestData(pid int) (input string, output string) {
defer spin("Downloading test cases")()
problemHomePage := fmt.Sprintf("https://www.udebug.com/UVa/%d", pid)
doc, err := goquery.NewDocument(problemHomePage)
if err != nil {
panic(err)
}
sel := doc.Find("a.input_desc")
// some problems has no input
if sel.Length() != 0 {
inputID, ok := sel.Attr("data-id")
if !ok {
panic("no input found")
}
resp, err := http.PostForm(
"https://www.udebug.com/udebug-custom-get-selected-input-ajax",
url.Values{"input_nid": {inputID}},
)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var m map[string]string
if err := json.Unmarshal(data, &m); err != nil {
panic(err)
}
input = m["input_value"]
}
form := url.Values{}
doc.Find("#udebug-custom-problem-view-input-output-form input").Each(func(i int, s *goquery.Selection) {
form.Set(s.AttrOr("name", ""), s.AttrOr("value", ""))
})
if input != "" {
form.Set("input_data", input)
}
resp, err := http.PostForm(problemHomePage, form)
if err != nil {
panic(err)
}
doc, err = goquery.NewDocumentFromResponse(resp)
if err != nil {
panic(err)
}
output = doc.Find("#edit-output-data").Text()
return
}
type loginInfo struct {
// Export these fields so that gob can dump them.
Username string
Cookies []*http.Cookie
}
func login() (username string) {
fmt.Print("Username: ")
fmt.Scanln(&username)
fmt.Print("Password: ")
password, err := terminal.ReadPassword(0)
fmt.Print("\n")
if err != nil {
panic(err)
}
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
panic(err)
}
http.DefaultClient.Jar = jar
defer spin("Signing in onlinejudge.org (UVa)")()
resp, err := http.Get(baseURL)
if err != nil {
panic(err)
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
panic(err)
}
form := url.Values{}
doc.Find("#mod_loginform > table > tbody > tr:nth-child(1) > td > input").
Each(func(i int, s *goquery.Selection) {
name, _ := s.Attr("name")
value := s.AttrOr("value", "")
form.Set(name, value)
})
form.Set("username", username)
form.Set("passwd", string(password))
r, err := http.PostForm(
baseURL+"/index.php?option=com_comprofiler&task=login", form)
if err != nil {
panic(err)
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
const failed = "Incorrect username or password"
if strings.Contains(string(body), failed) {
panic(failed)
}
f, err := os.Create(loginInfoFile)
if err != nil {
panic(err)
}
defer f.Close()
info := loginInfo{
Username: username,
Cookies: http.DefaultClient.Jar.Cookies(uvaURL),
}
if err := gob.NewEncoder(f).Encode(info); err != nil {
panic(err)
}
return
}