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

import (
	"errors"
	"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 := app.customCollector([]string{"whiskyzone.de", "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.Ctx.Put("offer_url", W.Url)
		e.Request.Visit(W.Url)

		if "sold_out" == e.Request.Ctx.Get("sold_out") {
			W.Println("Whiskyzone: Sold out")
			e.Request.Ctx.Put("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")
			W.Println("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")
			W.Println("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
			W.Println("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
			W.Println("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
				W.Println("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) {
		offer_url := e.Request.Ctx.Get("offer_url")
		Debug(nil, "Visiting: "+e.Request.URL.String()+" with offer_url: "+offer_url)

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

		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) {
			Debug(errors.New("Discount: "+e.ChildText(".price--content.content--default")), "Whiskyzone: Original:"+e.ChildText(".price--line-through"))
			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--attribute1", 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(".alert.is--error > .alert--content", func(e *colly.HTMLElement) {
		if e.Request.URL.String() == Shop_url {
			return
		}

		if strings.Contains(e.Text, "Dieser Artikel steht derzeit nicht zur Verfügung!") {
			e.Request.Ctx.Put("sold_out", "sold_out")
		}

		return
	})

	err := c.Visit(Shop_url)
	if err != nil {
		shop.error_msg = err.Error()
		shop.error_ctx = Shop_url
		shop.Warn("Crawling failed")
	}

	return Whiskys
}