-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
51 lines (47 loc) · 1.11 KB
/
Copy pathparser.go
File metadata and controls
51 lines (47 loc) · 1.11 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
package main
import (
"strings"
"github.com/PuerkitoBio/goquery"
)
// ExtractPrices parses a tgju.org DOM and extracts price fields based on Persian labels
// mapped to English keys via columnMap.
func ExtractPrices(doc *goquery.Document) map[string]string {
data := make(map[string]string)
containers := []string{
"ul.info-bar.mobile-hide li",
"ul.info-bar li",
}
seen := make(map[string]bool)
for _, sel := range containers {
doc.Find(sel).Each(func(i int, s *goquery.Selection) {
fullText := strings.TrimSpace(s.Text())
if fullText == "" {
return
}
for fa, en := range columnMap {
if seen[en] {
continue
}
if strings.Contains(fullText, fa) {
candidates := []string{
strings.TrimSpace(s.Find(".value").Text()),
strings.TrimSpace(s.Find(".info-price").Text()),
strings.TrimSpace(s.Find("span").Last().Text()),
fullText,
}
for _, c := range candidates {
if c == "" {
continue
}
if num := firstNumberLike(c); num != "" {
data[en] = num
seen[en] = true
break
}
}
}
}
})
}
return data
}