-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (143 loc) · 3.89 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
167
168
169
170
171
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"regexp"
"time"
md "github.com/JohannesKaufmann/html-to-markdown"
mdPlugin "github.com/JohannesKaufmann/html-to-markdown/plugin"
colly "github.com/gocolly/colly/v2"
collyQueue "github.com/gocolly/colly/v2/queue"
sf "github.com/simpleforce/simpleforce"
)
var (
sfURL = os.Getenv("sfURL")
sfAPIVersion = "56.0"
sfUser = os.Getenv("sfUser")
sfPassword = os.Getenv("sfPass")
sfToken = os.Getenv("sfToken")
sfIdsRequest = os.Getenv("sfIdsRequest")
kbPath = "./website/docs/kbs/"
logger *log.Logger
)
// Article skeleton
type Article struct {
Id string
Title string
Url string
Content string
}
// Return simpleforce authenticated client
func createSfClient() *sf.Client {
client := sf.NewClient(sfURL, sf.DefaultClientID, sfAPIVersion)
if client == nil {
logger.Fatal("error creating salesforce client")
return nil
}
err := client.LoginPassword(sfUser, sfPassword, sfToken)
if err != nil {
logger.Fatal("failed with salesforce client authentication")
return nil
}
return client
}
// Return SOQL Query results
func sfQuery(q string) *sf.QueryResult {
sfClient := createSfClient()
result, err := sfClient.Query(q)
if err != nil {
logger.Fatal("query failed")
}
if result.TotalSize < 1 {
logger.Println("no records returned.")
}
return result
}
// Do Main stuff
func main() {
// setup scrapper logger
logger = log.New(os.Stderr, "[RancherKB-Fuzz] ", log.Lmsgprefix|log.LstdFlags)
// get articles ID
articlesId := sfQuery(sfIdsRequest)
// setup articles list
book := make([]Article, 0)
// setup GoColly Queue
collyQ, _ := collyQueue.New(
10,
&collyQueue.InMemoryQueueStorage{MaxSize: 10000},
)
// prepare articles Collector
articleCollector := colly.NewCollector(
colly.URLFilters(
regexp.MustCompile(`www.suse.com/support/kb/(.*)/?id(.*)`),
),
colly.MaxDepth(1),
)
articleCollector.SetRequestTimeout(30 * time.Second)
articleCollector.Limit(&colly.LimitRule{
Parallelism: 4,
Delay: 10 * time.Second,
RandomDelay: 5 * time.Second,
})
articleCollector.WithTransport(&http.Transport{
DisableKeepAlives: false,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 50,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 3 * time.Second,
})
articleCollector.OnHTML(".col_one", func(mainHtmlArticle *colly.HTMLElement) {
page := Article{}
url := mainHtmlArticle.Request.URL.String()
title := mainHtmlArticle.ChildText("h1")
content, _ := mainHtmlArticle.DOM.Html()
page.Id = url[len(url)-9:]
page.Url = url
page.Title = title
page.Content = content
book = append(book, page)
fmt.Println("-------")
fmt.Println("Id:", page.Id)
fmt.Println("Link:", page.Url)
fmt.Println("Title:", page.Title)
fmt.Println("-------")
converter := md.NewConverter("", true, nil)
converter.Use(mdPlugin.GitHubFlavored())
markdown, err := converter.ConvertString(page.Content)
if err != nil {
logger.Println(err)
os.Exit(5)
}
file, err := os.Create(kbPath + page.Id + ".md")
if err != nil {
logger.Println(err)
os.Exit(10)
} else {
file.WriteString(markdown)
}
file.Close()
})
articleCollector.OnRequest(func(response *colly.Request) {
logger.Println("visiting kb:", response.URL.String())
})
// articleCollector.OnResponse(func(response *colly.Response) {
// fmt.Println("Response received:", response.StatusCode)
// })
articleCollector.OnError(func(response *colly.Response, err error) {
logger.Println("Http Code:", response.StatusCode)
logger.Println("got this error:", err)
os.Exit(15)
})
for _, record := range articlesId.Records {
id := record.StringField("ArticleNumber")
collyQ.AddURL("https://www.suse.com/support/kb/doc/?id=" + id)
}
collyQ.Run(articleCollector)
}