-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainApi.go
181 lines (153 loc) · 3.84 KB
/
mainApi.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
// This file is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/
// Original work by serstuk93.
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/gin-gonic/gin"
"golang.org/x/net/html"
)
type Product struct {
Title string `json:"title"`
Price string `json:"price"`
URL string `json:"url"`
}
// nolint
func mainApi() {
r := gin.Default()
r.GET("/check", func(c *gin.Context) {
var receivedUrls string
rawQuery := c.Request.URL.RawQuery
decodedUrl, err := url.QueryUnescape(rawQuery)
if err != nil {
return
}
// Find the `urls` parameter value
for _, param := range strings.Split(decodedUrl, "&") {
keyValue := strings.SplitN(param, "=", 2)
if keyValue[0] == "urls" && len(keyValue) > 1 {
receivedUrls = keyValue[1]
break
}
}
fmt.Println("Received URLs:", receivedUrls)
if receivedUrls == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": ErrIncorrectUrls})
return
}
// Split on the special delimiter
rawUrlList := strings.Split(receivedUrls, "|")
urlProductMap := make(map[string][]Product)
for _, rawUrl := range rawUrlList {
decodedUrl, err := url.QueryUnescape(rawUrl)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUrlFormat})
return
}
_, header, products, err := checkHeureka(decodedUrl)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUrlFormat})
return
}
if len(products) > 5 {
products = products[:5]
}
urlProductMap[header] = products
}
c.JSON(http.StatusOK, gin.H{"products": urlProductMap})
})
err := r.Run(":8080")
if err != nil {
return
}
}
func checkHeureka(url string) (string, string, []Product, error) {
//url := "https://monitory.heureka.sk/f:1676:34-;p:1/"
resp, err := http.Get(url)
if err != nil {
return "", "", nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", "", nil, err
}
doc, err := html.Parse(resp.Body)
if err != nil {
return "", "", nil, err
}
searchUrl := url
categoryHeader := findNodeByClass(doc, ClassHeading)
header := extractText(categoryHeader)
return searchUrl, header, extractProducts(doc), nil
}
func extractProducts(n *html.Node) []Product {
var products []Product
if n.Type == html.ElementNode && n.Data == "li" {
isProductItem := false
for _, a := range n.Attr {
if a.Key == DataId && a.Val == ProductList {
isProductItem = true
break
}
}
if isProductItem {
titleNode := findNodeByClass(n, ClassTitle)
linkNode := findNodeByClass(n, ClassLink)
priceNode := findNodeByClass(n, ClassPrice)
if titleNode != nil && linkNode != nil && priceNode != nil {
product := Product{
Title: extractText(titleNode),
Price: extractText(priceNode),
URL: extractAttr(linkNode, "href"),
}
products = append(products, product)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
products = append(products, extractProducts(c)...)
}
return products
}
func findNodeByClass(n *html.Node, class string) *html.Node {
if n.Type == html.ElementNode {
for _, a := range n.Attr {
if a.Key == "class" && strings.Contains(a.Val, class) {
return n
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if found := findNodeByClass(c, class); found != nil {
return found
}
}
return nil
}
func extractText(n *html.Node) string {
if n == nil {
return ""
}
if n.Type == html.TextNode {
return strings.TrimSpace(n.Data)
}
var text string
for c := n.FirstChild; c != nil; c = c.NextSibling {
text += extractText(c)
}
return strings.TrimSpace(text)
}
func extractAttr(n *html.Node, attrName string) string {
if n == nil {
return ""
}
for _, a := range n.Attr {
if a.Key == attrName {
return a.Val
}
}
return ""
}