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

import (
	"regexp"

	"github.com/gocolly/colly"
)

func ScrapeMCWhisky(shop Shop) []Angebot {
	Shop_url := "https://www.mcwhisky.com/whisky/whisky-sonderangebote.html"

	Whiskys := []Angebot{}

	c := colly.NewCollector(
		colly.AllowedDomains("mcwhisky.com"),
		colly.AllowedDomains("www.mcwhisky.com"),
	)

	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 {
					Fatal(err, "MC Whisky: Converting original price failed")
				}
			})
			e.ForEach(".special-price", func(i int, e *colly.HTMLElement) {
				W.Discounted_price, err = convert_price(e.ChildText(".price"))
				if err != nil {
					Fatal(err, "MC Whisky: Converting discounted price failed")
				}
			})
		})

		price_per_litre_noisy := e.ChildText(".price-box-extended-info-ppl")
		W.Base_price, err = sanitize_base_price(price_per_litre_noisy)
		if err != nil {
			Fatal(err, "MC Whisky: Sanitizing base price failed")
		}

		W.Image_url = e.ChildAttr("img", "src")

		e.Request.Visit(W.Url)

		W.Volume = get_volume(e)
		if W.Abv == 0 {
			DebugOffer(W, "MC Whisky: Volume is zero")
			return
		}
		W.Abv = get_abv(e)
		if W.Abv == 0 {
			DebugOffer(W, "MC Whisky: 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(".products-attributes-alcohol", func(e *colly.HTMLElement) {
		text_noisy := e.Text

		r_abv, err := regexp.Compile(`[0-9]+([,.][0-9]+)?( )?%`)
		if err != nil {
			Fatal(err, "MC Whisky: ABV regex failed")
		}

		e.Request.Ctx.Put("abv", r_abv.FindString(text_noisy))

		r_volume, err := regexp.Compile(`[0-9]+([,.][0-9]+)?( )?Liter$`)
		if err != nil {
			Fatal(err, "MC Whisky: Volume regex failed")
		}

		e.Request.Ctx.Put("volume", r_volume.FindString(text_noisy))
		e.Request.Ctx.Put("website", string(e.Response.Body))

	})

	c.Visit(Shop_url)

	return Whiskys
}