This repository was archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
406 lines (356 loc) · 9.61 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/tealeg/xlsx"
)
const (
sitePath = "https://proraw.ru"
waitTime = 700 * time.Millisecond
)
// Section ...
type Section struct {
ID int
Title, link string
}
// Item ...
type Item struct {
Category int `json:"category"`
Title string `json:"title"`
Description string `json:"description"`
Article string `json:"article"`
Property string `json:"property"`
Available string `json:"available"`
Keywords string `json:"keywords"`
}
var sectionsList []Section
var itemsChan = make(chan Item, 10)
var waitChan = make(chan struct{}, 10)
var totalItemAmount = 0
var currentItemAmount = 0
func init() {
const sectionsQuery = ".v-catalog__content a:not(.v-catalog__expand)"
// download all sections and group they
page, err := downloadPage(sitePath)
if err != nil {
panic(err)
}
secs := page.Find(sectionsQuery)
sectionsList = make([]Section, secs.Length())
secs.Each(func(i int, s *goquery.Selection) {
href, ok := s.Attr("href")
if !ok {
return
}
re := regexp.MustCompile(`/filter|/apply/w*`)
re1 := regexp.MustCompile(`-is-`)
href = re.ReplaceAllString(href, "")
href = sitePath + re1.ReplaceAllString(href, "_")
text := s.Text()
sectionsList[i] = Section{i + 1, text, href}
})
}
func main() {
defer waitInput()
st := time.Now()
defer fmt.Println("Выход!")
defer func() { fmt.Println("Вермя выполнения программы - ", time.Since(st)) }()
selected := selectSections()
if len(selected) == 0 {
return
}
fmt.Println("Выбранные элементы:")
printSections(selected)
fmt.Print("Начать скачивание? (y/n) ")
start := ""
fmt.Fscan(os.Stdin, &start)
if start != "y" {
return
}
fmt.Println("Начало скачивания...")
wg := new(sync.WaitGroup)
// go saveItem(wg)
go saveItemToJSON(wg)
wg.Add(1)
go startDownload(selected, wg)
wg.Wait()
close(itemsChan)
}
func startDownload(sections []Section, wg *sync.WaitGroup) {
defer wg.Done()
for _, val := range sections {
wg.Add(1)
go parseSection(val, wg, val.ID)
}
}
func parseSection(section Section, wg *sync.WaitGroup, ind int) {
defer wg.Done()
fmt.Printf("%+s\n", section.link)
page, err := downloadPage(section.link)
if err != nil {
fmt.Println(err)
return
}
pagesAmount := 0
if pagesAmount == 0 {
wg.Add(1)
go parsePageSection(page, wg, ind)
}
}
func parsePageSection(page *goquery.Document, wg *sync.WaitGroup, ind int) {
defer wg.Done()
itemsLinks := page.Find(".v-products-tbl__title > .v-products-tbl__name").Map(func(i int, s *goquery.Selection) string {
href, _ := s.Attr("href")
return sitePath + href
})
totalItemAmount += len(itemsLinks)
for _, val := range itemsLinks {
wg.Add(1)
waitChan <- struct{}{}
go parseItemPage(val, wg, ind)
}
}
func parseItemPage(url string, wg *sync.WaitGroup, ind int) {
page, err := downloadPage(url)
if err != nil {
fmt.Println()
<-waitChan
return
}
// re := regexp.MustCompile(`\s+`)
title := page.Find(".v-product-info__title").Text()
description := page.Find(".v-product-description__header__content").Text()
article := strings.Replace(page.Find(".v-product-selections__sku .hint").First().Text(), "Артикул: ", "", -1)
property := strings.Join(page.Find(".v-product-features__zag + .v-product-features__table .v-product-features__tr").Map(func(i int, s *goquery.Selection) string {
text := ""
text += s.Find(".v-product-features__left").Text() + "|" + s.Find(".v-product-features__right").Text()
return text
// return re.ReplaceAllString(text, "")
}), "\n")
keywords, _ := page.Find("meta[name=\"Keywords\"]").Attr("content")
available := page.Find(".stock-high.v-product-stock._height").First().Text()
article = strings.Trim(article, " \n\t")
item := Item{ind, title, description, article, property, available, keywords}
itemsChan <- item
}
func saveItemToJSON(wg *sync.WaitGroup) {
jsonFileName := "proraw.js"
file, err := os.Create(jsonFileName)
if err != nil {
fmt.Println("невозможно открыть и создать файл js")
fmt.Println(err)
os.Exit(1)
}
file.WriteString("exports.arr = [")
first := true
for it := range itemsChan {
if !first {
file.WriteString(", ")
}
jsonString, err := json.Marshal(it)
if err != nil {
fmt.Printf("Неудалось преобразовать в json %s: %s", it.Article, err)
}
_, err = file.Write(jsonString)
if err != nil {
fmt.Printf("Неудалось записать в json %s: %s", it.Article, err)
}
currentItemAmount++
time.Sleep(waitTime)
<-waitChan
fmt.Printf("\r%d/%d\t-\t%s\t\t", currentItemAmount, totalItemAmount, it.Article)
wg.Done()
first = false
}
file.WriteString("]")
file.Close()
}
func saveItem(wg *sync.WaitGroup) {
excelFileName := "./proraw.xlsx"
var file *xlsx.File
var sheet *xlsx.Sheet
file, err := xlsx.OpenFile(excelFileName)
if err != nil {
file = xlsx.NewFile()
}
if len(file.Sheets) == 0 {
sheet, err = file.AddSheet("Sheet1")
if err != nil {
fmt.Printf(err.Error())
}
} else {
sheet = file.Sheets[0]
}
_ = sheet.SetColWidth(0, 0, 20)
_ = sheet.SetColWidth(1, 1, 20)
_ = sheet.SetColWidth(2, 2, 40)
_ = sheet.SetColWidth(3, 3, 20)
_ = sheet.SetColWidth(4, 4, 50)
_ = sheet.SetColWidth(4, 4, 50)
_ = sheet.SetColWidth(4, 4, 10)
fmt.Println("")
for it := range itemsChan {
addRow(it, sheet)
currentItemAmount++
time.Sleep(waitTime)
<-waitChan
fmt.Printf("\r%d/%d\t-\t%s\t\t", currentItemAmount, totalItemAmount, it.Article)
if err = file.Save(excelFileName); err != nil {
fmt.Printf("Неудалось записать %s в документ: %s", it.Article, err)
}
wg.Done()
}
fmt.Println()
}
func addRow(it Item, sheet *xlsx.Sheet) {
r := sheet.AddRow()
c := r.AddCell()
c.SetString(it.Article)
c = r.AddCell()
c.SetString(it.Title)
c = r.AddCell()
c.SetString(it.Description)
c = r.AddCell()
c.SetString(it.Property)
c = r.AddCell()
c.SetString(it.Available)
c = r.AddCell()
}
func downloadPageToFile(link string) error {
res, err := http.Get(link)
if err != nil {
return fmt.Errorf("ошибка при загрузке данных сайта!: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("ошибка при загрузке данных сайта!: %d %s", res.StatusCode, res.Status)
}
file, err := os.Create("html.html")
if err != nil {
fmt.Println(err)
return err
}
defer file.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
fmt.Println(err)
fmt.Printf("Неудалось сохранить картинку для %s: %s", "asd", err.Error())
return err
}
return nil
}
func downloadImg(url, artikul, folder string) {
if url == "no photo" {
return
}
res, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
fmt.Printf("status code error: %d %s\n", res.StatusCode, res.Status)
return
}
contentType := strings.Split(res.Header.Get("Content-Type"), "/")[1]
artikul = strings.ReplaceAll(artikul, "/", "_")
artikul = strings.ReplaceAll(artikul, " ", "")
imgPath := fmt.Sprintf("%s/%s.%s", folder, artikul, contentType)
fmt.Println(imgPath)
file, err := os.Create(imgPath)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
fmt.Println(err)
fmt.Printf("Неудалось сохранить картинку для %s: %s", artikul, err.Error())
return
}
}
func downloadPage(url string) (*goquery.Document, error) {
res, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("ошибка при загрузке данных сайта!: %s", err.Error())
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("ошибка при загрузке данных сайта!: %d %s", res.StatusCode, res.Status)
}
page, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, fmt.Errorf("ошибка при загрузке данных сайта!: %s", err.Error())
}
return page, nil
}
func selectSections() []Section {
fmt.Println("Выберите разделы, которые нужно скачать (написать цифру):")
fmt.Println("0\t-\tВыбрать все")
for key, val := range sectionsList {
fmt.Printf("%v\t-\t%s\n", key+1, val.Title)
}
fmt.Printf("%v\t-\tНачать\n", len(sectionsList)+1)
fmt.Println("-1\t-\tВыход")
selected := make([]Section, 0)
loop:
for {
cur := ""
fmt.Print("-> ")
fmt.Fscan(os.Stdin, &cur)
switch cur {
case fmt.Sprintf("%v", len(sectionsList)+1):
break loop
case "0":
fmt.Println("Выбраны все разделы!")
selected = append(make([]Section, 0), sectionsList[:]...)
break loop
case "-1":
return nil
default:
if len(cur) > 2 {
break
}
curInt, err := strconv.Atoi(cur)
if err != nil {
fmt.Println(err)
break
}
if curInt > len(sectionsList) || includes(selected, sectionsList[curInt-1].Title) {
break
}
selected = append(selected, sectionsList[curInt-1])
printSections(selected)
}
}
return selected
}
func includes(slice []Section, str string) bool {
for _, val := range slice {
if strings.Contains(val.Title, str) {
return true
}
}
return false
}
func printSections(arr []Section) {
for _, val := range arr {
fmt.Printf("\t%s\n", val.Title)
}
}
func waitInput() {
end := ""
fmt.Print("Для завершения введите любой символ и нажмите enter ")
fmt.Fscan(os.Stdin, &end)
}