summaryrefslogtreecommitdiff
path: root/crawler/shop_whic.go
blob: 0e7cdf98c8567ad30e727bbe9dff8a6292681ff3 (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 (
	"regexp"
	"strings"

	"github.com/PuerkitoBio/goquery"
	"github.com/gocolly/colly"
)

func (app *App) ScrapeWhic(shop Shop) []Angebot {
	Shop_url := "https://whic.de/angebote"
	Whiskys := []Angebot{}

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

	c.OnHTML("li.item", func(e *colly.HTMLElement) {

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

		W := Angebot{}

		whisky_name := e.ChildAttr("a", "title")
		whisky_url := e.ChildAttr("a", "href")

		W.Name = whisky_name
		W.Url = whisky_url

		var err error

		e.ForEach(".price-box", func(i int, e *colly.HTMLElement) {
			e.ForEach(".old-price", func(i int, e *colly.HTMLElement) {
				W.Original_price, err = convert_price(e.ChildText(".price"))
				if err != nil {
					W.error_msg = err.Error()
					W.error_ctx = e.ChildText(".price")
					WarnOffer(W, "Whic: Converting original price failed")
					return
				}
			})
			e.ForEach(".special-price", func(i int, e *colly.HTMLElement) {
				W.Discounted_price, err = convert_price(e.ChildText(".price"))
				if err != nil {
					W.error_msg = err.Error()
					W.error_ctx = e.ChildText(".price")
					WarnOffer(W, "Whic: Converting discounted price failed")
					return
				}
			})
		})

		base_price_noisy := e.ChildText(".base-price")
		W.Base_price, err = sanitize_base_price(base_price_noisy)
		if err != nil {
			W.error_msg = err.Error()
			W.error_ctx = base_price_noisy
			WarnOffer(W, "Whic: Sanitizing base price failed")
			return
		}

		/*
		 * colly does not parse a <noscript>, thus we are reading the content and parse it as html.
		 */
		img_link_noisy := e.ChildText(".product-image")

		doc, err := goquery.NewDocumentFromReader(strings.NewReader(img_link_noisy))
		if err != nil {
			W.error_msg = err.Error()
			W.error_ctx = img_link_noisy
			FatalOffer(W, "Whic: Parsing document in Goquery failed")
		}
		W.Image_url, _ = doc.Find("img").Attr("src")

		e.Request.Visit(W.Url)

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

		W.Shop = shop.Id
		W.Spirit_type = "Whisky"
		W.Website = e.Request.Ctx.Get("website")

		Whiskys = append(Whiskys, W)
	})

	c.OnHTML("#product-view-head-txt-extra-info", func(e *colly.HTMLElement) {
		text_noisy := e.Text

		r_volume, err := regexp.Compile("Volumen: ([0-9]+([.,][0-9]+)) Liter")
		if err != nil {
			Fatal(err, "Whic: Volumen regex failed")
		}
		volume := r_volume.FindStringSubmatch(text_noisy)
		if volume == nil || len(volume) < 2 {
			return
		}

		e.Request.Ctx.Put("volume", volume[1]+"l")

		r_abv, err := regexp.Compile("Alkoholgehalt: ([0-9]+([.,][0-9]+))%")
		if err != nil {
			Fatal(err, "Whic: Abv regex failed")
		}
		abv := r_abv.FindStringSubmatch(text_noisy)
		if abv == nil || len(abv) < 2 {
			return
		}

		e.Request.Ctx.Put("abv", abv[1]+"%")
		e.Request.Ctx.Put("website", string(e.Response.Body))
	})

	c.Visit(Shop_url)

	return Whiskys
}