-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparse.go
More file actions
86 lines (78 loc) · 1.72 KB
/
Copy pathparse.go
File metadata and controls
86 lines (78 loc) · 1.72 KB
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
package safer
import (
"regexp"
"strconv"
"strings"
"time"
)
var (
dotSearchParamsRegex = regexp.MustCompile(`query_string=([0-9]+)`)
mcs150MileageYearRegex = regexp.MustCompile(`([0-9,]+) \(([0-9]{4})\)`)
)
func parseInt(text string) int {
if text == "" {
return 0
}
text = strings.Replace(text, ",", "", -1)
if parsed, err := strconv.Atoi(text); err == nil {
return parsed
}
return 0
}
func parseDate(text string) *time.Time {
if text == "" || len(text) < 10 {
return nil
}
if parsed, err := time.Parse("01/02/2006", text[:10]); err == nil {
return &parsed
}
return nil
}
func parsePctToFloat32(text string) float32 {
if text == "" {
return 0
}
if text[len(text)-1] == '%' {
text = text[:len(text)-1]
}
if f, err := strconv.ParseFloat(text, 64); err == nil {
return float32(f / 100)
}
return 0
}
func parseMCS150MileageYear(text string) (mileage int, year string) {
if text == "" {
return
}
if res := mcs150MileageYearRegex.FindStringSubmatch(text); len(res) == 3 {
mileage = parseInt(res[1])
year = res[2]
}
return
}
// parse an address returned by xpath query on html.
// multiline address returns an array of strings in this format:
// []string{"3101 S PACKERLAND DR", "GREEN BAY, WI \u00a0 54313", "X"}
func parseAddress(texts ...string) string {
var b strings.Builder
var written int
for _, text := range texts {
if text != "X" {
if written > 0 {
b.WriteString(" ")
}
b.WriteString(strings.ReplaceAll(text, "\u00a0 ", "")) // remove
written++
}
}
return b.String()
}
func parseDotFromSearchParams(params string) string {
if params == "" {
return ""
}
if res := dotSearchParamsRegex.FindStringSubmatch(params); len(res) == 2 {
return res[1]
}
return ""
}