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

import (
	"strings"

	"github.com/gocolly/colly"
)

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

	Shop_url := "https://www.whiskyzone.de/widgets/emotion/index/emotionId/248/controllerName/listing"

	Whiskys := []Angebot{}

	c := colly.NewCollector(
		colly.AllowedDomains("whiskyzone.de"),
		colly.AllowedDomains("www.whiskyzone.de"),
	)

	c.OnHTML(".product--info", func(e *colly.HTMLElement) {

		if e.Request.URL.String() != Shop_url {
			return
		}

		W := Angebot{}

		W.Name = e.ChildAttr("a", "title")
		W.Url = e.ChildAttr("a", "href")

		e.ForEach(".image--media", func(i int, e *colly.HTMLElement) {
			W.Image_url = e.ChildAttr("img", "src")
		})

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

		e.Request.Visit(W.Url)

		if "sold_out" == e.Request.Ctx.Get("sold_out") {
			PrintlnOffer(W, "Whiskyzone: Sold out")
			return
		}

		var err error
		W.Discounted_price, err = convert_price(e.Request.Ctx.Get("discounted_price"))
		if err != nil {
			W.error_msg = err.Error()
			W.error_ctx = e.Request.Ctx.Get("discounted_price")
			WarnOffer(W, "Whiskyzone: Convert discounted price failed")
			return
		}

		W.Original_price, err = convert_price(e.Request.Ctx.Get("original_price"))
		if err != nil {
			W.error_msg = err.Error()
			W.error_ctx = e.Request.Ctx.Get("original_price")
			WarnOffer(W, "Whiskyzone: Convert original price failed")
			return
		}

		var ctx string
		W.Volume, ctx = get_volume(e)
		if W.Volume == 0 {
			W.error_msg = "Whiskyzone: Volume is zero"
			W.error_ctx = ctx
			WarnOffer(W, "Whiskyzone: Volume is zero")
			return
		}
		W.Abv, ctx = get_abv(e)
		if W.Abv == 0 {
			W.error_msg = "Whiskyzone: Abv is zero"
			W.error_ctx = ctx
			WarnOffer(W, "Whiskyzone: Abv is zero")
			return
		}

		base_price := e.Request.Ctx.Get("base_price")
		if base_price == "same_as_discounted_price" {
			W.Base_price = W.Discounted_price
		} else {
			W.Base_price, err = get_base_price(e)
			if err != nil {
				W.error_msg = err.Error()
				W.error_ctx = base_price
				WarnOffer(W, "Whiskyzone: Extracting base price failed")
				return
			}
		}

		W.Website = e.Request.Ctx.Get("website")
		Whiskys = append(Whiskys, W)
	})

	c.OnHTML(".product--buybox", func(e *colly.HTMLElement) {
		if e.Request.URL.String() == Shop_url {
			return
		}

		// Original & Discounted Price
		e.ForEach(".product--price.price--default.price--discount", func(i int, e *colly.HTMLElement) {
			e.Request.Ctx.Put("discounted_price", e.ChildText(".price--content.content--default"))
			e.Request.Ctx.Put("original_price", e.ChildText(".price--line-through"))
		})

		// Volume & Base Price
		e.ForEach(".product--price.price--unit", func(i int, e *colly.HTMLElement) {
			text_noisy_t := e.Text
			text_noisy_t = strings.Replace(text_noisy_t, "Inhalt", "", 1)
			text_noisy_t = strings.Replace(text_noisy_t, ":", "", 1)

			// Containts the base price in "(" if it's not "1 Liter"
			if strings.Contains(text_noisy_t, "(") {
				text_noisy := strings.Split(text_noisy_t, "(")
				volume_noisy := strings.Replace(text_noisy[0], "(", "", 1)
				e.Request.Ctx.Put("volume", volume_noisy)

				base_price_noisy := strings.Replace(text_noisy[1], ")", "", 1)
				e.Request.Ctx.Put("base_price", base_price_noisy)
			} else {
				e.Request.Ctx.Put("volume", text_noisy_t)
				e.Request.Ctx.Put("base_price", "same_as_discounted_price")
			}
		})

		// ABV
		e.ForEach(".base-info--entry.entry-attribute", func(i int, e *colly.HTMLElement) {
			text_noisy := e.ChildText(".entry--content")

			if strings.Contains(text_noisy, "Alkoholgehalt") && strings.Contains(text_noisy, "%") {
				abv_noisy := strings.Replace(text_noisy, "Alkoholgehalt:", "", 1)
				e.Request.Ctx.Put("abv", abv_noisy)

			}
		})

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

	// Product not found
	c.OnHTML(".detail-error--headline", func(e *colly.HTMLElement) {
		if e.Request.URL.String() == Shop_url {
			return
		}

		e.Request.Ctx.Put("sold_out", "sold_out")

	})

	c.Visit(Shop_url)

	return Whiskys
}