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
|
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
//"github.com/gocolly/colly"
"github.com/PuerkitoBio/goquery"
log "github.com/sirupsen/logrus"
)
func (app *App) ScrapeSpirituosenWolf(shop Shop) []Angebot {
Offers := []Angebot{}
/**
* Parse the API.
*/
API_URL := "https://www.spirituosen-wolf.de/widgets/listing/listingCount/sCategory/466?p=1&n=100&c=466&o=7&loadProducts=1"
http_client := http.Client{}
req, err := http.NewRequest(http.MethodGet, API_URL, nil)
if err != nil {
// TODO
panic(err)
}
req.Header.Set("accept", "application/json")
req.Header.Set("User-Agent", "like googlebot")
api_resp, err := http_client.Do(req)
if err != nil {
// TODO
panic(err)
}
api_body, err := ioutil.ReadAll(api_resp.Body)
if err != nil {
// TODO
panic(err)
}
//log.Println("%v\n", string(api_body))
type api struct {
TotalCount int `json:"totalCount"`
Listing string `json:"listing"`
Pagination string `json:"pagination"`
}
var api_val api
err = json.Unmarshal(api_body, &api_val)
if err != nil {
log.Println("Wolf: offers json unmarshal failed")
log.Printf("%+s\n", string(api_body))
panic(err)
}
html, err := goquery.NewDocumentFromReader(bytes.NewBuffer([]byte(api_val.Listing)))
if err != nil {
log.Fatal(err)
}
html.Find(".product--box.box--basic").Each(func(count int, doc *goquery.Selection) {
offer := Angebot{}
offer.Shop = shop.Id
doc.Find(".sw_frontend_listing_box_article_imagewrapper").Each(func(i int, s *goquery.Selection) {
// Url
offer.Url, _ = s.Find("a").Attr("href")
// Image_url
_srcset, _ := s.Find("img").Attr("srcset")
srcset := strings.Split(_srcset, ",")
for _, image_url := range srcset {
if strings.Contains(image_url, "@2x") {
offer.Image_url = image_url
break
}
}
// Name
offer.Name, _ = s.Find("img").Attr("alt")
})
doc.Find(".sw_frontend_listing_box_article_priceinfo_alcvol").Each(func(i int, s *goquery.Selection) {
// Abv
offer.Abv, err = extract_abv(strings.TrimSpace(s.Text()))
if err != nil {
log.Println("Wolf: Extracting Abv failed")
}
})
doc.Find(".sw_frontend_listing_box_article_priceinfo_qty").Each(func(i int, s *goquery.Selection) {
// Volume
offer.Volume, err = extract_volume(strings.TrimSpace(strings.TrimPrefix("Inhalt:", strings.TrimSpace(s.Text()))))
if err != nil {
log.Println("Wolf: Extracting volume failed")
}
})
doc.Find(".sw_frontend_listing_box_article_price_pseudo").Each(func(i int, s *goquery.Selection) {
// Original_price
offer.Original_price, err = convert_price(strings.TrimSpace(s.Text()))
if err != nil {
log.Println("Wolf: Converting original_price failed")
}
})
doc.Find(".sw_frontend_listing_box_article_price_default").Each(func(i int, s *goquery.Selection) {
// Discounted_price
offer.Discounted_price, err = convert_price(strings.TrimSpace(s.Text()))
if err != nil {
log.Println("Wolf: Converting Discounted_price failed")
}
})
doc.Find(".sw_frontend_listing_box_article_priceinfo_baseprice").Each(func(i int, s *goquery.Selection) {
// Base_price
offer.Base_price, err = sanitize_base_price(strings.TrimSpace(s.Text()))
if err != nil {
log.Println("Wolf: Sanitizing Base_price failed")
}
})
// detect spirit_type
offer.Spirit_type = detect_spirit_type(offer.Name)
if offer.Spirit_type == "Verschiedenes" {
offer.Spirit_type = WolfGetSpiritTypeFromUrl(offer.Url)
}
Offers = append(Offers, offer)
})
return Offers
}
func WolfGetSpiritTypeFromUrl(url string) string {
url = strings.TrimPrefix("https://www.spirituosen-wolf.de/", url)
subfolders := strings.Split(url, "/")
for index, folder := range subfolders {
// match Gin, Whisky, R
switch folder {
case "whisk-e-y":
return "Whisky"
case "gin-co":
if subfolders[1] == "genever" {
return "Genever"
}
return "Gin"
case "likoer":
return "Likör"
case "rum-cachaca", "rum-co":
return "Rum"
case "champagner-co":
switch subfolders[1] {
case "champagner":
return "Champagner"
case "Cremant":
return "Cremant"
default:
return "Sekt"
}
case "wodka-vodka":
return "Wodka"
case "weine":
return "Wein"
case "grappa-korn-braende":
switch subfolders[index+1] {
case "grappa":
return "Grappa"
case "korn":
return "Korn"
default:
return "Verschiedenes"
}
case "brandy-cognac-co":
switch subfolders[index+1] {
case "cognac":
return "Cognac"
case "brandy":
return "Brandy"
case "calvados":
return "Calvados"
case "armagnac":
return "Armagnac"
}
case "cachaca":
return "Cachaca"
case "Absinth":
return "Absinth"
case "mezcal":
return "Mezcal"
case "Tequila":
return "Tequila"
default:
return "Verschiedenes"
}
}
return "Verschiedenes"
}
|