/
parser.go
197 lines (145 loc) · 4.46 KB
/
parser.go
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
package cherry
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
var (
//BaseURL for main site
BaseURL = "https://cherry.ee"
// Categories is an url and category map, move this map to config file
Categories = map[string]string{
"Perfume": fmt.Sprintf("%s%s", BaseURL, "/kaubad/parfuumid"),
"BeautyProducts": fmt.Sprintf("%s%s", BaseURL, "/pakkumised/kaubamaja/ilutooted"),
"SportsHealth": fmt.Sprintf("%s%s", BaseURL, "/kaubad/spordi-ja-tervisetooted"),
"HomeGarden": fmt.Sprintf("%s%s", BaseURL, "/kaubad/kodu-ja-aed"),
"FoodConsumer": fmt.Sprintf("%s%s", BaseURL, "/kaubad/toidu-ja-tarbekaubad"),
"Fashion": fmt.Sprintf("%s%s", BaseURL, "/kaubad/moekaubad"),
"Children": fmt.Sprintf("%s%s", BaseURL, "/kaubad/lastekaubad-ja-manguasjad"),
}
)
// Offer is single cherry structure with all the possible
// metadata available
type Offer struct {
Title string
URL string
Price float64
PromoPrice float64
Bought int
Limit int
Time time.Duration
}
// ParseResult is high level container for the offers
type ParseResult struct {
Category string
Created time.Time
Offers []*Offer
}
func (o Offer) String() string {
return fmt.Sprintf("[%s - %0.2f - %0.2f bought %d limit '%d' time %d]",
o.Title, o.Price, o.PromoPrice, o.Bought, o.Limit, o.Time)
}
// The timeleft is not in each page at the same location
// that is the reason why there is need to go through the elements
//
// It returns the string of javascript object
func getTimeCache(d *goquery.Document) string {
filterScript := func(index int, s *goquery.Selection) bool {
return strings.Contains(s.Text(), "timeleft_cache")
}
// Handle case when there is no element
return d.Find("script").FilterFunction(filterScript).First().Text()
}
// Times are cached in string so to parse
// first needs a string clearing and then parse as a JSON
func parseTimeCache(cache string) map[string]int {
m := map[string]int{}
// First trim space
cache = strings.TrimSpace(cache)
// Trim the variable instance
cache = strings.TrimLeft(cache, "timeleft_cache = ")
err := json.Unmarshal([]byte(cache), &m)
if err != nil {
log.Println("Error on parsing timecache")
return m
}
return m
}
func getTimerValue(s *goquery.Selection) string {
timeVal := s.Find("span.timer").AttrOr("class", "")
timeVal = strings.TrimSuffix(timeVal, " timer")
timeVal = strings.TrimPrefix(timeVal, "timeleft_")
return timeVal
}
func getTitle(s *goquery.Selection) string {
return s.Find("h3 > a").Text()
}
func getURL(s *goquery.Selection) string {
return s.Find("h3 > a").AttrOr("href", "")
}
func getPrice(s *goquery.Selection) (result float64) {
fmt.Sscan(s.Find(".price > .actual").Text(), &result)
return
}
func getPromotionPrice(s *goquery.Selection) (result float64) {
fmt.Sscan(s.Find(".price > .promotion").Text(), &result)
return
}
func getBoughtAmount(s *goquery.Selection) (bought int) {
fmt.Sscan(s.Find(".amount > strong").Text(), &bought)
return
}
func getLimit(s *goquery.Selection) (limit int) {
limitText := s.Find(".limit").Text()
num, err := strconv.Atoi(strings.TrimPrefix(limitText, "/ "))
if err != nil {
num = 0
}
return num
}
// Parse based on the goquery.Document object and return result
func Parse(category string, d *goquery.Document) *ParseResult {
offers := []*Offer{}
// The script tag holds object map of time
timeCacheText := getTimeCache(d)
timeIDMap := parseTimeCache(timeCacheText)
d.Find(".box-green").Each(func(i int, s *goquery.Selection) {
duration := time.Duration(int64(timeIDMap[getTimerValue(s)]))
offers = append(offers, &Offer{
getTitle(s),
getURL(s),
getPrice(s),
getPromotionPrice(s),
getBoughtAmount(s),
getLimit(s),
duration})
})
return &ParseResult{
category,
time.Now(),
offers,
}
}
// ParseFromReader parse result from io.Reader, could be example from file
func ParseFromReader(category string, r io.Reader) (*ParseResult, error) {
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return nil, errors.New("Failed to parse from io reader")
}
return Parse(category, doc), nil
}
// ParseFromResponse parse the result from HTTP response
func ParseFromResponse(category string, r *http.Response) (*ParseResult, error) {
doc, err := goquery.NewDocumentFromResponse(r)
if err != nil {
return nil, errors.New("Failed to parse from HTTP response")
}
return Parse(category, doc), nil
}