-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (132 loc) · 3.38 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
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
package main
import (
"encoding/csv"
"fmt"
"log"
"math/rand"
"os"
"time"
"github.com/gocolly/colly/v2"
)
func scrape_data(dir string) []string {
c := colly.NewCollector(
colly.AllowedDomains(
"https://codeforces.com",
"https://codeforces.com/problemset",
"http://codeforces.com/problemset",
"codeforces.com/problemset",
"http://codeforces.com/",
"codeforces.com",
),
)
links := make([]string, 0)
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, err error) {
fmt.Println("Something went wrong:", err)
return
})
c.OnResponse(func(r *colly.Response) {
})
c.OnHTML(".id", func(e *colly.HTMLElement) {
l := e.ChildAttrs("a", "href")
links = append(links, l[0])
})
page := 1
for page <= 79 {
link := fmt.Sprint("https://codeforces.com/problemset/page/", page)
c.Visit(link)
page += 1
}
save_to_csv(links, dir)
return links
}
func save_to_csv(data []string, dir string) (fName string) {
fName = "problem_set.csv"
err := os.MkdirAll(dir+"\\data\\", os.ModePerm)
if err != nil {
log.Fatalf("Cannot create directory %q: %s\n", dir+"\\data\\", err)
return
}
file, err := os.Create(dir + "\\data\\" + fName)
if err != nil {
log.Fatalf("Cannot create file %q: %s\n", fName, err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, link := range data {
l := []string{link}
writer.Write(l)
}
fmt.Println("Successfully saved scraped data to", fName)
return fName
}
func get_random_problem(data []string, c string) string {
rand.Seed(time.Now().UnixNano())
r := rand.Intn(len(data))
guess := data[r]
if c == "any" {
return "https://codeforces.com" + guess
}
if StringInSlice(c[0], []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}) {
if c[0] == guess[len(guess)-1] {
return "https://codeforces.com" + guess
} else {
return get_random_problem(data, c)
}
} else {
return "Please try fixing the task complexity argument when executing a binary.\nEnter valid -c parameter\n\nRun --help for more info"
}
}
func get_data_from_file(filename string, dir string) (data []string) {
f, err := os.Open(dir + "\\data\\" + filename)
if err != nil {
log.Printf("Cannot open file %q: %s\n", filename, err)
log.Fatalf("Please use --scrape argument to scrape the problem set first.")
}
defer f.Close()
csv_reader := csv.NewReader(f)
lines, err := csv_reader.ReadAll()
if err != nil {
log.Fatalf("Cannot read file %q: %s\n", filename, err)
}
data = make([]string, 0)
for _, line := range lines {
data = append(data, line[0])
}
return data
}
func main() {
dir := Get_run_directory()
args := ReadArgs()
ARG_SCRAPE := args.GetArg("--scrape")
ARG_FILE := args.GetArg("-p")
ARG_COMPLEXITY := args.GetArg("-c")
ARG_BROWSER := args.GetArg("-d")
ARG_HELP_ONE := args.GetArg("--help")
ARG_HELP_TWO := args.GetArg("--help")
if ARG_HELP_ONE.exists || ARG_HELP_TWO.exists {
GetHelp()
}
var data []string
if ARG_SCRAPE.exists {
data = scrape_data(dir)
} else {
if ARG_FILE.exists {
data = get_data_from_file(ARG_FILE.value, dir)
} else {
ARG_FILE.value = "problem_set.csv"
data = get_data_from_file(ARG_FILE.value, dir)
}
}
var link string
if ARG_COMPLEXITY.exists {
link = get_random_problem(data, ARG_COMPLEXITY.value)
} else {
link = get_random_problem(data, "any")
}
Print_result(link, ARG_BROWSER.exists)
}