Skip to content

Commit a433421

Browse files
committed
Report the average price of each item
1 parent f6bec1b commit a433421

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Price Tracker
2+
3+
Simple script to fetch current price of items on Amazon and archive historical prices
4+
5+
6+
### Usage
7+
8+
Edit `items.json` with a key value list of the item name and it's URL on Amazon
9+
10+
```
11+
$ go run main.go
12+
```
13+
14+
15+
### Output
16+
17+
```
18+
atlas$ go run main.go
19+
2017/10/22 14:24:29 Fetching today's prices...
20+
21+
2017/10/22 14:24:32 Today's price for "Seagate 8 TB Hard Drive" is $169.99
22+
23+
2017/10/22 14:24:32 The Average price for this item was $169.99 over 2 samples
24+
2017/10/22 14:24:32 The max price for this item was $169.99 on 2017-10-21
25+
2017/10/22 14:24:32 The min price for this item was $169.99 on 2017-10-21
26+
27+
2017/10/22 14:24:34 Today's price for "Western Digital 8 TB Hard Drive" is $198.99
28+
29+
2017/10/22 14:24:34 The Average price for this item was $198.99 over 2 samples
30+
2017/10/22 14:24:34 The max price for this item was $198.99 on 2017-10-21
31+
2017/10/22 14:24:34 The min price for this item was $198.99 on 2017-10-21
32+
```
33+
34+
35+
### TODO
36+
37+
* Provide range over which average price samples span
38+
* tests
39+
40+
41+
### Notes
42+
43+
* Build with go1.6.2 on macOS 10.11

items.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"Items": [
3+
{
4+
"Name": "Seagate 8 TB Hard Drive",
5+
"Link": "https://www.amazon.com/Seagate-Expansion-Desktop-External-STEB8000100/dp/B01HAPGEIE",
6+
"Prices": [
7+
{
8+
"Date": "2017-10-21",
9+
"Price": "$169.99"
10+
},
11+
{
12+
"Date": "2017-10-22",
13+
"Price": "$169.99"
14+
}
15+
]
16+
},
17+
{
18+
"Name": "Western Digital 8 TB Hard Drive",
19+
"Link": "https://www.amazon.com/dp/B01LQQHLGC/ref=twister_B0751SCZW7",
20+
"Prices": [
21+
{
22+
"Date": "2017-10-21",
23+
"Price": "$198.99"
24+
},
25+
{
26+
"Date": "2017-10-22",
27+
"Price": "$198.99"
28+
}
29+
]
30+
}
31+
]
32+
}

main.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,28 @@ type Config struct {
3434

3535
func main() {
3636

37-
log.Printf("Fetching today's prices...\n\n")
37+
log.Printf("Fetching today's prices...")
3838

3939
json_file := "items.json"
4040
product_link_map := getProductList(json_file)
4141

4242
for name, link := range product_link_map {
4343
price := getPriceFromSite(name, link)
44+
45+
fmt.Println("")
4446
log.Printf(`Today's price for "%s" is %s`, name, price)
47+
fmt.Println("")
4548

4649
addPriceToProductList(name, price, json_file)
4750

4851
average_price, sample_size := getAveragePriceForItem(name, json_file)
49-
log.Printf("The Average price for this item was $%.2f over %d samples\n\n", average_price, sample_size)
52+
log.Printf("The Average price for this item was $%.2f over %d samples", average_price, sample_size)
53+
54+
min_price, max_price, valid := getMinMaxPriceForItem(name, json_file)
55+
if valid {
56+
log.Printf("The max price for this item was %s on %s", max_price.Price, max_price.Date)
57+
log.Printf("The min price for this item was %s on %s", min_price.Price, min_price.Date)
58+
}
5059
}
5160
}
5261

@@ -172,6 +181,48 @@ func getAveragePriceForItem(name string, filename string) (float32, int) {
172181
}
173182

174183

184+
func getMinMaxPriceForItem(name string, filename string) (Price, Price, bool) {
185+
186+
raw, err := ioutil.ReadFile(filename)
187+
188+
if err != nil {
189+
panic("Failed to read JSON file : " + filename + " => " + err.Error())
190+
}
191+
192+
var conf Config
193+
json.Unmarshal(raw, &conf)
194+
195+
valid := false
196+
samples := 0
197+
var min_price Price
198+
var max_price Price
199+
200+
for item := range conf.Items {
201+
if conf.Items[item].Name == name {
202+
for i := range conf.Items[item].Prices {
203+
204+
current_price := conf.Items[item].Prices[i]
205+
206+
if current_price.Price > max_price.Price {
207+
max_price = current_price
208+
}
209+
210+
if current_price.Price < min_price.Price || i == 0 {
211+
min_price = current_price
212+
}
213+
214+
samples += 1
215+
}
216+
}
217+
}
218+
219+
if samples > 0 {
220+
valid = true
221+
}
222+
223+
return min_price, max_price, valid
224+
}
225+
175226

176227
func toJson(j Config) string {
177228
bytes, err := json.MarshalIndent(j, "", "\t")

0 commit comments

Comments
 (0)