-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecking.go
194 lines (171 loc) · 5.64 KB
/
checking.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
package main
import (
"encoding/xml"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html/charset"
)
// Rival contains a main URL and SerachURL
type Rival struct {
Name string `json:"name"`
URL string `json:"URL"`
SearchURL string `json:"searchURL"`
ProductsSelector string `json:"productsSelector"`
PriceSelector string `json:"priceSelector"`
TitleSelector string `json:"titleSelector"`
PricePattern string `json:"pricePattern"`
URLSelector string `json:"urlSelector"`
RedirectedProductSelector string `json:"redirectedProductSelector"`
RedirectedPriceSelector string `json:"redirectedPriceSelector"`
RedirectedTitleSelector string `json:"redirectedTitleSelector"`
RedirectedPricePattern string `json:"redirectedPricePattern"`
T string `json:"t"`
}
// Products contains a couple of the Product
type Products struct {
XMLName xml.Name `xml:"products"`
Amount int `xml:"amount,attr"`
ReqArticle string `xml:"reqArticle,attr"`
Data []*Product
}
// ProductsWithSeveralArticle contains a couple of the Products
type ProductsWithSeveralArticle struct {
XMLName xml.Name `xml:"articles"`
Data []*Products
Amount int `xml:"amount,attr"`
}
// Product contains a main info of a product
type Product struct {
XMLName xml.Name `xml:"product"`
Title string `xml:"title"`
ReqArticle string `xml:"-"`
Price string `xml:"price,attr"`
URL string `xml:"url"`
RivalName string `xml:"rivalName,attr"`
}
// ToString return format string of product
func (p *Product) ToString() string {
return fmt.Sprintf("%s (%s). %s", p.Title, p.Price, p.URL)
}
// Links to checking websites
// const (
// InstBy = "https://instr.by/"
// InstrumentBy = "https://instrument.by/"
// DewaltBDBy = "http://dewalt-bd.by/"
// ToolsBy = "http://www.tools.by/"
// TProBy = "https://tpro.by/"
// By7745 = "https://7745.by/"
// Vek21By = "https://www.21vek.by/"
// DelomasteraBy = "https://delomastera.by/"
// OmaBy = "https://www.oma.by/"
// )
// GetProductInfo return Product corresponding to the recived article
func (r *Rival) GetProductInfo(article string) (*Product, error) {
searchURL := fmt.Sprintf(r.SearchURL, article)
// fmt.Println(searchURL)
page, err, isRedirected, currentPageUrl := downloadPage(searchURL)
if err != nil {
return nil, err
}
productSelector := r.ProductsSelector
if isRedirected {
productSelector = r.RedirectedProductSelector
}
priceSelector := r.PriceSelector
if isRedirected {
priceSelector = r.RedirectedPriceSelector
}
titleSelector := r.TitleSelector
if isRedirected {
titleSelector = r.RedirectedTitleSelector
}
nodes := page.Find(productSelector)
if nodes.Size() == 0 {
return nil, fmt.Errorf("Подходящего товара не найдено")
}
nodesArr := make([]*goquery.Selection, 0, nodes.Size())
nodes = nodes.Each(func(i int, s *goquery.Selection) {
title := s.Find(titleSelector).Text()
rg := fmt.Sprintf(`%s(\s|\z|\))`, article)
matched, err := regexp.Match(rg, []byte(title))
if err != nil {
fmt.Println(err)
}
if matched {
nodesArr = append([]*goquery.Selection{s}, nodesArr...)
}
nodesArr = append(nodesArr, s)
})
product := new(Product)
prodNode := nodesArr[0]
product.Title = prodNode.Find(titleSelector).Text()
if product.Title == "" || product.Title == " " {
product.Title = "Заголовок отцувствует."
}
isUrlExist := false
prURL, ok := prodNode.Find(titleSelector).Attr("href")
prURL1, ok1 := prodNode.Find(r.URLSelector).Attr("href")
isUrlExist = ok || ok1
nextUrl := prURL
if nextUrl == "" {
nextUrl = prURL1
}
if isUrlExist {
if strings.Contains(nextUrl, "http") {
product.URL = nextUrl
} else {
product.URL = r.URL + nextUrl
}
} else if isRedirected {
product.URL = currentPageUrl
} else {
product.URL = "none"
}
product.ReqArticle = article
product.Price = strings.ReplaceAll(prodNode.Find(priceSelector).Text(), "\n", " ")
product.Price = strings.Trim(product.Price, " \n\t")
product.Price = extractPrice(product.Price, r.PricePattern)
product.RivalName = r.Name
return product, nil
}
func extractPrice(input string, regexPattern string) string {
if regexPattern == "" {
regexPattern = `([\d.,]+)`
}
regex := regexp.MustCompile(regexPattern)
match := regex.FindString(input)
return match
}
func downloadPage(url string) (*goquery.Document, error, bool, string) {
isRedirected := false
client := http.Client{
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
isRedirected = true
return nil
},
}
currentPageUrl := url
res, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("ошибка при загрузке данных сайта!: %s", err.Error()), isRedirected, currentPageUrl
}
currentPageUrl = res.Request.URL.String()
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("ошибка при загрузке данных сайта!: %d %s", res.StatusCode, res.Status), isRedirected, currentPageUrl
}
utf8, err := charset.NewReader(res.Body, res.Header.Get("Content-Type"))
if err != nil {
return nil, fmt.Errorf("Ошибка шифровки: %s", err.Error()), isRedirected, currentPageUrl
}
page, err := goquery.NewDocumentFromReader(utf8)
if err != nil {
return nil, fmt.Errorf("Ошибка при загрузке данных сайта!: %s", err.Error()), isRedirected, currentPageUrl
}
return page, nil, isRedirected, currentPageUrl
}