-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpricecheck.go
171 lines (138 loc) · 3.36 KB
/
pricecheck.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
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/xml"
"github.com/moovweb/gokogiri/xpath"
"io/ioutil"
"launchpad.net/goyaml"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var opts struct {
Products string `short:"p" long:"products" description:"A YAML file with product data" required:"true"`
Stores string `short:"s" long:"stores" description:"A YAML file with store data" required:"true"`
}
var storeList []Store
var productList []Product
type Store struct {
Name string
Domain string
XPath string
compiledXPath *xpath.Expression
}
type StorePrice struct {
Store *Store
Price float64
}
type Product struct {
Name string
URLs []string
StoreCount int
StorePrices []StorePrice
}
func (store *Store) LoadPrice(url string) (price float64, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
doc, err := gokogiri.ParseHtml(body)
if err != nil {
return
}
nxpath := xpath.NewXPath(doc.DocPtr())
nodes, err := nxpath.Evaluate(doc.DocPtr(), store.compiledXPath)
if err != nil {
return
}
if len(nodes) == 0 {
fmt.Printf("Check XPath correctness (not found) for domain: %s\n", store.Domain)
return
}
price_raw := xml.NewNode(nodes[0], doc).InnerHtml()
price_raw = strings.Trim(price_raw, "$ \n\r")
price, err = strconv.ParseFloat(price_raw, 64)
if err != nil {
fmt.Printf("Check XPath correctness (not monetary) for domain: %s\n", store.Domain)
return
}
return
}
func (product *Product) GetPrices(stores []Store) {
product.StorePrices = make([]StorePrice, len(product.URLs))
store_count := 0
for i, url := range product.URLs {
for j, store := range stores {
if !strings.Contains(url, store.Domain) {
continue
}
store_count += 1
price, _ := store.LoadPrice(url)
product.StorePrices[i] = StorePrice{Store: &stores[j], Price: price}
}
}
product.StoreCount = store_count
return
}
func main() {
// Parse options
_, err := flags.Parse(&opts)
if err != nil {
fmt.Println("Error: Check options")
return
}
// Open, parse YAML data
f, _ := os.Open(opts.Products)
products := make([]byte, 10000)
count, _ := f.Read(products)
err = goyaml.Unmarshal(products[:count], &productList)
if err != nil {
fmt.Println("Error: Check YAML file of product data")
return
}
f, _ = os.Open(opts.Stores)
stores := make([]byte, 10000)
count, _ = f.Read(stores)
err = goyaml.Unmarshal(stores[:count], &storeList)
if err != nil {
fmt.Println("Error: Check YAML file of store data")
return
}
// Compile XPaths
for i, store := range storeList {
storeList[i].compiledXPath = xpath.Compile(store.XPath)
}
// Loop through products
for i, _ := range productList {
productList[i].StoreCount = -1
go productList[i].GetPrices(storeList)
}
for {
complete := true
for _, product := range productList {
if product.StoreCount == -1 {
complete = false
}
}
if complete == true {
break
}
time.Sleep(100 * time.Millisecond)
}
for _, product := range productList {
fmt.Printf("\033[1m%s\033[0m\n", product.Name)
// Get prices
for _, store_price := range product.StorePrices {
fmt.Printf(" - %s: \t$%s\n", store_price.Store.Name, strconv.FormatFloat(store_price.Price, 'f', 2, 32))
}
}
}