summaryrefslogtreecommitdiff
path: root/crawler/shop_drankdozijn.go
blob: ed8279240cdb532fb4aca9ec9c4219e63f69441d (plain)
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
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math"
	"net/http"
	//"strings"

	//"github.com/gocolly/colly"
	log "github.com/sirupsen/logrus"
)

func (app *App) ScrapeDrankdozijn(shop Shop) []Angebot {

	Offers := []Angebot{}

	/**
	 * Parse the API.
	 */
	API_URL := "https://es-api.drankdozijn.nl/sale-products?country=DE&language=de"
	API_URL_PRODUCT := "https://es-api.drankdozijn.nl/product?country=DE&language=de&page_template=artikel&alias="
	IMAGE_URL := "https://res-2.cloudinary.com/boozeboodcdn/image/upload/e_trim:10/c_pad,g_south,h_400,w_280/c_limit,h_910,w_280/f_auto,q_auto:best/v1/"

	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_products struct {
		Alias  string
		Images []string
	}
	type api_product_group struct {
		Description string
	}
	type api_offer struct {
		Type            string
		Price           float64
		SalePrice       float64
		SaleDescription string
		Group           map[string]interface{}
		ProductGroup    api_product_group
		Products        []api_products
	}
	type Value struct {
		Alias       string `json:"alias"`
		Description string `json:"description"`
	}
	type Feature struct {
		Alias       string `json:"alias"`
		Description string `json:"description"`
		Id          int    `json:"id"`
		Value       Value  `json:"value"`
		ShownOnPage bool   `json:"shownOnPage"`
	}
	type product_details struct {
		FormatedPrice     string    `json:"formatedPrice"`
		FormatedSalePrice string    `json:"formatedSalePrice"`
		Description       string    `json:"description"`
		Features          []Feature `json:"features`
	}

	var offers []api_offer
	//err = json.Unmarshal(api_body, &tmp_api_map)
	err = json.Unmarshal(api_body, &offers)
	if err != nil {
		// TODO

		log.Println("offers json unmarshal failed")
		log.Printf("%+v\n", string(api_body))
		panic(err)
	}

	for _, api_data := range offers {

		W := Angebot{}
		W.Shop = shop.Id

		W.Name = api_data.SaleDescription

		tmp_spirit_type := api_data.ProductGroup.Description

		if "Bier" == tmp_spirit_type {
			W.Debug("Drankdozijn: skip offer because it's beer")
			continue
		}

		W.Spirit_type = detect_spirit_type(tmp_spirit_type)

		if api_data.Price == 0 {
			//log.Println("%v\n", api_data["price"])
			//log.Println("%v\n", api_data)
			W.Debug("Drankdozijn: price is nil -> skip offer")
			continue
		}
		W.Original_price, err = convert_price(fmt.Sprintf("%.2f", api_data.Price))
		if err != nil {
			// TODO
			panic(err)
		}

		W.Discounted_price, err = convert_price(fmt.Sprintf("%.2f", api_data.SalePrice))
		if err != nil {
			// TODO
			panic(err)
		}

		if W.Discounted_price >= W.Original_price {
			W.Debug("Drankdozijn: Discounted price is not cheaper")
			continue
		}

		// Offer URL
		//tmp_offer_url_img_map := api_data.Products["products"].(map[string]interface{})
		tmp_offer_product_map := api_data.Products

		// Check for bundle offer
		if len(tmp_offer_product_map) > 1 {
			W.Debug("Drankdozijn: Skip Offer because of bundle")
			continue
		}

		var alias string
		for _, v := range tmp_offer_product_map {
			W.Url = "https://drankdozijn.de/artikel/" + v.Alias
			//tmp_image_map := tmp_map["images"].([]interface{})
			//W.Image_url = IMAGE_URL + tmp_image_map[0].(string)
			if len(v.Images) > 0 {
				W.Image_url = IMAGE_URL + v.Images[0]
				alias = v.Alias
			} else {
				W.Image_url = ""
			}
		}

		if W.Image_url == "" {
			W.Debug("Drankdozijn: No image")
			continue
		}

		req, err := http.NewRequest(http.MethodGet, API_URL_PRODUCT+alias, 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)
		}
		var d12_product product_details
		err = json.Unmarshal(api_body, &d12_product)
		if err != nil {
			// TODO
			log.Println("product_details json unmarshal failed")
			log.Printf("%+v\n", string(api_body))
			panic(err)
		}

		for _, v := range d12_product.Features {
			if v.Description == "Alkoholgehalt" {
				W.Abv, err = extract_abv(v.Value.Description)
				if err != nil {
					log.Println("extracting abv failed")
				}
			} else if v.Description == "Inhalt" {
				W.Volume, err = extract_volume(v.Value.Description)
				if err != nil {
					log.Println("extracting volume failed")
				}
			} else if v.Description == "Kategorie" {
				var tmp_type string
				if "Moussierend" == v.Value.Description {
					tmp_type = "Sekt"
				} else {
					tmp_type = detect_spirit_type(v.Value.Description)
				}
				if "Champagner" == tmp_type || "Sekt" == tmp_type {
					if tmp_type != W.Spirit_type {
						W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
						W.Spirit_type = tmp_type
					}
					W.Spirit_type = tmp_type
				}

				switch W.Spirit_type {
				case "Cognac":
					if tmp_type != W.Spirit_type {
						W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
						W.Spirit_type = tmp_type
					}
					W.Spirit_type = tmp_type
				case "Brandy":
					if tmp_type != W.Spirit_type {
						W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
						W.Spirit_type = tmp_type
					}
					W.Spirit_type = tmp_type
				case "Sherry":
					if tmp_type != W.Spirit_type {
						W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
						W.Spirit_type = tmp_type
					}
					W.Spirit_type = tmp_type
				case "Likör":
					retest_type := detect_spirit_type(tmp_type)
					if "Likör" != retest_type && "Verschiedenes" != retest_type {
						if tmp_type != W.Spirit_type {
							W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + retest_type)
							W.Spirit_type = tmp_type
						}
						W.Spirit_type = detect_spirit_type(W.Name)
					}
					if "Tequila" == tmp_type {
						if tmp_type != W.Spirit_type {
							W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
							W.Spirit_type = tmp_type
						}
						W.Spirit_type = tmp_type
					}
					if "Mezcal" == tmp_type {
						if tmp_type != W.Spirit_type {
							W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
							W.Spirit_type = tmp_type
						}
						W.Spirit_type = tmp_type
					}
					if "Baijiu" == tmp_type {
						if tmp_type != W.Spirit_type {
							W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
							W.Spirit_type = tmp_type
						}
						W.Spirit_type = tmp_type
					}
					if "Absinth" == tmp_type {
						if tmp_type != W.Spirit_type {
							W.Trace("Spirit Type Changed: " + W.Spirit_type + " -> " + tmp_type)
							W.Spirit_type = tmp_type
						}
						W.Spirit_type = tmp_type
					}
				}
			}
		}

		W.Base_price = int(math.Round(float64(W.Discounted_price) / float64(W.Volume)))

		//W.Debug("DEBUG OFFER")
		if W.Abv == 0 {
			W.Println("Drankdozijn: Abv is zero")
			continue
		}

		Offers = append(Offers, W)
	}

	return Offers
}