summaryrefslogtreecommitdiff
path: root/crawler/utility.go
blob: 0650546f91413899f2d4a0b53efc1ca83dc17900 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main

import (
	"errors"
	"math"
	"regexp"
	"strconv"
	"strings"

	"github.com/gocolly/colly"
)

func stringInSlice(a string, list []string) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

func detect_spirit_type(name string) string {
	matched, err := regexp.MatchString(`(^|\s)Gin(\s|$)`, name)
	if err != nil {
		Fatal(err, "Gin regex failed")
	}
	if matched {
		return "Gin"
	}
	matched, err = regexp.MatchString(`(^|\s)Rh?um(\s|$)`, name)
	if err != nil {
		Fatal(err, "Rum regex failed")
	}
	if matched {
		return "Rum"
	}
	matched, err = regexp.MatchString(`(^|\s)[VW]odka(\s|$)`, name)
	if err != nil {
		Fatal(err, "Wodka regex failed")
	}
	if matched {
		return "Wodka"
	}
	matched, err = regexp.MatchString(`(^|\s)Whiske?y(\s|$)`, name)
	if err != nil {
		Fatal(err, "Whisky regex failed")
	}
	if matched {
		return "Whisky"
	}
	matched, err = regexp.MatchString(`(^|\s)Champagner(\s|$)`, name)
	if err != nil {
		Fatal(err, "Champagner regex failed")
	}
	if matched {
		return "Champagner"
	}
	matched, err = regexp.MatchString(`(^|\s)Cognac(\s|$)`, name)
	if err != nil {
		Fatal(err, "Cognac regex failed")
	}
	if matched {
		return "Cognac"
	}
	matched, err = regexp.MatchString(`(^|\s)Grappa(\s|$)`, name)
	if err != nil {
		Fatal(err, "Grappa regex failed")
	}
	if matched {
		return "Grappa"
	}
	matched, err = regexp.MatchString(`(^|\s)Likör(\s|$)`, name)
	if err != nil {
		Fatal(err, "Likör regex failed")
	}
	if matched {
		return "Likör"
	}

	return "Verschiedenes"
}

func extract_volume(volume string) (float32, error) {
	var volume_noisy string
	var is_litre_instead_of_cl bool

	// difference between cl...
	r_cl, err := regexp.Compile(`[0-9]+([,.][0-9]+)?( )?[cC][lL]`)
	if err != nil {
		Fatal(err, "Extract volume (centiliter) regex failed")
	}

	volume_noisy = r_cl.FindString(volume)

	if volume_noisy == "" {
		// ...and litre
		is_litre_instead_of_cl = true

		r_liter, err := regexp.Compile(`[0-9]+([,.][0-9]+)?( )?[lL](iter)?`)
		if err != nil {
			Fatal(err, "Extract volume regex failed")
		}
		volume_noisy = r_liter.FindString(volume)
	}

	// extract numbers
	r_liter2, err := regexp.Compile(`[0-9]+([,.][0-9]+)?`)
	if err != nil {
		Fatal(err, "2nd extract volume regex failed")
	}
	volume_noisy = r_liter2.FindString(volume_noisy)
	volume_noisy = strings.Replace(volume_noisy, ",", ".", 1)
	volume64, err := strconv.ParseFloat(volume_noisy, 0)
	if err != nil {
		Println(err, "Parsing volume to float failed")
		return 0, err
	}

	// converting from cl to litre
	if !is_litre_instead_of_cl {
		volume64 = volume64 / 100
	}

	return float32(volume64), err
}

func extract_abv(abv_noisy string) (float32, error) {
	if strings.Contains(abv_noisy, "%") {
		abv_noisy = strings.Replace(abv_noisy, "%", "", 1)
	}
	if strings.Contains(abv_noisy, "vol") {
		abv_noisy = strings.Replace(abv_noisy, "vol", "", 1)
	}
	if strings.Contains(abv_noisy, "Vol") {
		abv_noisy = strings.Replace(abv_noisy, "Vol", "", 1)
	}
	abv_noisy = strings.Replace(abv_noisy, ",", ".", 1)
	abv_noisy = strings.TrimSpace(abv_noisy)
	r_abv, err := regexp.Compile(`[0-9]+([,.][0-9]+)?`)
	if err != nil {
		Fatal(err, "Extract abv regex failed")
	}
	abv_noisy = r_abv.FindString(abv_noisy)

	abv64, err := strconv.ParseFloat(abv_noisy, 0)
	if err != nil {
		Println(err, "Parsing abv to float failed")
		return 0, err
	}

	return float32(abv64), nil
}

/*
 * In litre, but float.
 */
func get_volume(e *colly.HTMLElement) (float32, string) {

	volume_noisy := e.Request.Ctx.Get("volume")

	matched, err := regexp.MatchString(`[lL](iter)?`, volume_noisy)
	if err != nil {
		Fatal(err, "Get volume regex failed")
	}
	if !matched {
		return 0, volume_noisy
	}

	volume, err := extract_volume(volume_noisy)
	if err != nil {
		Warn(err, "Get Volume: Extract Volume failed: "+volume_noisy)
		return 0, err.Error() + " // volume_noisy: " + volume_noisy
	}

	return volume, ""
}

/*
 * In procent. (float)
 */
func get_abv(e *colly.HTMLElement) (float32, string) {

	abv_noisy := e.Request.Ctx.Get("abv")

	if abv_noisy == "" {
		return 0, abv_noisy
	}
	//	abv_noisy = strings.Replace(abv_noisy, ".", ",", 1)

	abv, err := extract_abv(abv_noisy)
	if err != nil {
		Warn(err, "Get ABV: Extract ABV failed: "+abv_noisy)
		return 0, err.Error() + " // abv_noisy: " + abv_noisy
	}

	return abv, ""
}

/*
 * In cents. (int)
 */
func get_base_price(e *colly.HTMLElement) (int, error) {

	base_price_noisy := e.Request.Ctx.Get("base_price")

	if base_price_noisy == "" {
		return 0, errors.New("Base price empty")
	}

	base_price, err := sanitize_base_price(base_price_noisy)
	if err != nil {
		return 0, err
	}

	return base_price, nil
}

/*
 * Source: https://golang.org/src/math/floor.go?s=2165:2200#L104
 * Will use std lib with go version >= 1.10
 */
func RoundToEven(x float64) float64 {
	t := math.Trunc(x)
	odd := math.Remainder(t, 2) != 0
	if d := math.Abs(x - t); d > 0.5 || (d == 0.5 && odd) {
		return t + math.Copysign(1, x)
	}
	return t
}