forked from at1as/price-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
264 lines (184 loc) · 5.32 KB
/
main.go
File metadata and controls
264 lines (184 loc) · 5.32 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"encoding/json"
"io/ioutil"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/lestrrat/go-libxml2"
"github.com/lestrrat/go-libxml2/types"
"github.com/lestrrat/go-libxml2/xpath"
)
type Price struct {
Date string
Price string
}
type Item struct {
Name string
Link string
Prices []Price
}
type Config struct {
Items []Item
}
func main() {
log.Printf("Fetching today's prices...")
json_file := "items.json"
product_link_map := getProductList(json_file)
for name, link := range product_link_map {
price := getPriceFromSite(name, link)
fmt.Println("")
log.Printf(`Today's price for "%s" is %s`, name, price)
fmt.Println("")
addPriceToProductList(name, price, json_file)
average_price, sample_size := getAveragePriceForItem(name, json_file)
log.Printf("The Average price for this item was $%.2f over %d samples", average_price, sample_size)
min_price, max_price, valid := getMinMaxPriceForItem(name, json_file)
if valid {
log.Printf("The max price for this item was %s on %s", max_price.Price, max_price.Date)
log.Printf("The min price for this item was %s on %s", min_price.Price, min_price.Date)
}
}
}
func getPriceFromSite(item_name string, link string) string {
site := strings.Split(link, "/")[2]
res, err := http.Get(link)
if err != nil {
panic("Failed to retrieve page at : " + site + " => " + err.Error())
}
doc, err := libxml2.ParseHTMLReader(res.Body)
if err != nil {
panic("Failed to parse HTML: " + err.Error())
}
defer res.Body.Close()
defer doc.Free()
doc.Walk(func(n types.Node) error {
return nil
})
target_xpath := `//*[@id="priceblock_ourprice"]`
text := xpath.String(doc.Find(target_xpath))
return text
}
func getProductList(filename string) map[string]string {
raw, err := ioutil.ReadFile(filename)
if err != nil {
panic("Failed to read JSON file: " + filename + " => " + err.Error())
}
var conf Config
err = json.Unmarshal([]byte(raw), &conf)
if err != nil {
panic("Failed to parse JSON file: " + filename + " => " + err.Error())
}
name_link := make(map[string]string)
for item := range conf.Items {
name_link[conf.Items[item].Name] = conf.Items[item].Link
}
return name_link
}
func addPriceToProductList(name string, price string, filename string) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
panic("Failed to read JSON file : " + filename + " => " + err.Error())
}
var conf Config
json.Unmarshal(raw, &conf)
for item := range conf.Items {
if conf.Items[item].Name == name {
// Don't add the price if it's already been added for today
for i := range conf.Items[item].Prices {
if conf.Items[item].Prices[i].Date == getDate() {
return
}
}
var p Price
p.Date = getDate()
p.Price = price
conf.Items[item].Prices = append(conf.Items[item].Prices, p)
}
}
fmt.Println(toJson(conf))
writeFile(toJson(conf), "items.json")
}
func getAveragePriceForItem(name string, filename string) (float32, int) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
panic("Failed to read JSON file : " + filename + " => " + err.Error())
}
var conf Config
json.Unmarshal(raw, &conf)
var price_total float32
price_total = 0.0
samples := 0
for item := range conf.Items {
if conf.Items[item].Name == name {
for i := range conf.Items[item].Prices {
next_price := conf.Items[item].Prices[i].Price
price_total += priceAsFloat(next_price)
samples += 1
}
}
}
if samples == 0 {
return 0.0, 0
}
return price_total / float32(samples), samples
}
func getMinMaxPriceForItem(name string, filename string) (Price, Price, bool) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
panic("Failed to read JSON file : " + filename + " => " + err.Error())
}
var conf Config
json.Unmarshal(raw, &conf)
valid := false
samples := 0
var min_price Price
var max_price Price
for item := range conf.Items {
if conf.Items[item].Name == name {
for i := range conf.Items[item].Prices {
current_price := conf.Items[item].Prices[i]
if current_price.Price > max_price.Price {
max_price = current_price
}
if current_price.Price < min_price.Price || i == 0 {
min_price = current_price
}
samples += 1
}
}
}
if samples > 0 {
valid = true
}
return min_price, max_price, valid
}
func toJson(j Config) string {
bytes, err := json.MarshalIndent(j, "", "\t")
if err != nil {
panic("Failed to save as JSON " + err.Error())
}
return string(bytes)
}
func writeFile(text string, filename string) {
err := ioutil.WriteFile(filename, []byte(text), 0644)
if err != nil {
panic("Failed to write JSON file to : " + filename + " => " + err.Error())
}
}
func getDate() string {
// => "YYYY-MM-DD"
return strings.Split(time.Now().Format(time.RFC3339), "T")[0]
}
func priceAsFloat(price string) float32 {
// "$169.99" => 169.99
price_value := strings.Split(price, "$")[1]
f, err := strconv.ParseFloat(price_value, 32)
if err != nil {
panic("Failed to parse :" + price + " to a float")
}
return float32(f)
}