-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
109 lines (83 loc) · 1.96 KB
/
config.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
package main
import (
"encoding/json"
"fmt"
"github.com/gocolly/colly"
"io/ioutil"
"strings"
)
func GenConfig(title, form string) Config {
c := colly.NewCollector(
//colly.AllowedDomains("https://docs.google.com"),
)
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnResponse(func(r *colly.Response) {
fmt.Println(r.StatusCode)
})
var config = Config{
Title: title,
Form: form,
}
c.OnHTML(".freebirdFormviewerComponentsQuestionBaseRoot", func(e *colly.HTMLElement) {
var question Question
e.ForEach(".freebirdFormviewerComponentsQuestionBaseHeader", func(_ int, e *colly.HTMLElement) {
question.Label = e.Text
//fmt.Println(e.Text)
})
e.ForEach("input", func(_ int, e *colly.HTMLElement) {
question.PostKey = strings.ReplaceAll(e.Attr("name"), "_sentinel", "")
//fmt.Println(e.Attr("name"))
})
e.ForEach(".docssharedWizToggleLabeledLabelText", func(_ int, e *colly.HTMLElement) {
var option = Option{
Value: e.Text,
Chance: 1,
}
question.Options = append(question.Options, option)
//fmt.Println(e.Text)
})
config.Questions = append(config.Questions, question)
})
err := c.Visit(form + "/viewform")
if err != nil {
panic(err)
}
SaveConfig(config)
return config
}
func SaveConfig(config Config) {
file, _ := json.MarshalIndent(config, "", " ")
_ = ioutil.WriteFile("configs/"+config.Title+".json", file, 0644)
}
func ReadConfigGen() (string, string) {
data, err := ioutil.ReadFile("configGen.json")
if err != nil {
panic(err)
}
type Info struct {
Title string
Form string
}
// json data
var obj Info
// unmarshall it
err = json.Unmarshal(data, &obj)
if err != nil {
panic(err)
}
return obj.Title, obj.Form
}
func ReadConfig(configName string) Config {
data, err := ioutil.ReadFile("configs/" + configName + ".json")
if err != nil {
panic(err)
}
var config Config
err = json.Unmarshal(data, &config)
if err != nil {
panic(err)
}
return config
}