-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (116 loc) · 3.04 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
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"net/http"
"os"
"runtime"
"sync"
"time"
)
// Ebay.
func GetEbayTags(ask string, wg *sync.WaitGroup) ([]string, error) {
// The goroutine is done.
// Defer statements are executed the last.
defer wg.Done()
// Answer.
var tags []string
// Creating url using given request and checking its correctness.
var url string = fmt.Sprintf("https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2380057.m570.l1313&_nkw=%s&_sacat=0", ask)
fmt.Println(url)
// Creating request and checking if it can be opened.
res, err := http.Get(url)
if err != nil {
fmt.Println(1)
return tags, err
}
// Closing request before leaving the function.
// Defer statements are executed the last.
defer res.Body.Close()
// If cannot work with res.
if res.StatusCode != 200 {
fmt.Println(2)
return tags, err
}
// Creating page source .html document and checking if it can be created.
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
fmt.Println(3)
return tags, err
}
// Searching all needed div tags.
doc.Find("div[class~=srp-related-searches]").Each(func(i int, s *goquery.Selection) {
// Searching all needed span tags inside the div tags.
s.Find("span[class=BOLD]").Each(func(i int, ss *goquery.Selection) {
// Logging.
tags = append(tags, ss.Text())
})
})
// Everything is ok.
return tags, nil
}
// Wildberries.
func GetWildberriesTags(ask string, wg *sync.WaitGroup) ([]string, error) {
// The goroutine is done.
// Defer statements are executed the last.
defer wg.Done()
// Answer.
var tags []string
// Creating url using given request and checking its correctness.
var url string = fmt.Sprintf("%s", ask)
fmt.Println(url)
// TODO...
// Everything is ok.
return tags, nil
}
func GetTags(ask string, wg *sync.WaitGroup) ([]string, []string) {
// Giving number of goroutines which we are going to wait.
wg.Add(2)
var ebayTags []string
var ebayError error
var wildberriesTags []string
var wildberriesError error
// Running both goroutines.
go func() {
ebayTags, ebayError = GetEbayTags(ask, wg)
}()
go func() {
wildberriesTags, wildberriesError = GetWildberriesTags(ask, wg)
}()
// Waiter waits at this part of code.
wg.Wait()
if ebayError != nil {
fmt.Println("Something went wrong in Ebay parsing!")
}
if wildberriesError != nil {
fmt.Println("Something went wrong in Wildberries parsing!")
}
return ebayTags, wildberriesTags
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}
func main() {
// Setting maximum number of threads we need to use.
runtime.GOMAXPROCS(min(runtime.NumCPU(), 2))
// Setting timer.
start := time.Now()
// Goroutines waiter.
var wg sync.WaitGroup
// If string is given, using it, otherwise set "anime", because anime rules.
var ask string
if len(os.Args) > 1 {
ask = os.Args[1]
} else {
ask = "anime"
}
// Getting all tags.
ebay, wildberries := GetTags(ask, &wg)
fmt.Println(ebay)
fmt.Println(wildberries)
// Printing the total time elapsed.
fmt.Printf("%v\n", time.Since(start))
}