summaryrefslogtreecommitdiff
path: root/crawler/shop_whiskyworld.go
blob: 3f0874d49dda12edf9f26169022aa88a38149dcf (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
package main

import (
	"strings"

	"github.com/gocolly/colly"
)

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

	Shop_urls := []string{"https://www.whiskyworld.de/themen/sonderangebote?ft=%257B%2522produkt_kategorie%2522:%2522Blended%2BMalt%2522%257D",
		"https://www.whiskyworld.de/themen/sonderangebote?ft=%257B%2522produkt_kategorie%2522:%2522Blended%2BWhiskies%2522%257D",
		"https://www.whiskyworld.de/themen/sonderangebote?ft=%257B%2522produkt_kategorie%2522:%2522Single%2BMalt%2522%257D",
	}

	Whiskys := []Angebot{}

	c := app.customCollector([]string{"whiskyworld.de", "www.whiskyworld.de"})

	c.OnHTML(".product-item", func(e *colly.HTMLElement) {
		if !stringInSlice(e.Request.URL.String(), Shop_urls) {
			return
		}

		W := Angebot{}

		W.Shop = shop.Id
		W.Spirit_type = "Whisky"

		whisky_name_part1 := e.ChildText(".item-brand")
		whisky_name_part2 := e.ChildText(".item-description")

		W.Name = whisky_name_part1 + " " + whisky_name_part2

		W.Url = "https://www.whiskyworld.de/" + e.ChildAttr("a", "href")

		regular_price_noisy := e.ChildText(".offer")
		regular_price := strings.TrimSuffix(strings.TrimPrefix(regular_price_noisy, "statt "), " €*")

		var err error

		W.Original_price, err = convert_price(regular_price)
		if err != nil {
			W.error_msg = err.Error()
			W.error_ctx = regular_price
			PrintlnOffer(W, "Whiskyworld: Converting original price failed")
			return
		}

		W.Discounted_price, err = convert_price(e.ChildText(".uvp"))
		if err != nil {
			W.error_msg = err.Error()
			W.error_ctx = e.ChildText(".uvp")
			PrintlnOffer(W, "Whiskyworld: Converting discounted price failed")
			return
		}

		e.ForEach(".product-infobox", func(i int, e *colly.HTMLElement) {
			text_noisy := e.ChildText(".item-inh")
			W.Volume, err = extract_volume(text_noisy)
			if err != nil {
				W.error_msg = err.Error()
				W.error_ctx = text_noisy
				PrintlnOffer(W, "Whiskyworld: Extracting volume failed")
				return
			}
			if W.Volume == 0 {
				W.error_msg = "Whiskyworld: Volume is zero"
				W.error_ctx = text_noisy
				PrintlnOffer(W, "Whiskyworld: Volume is zero")
				return
			}
			abv_noisy := strings.TrimSpace(strings.SplitAfter(text_noisy, "Liter")[1])
			abv_noisy = strings.TrimPrefix(abv_noisy, "/")
			W.Abv, err = extract_abv(abv_noisy)
			if err != nil {
				W.error_msg = err.Error()
				W.error_ctx = abv_noisy
				PrintlnOffer(W, "Whiskyworld: Extracting abv failed")
				return
			}
			if W.Abv == 0 {
				W.error_msg = "Whiskyworld: Abv is zero"
				W.error_ctx = abv_noisy
				PrintlnOffer(W, "Whiskyworld: Abv is zero")
				return
			}
		})

		e.ForEach(".price", func(i int, e *colly.HTMLElement) {
			base_price_noisy := e.ChildText(".unit")
			if strings.Contains(base_price_noisy, "Liter") {
				base_price_noisy = strings.TrimSpace(strings.SplitAfter(base_price_noisy, "Liter")[0])
				W.Base_price, err = sanitize_base_price(base_price_noisy)
				if err != nil {
					W.error_msg = err.Error()
					W.error_ctx = base_price_noisy
					PrintlnOffer(W, "Whiskyworld: Sanitizing base price failed")
					return
				}
			}

		})

		W.Image_url = e.ChildAttr("img", "data-src")
		if !strings.HasPrefix(W.Image_url, "https:") {
			W.Image_url = "https:" + W.Image_url
		}

		e.Request.Visit(W.Url)
		W.Website = e.Request.Ctx.Get("website")
		//Debug(nil, W.Website)

		Whiskys = append(Whiskys, W)
	})

	c.OnHTML("body", func(e *colly.HTMLElement) {
		if stringInSlice(e.Request.URL.String(), Shop_urls) {
			return
		}

		e.Request.Ctx.Put("website", string(e.Response.Body))
	})

	for _, url := range Shop_urls {
		err := c.Visit(url)
		if err != nil {
			Warn(nil, shop.Name+": "+err.Error())
		}
	}

	return Whiskys
}