-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (77 loc) · 2.05 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/meatcar/lcbo-car-fuel/client"
)
func GetProducts(c *client.Client, page int, limit int) (response client.ProductResponse, err error) {
req, err := c.ProductsRequest()
if err != nil {
return
}
client.PaginateRequest(req, page, limit)
body, err := c.DoRequest(req)
if err != nil {
return
}
err = json.Unmarshal(body, &response)
return
}
func main() {
ts := time.Now()
path := fmt.Sprintf("data/%04d-%02d-%02dT%02d:%02d:%02d",
ts.Year(), ts.Month(), ts.Day(), ts.Hour(), ts.Minute(), ts.Second())
err := os.Mkdir(path, 0750)
if err != nil {
log.Fatalf("Unable to make dir %s", path)
}
path = fmt.Sprintf("%s/products.json", path)
file, err := os.Create(path)
if err != nil {
log.Fatalf("Unable to open file %s", path)
}
c, err := client.NewClient()
if err != nil {
log.Fatalf("Error starting client: %v", err)
}
res, err := GetProducts(c, 1, 1)
if err != nil {
log.Fatalf("Error getting initial products: %v", err)
}
start := time.Now()
page, limit := 25, 200
count := res.ResultCount
for (page * limit) < count {
res, err := GetProducts(c, page, limit)
if err != nil {
log.Fatalf("Error getting products: %v", err)
}
page += 1
for i, product := range res.Products {
cur := (page-1)*limit + i + 1
elapsed := time.Now().Sub(start)
avg := elapsed.Seconds() / float64(cur)
duration := fmt.Sprintf("%.fs", float64(count-cur)*avg)
remaining, _ := time.ParseDuration(duration)
if err != nil {
log.Fatalf("Unable to parse duration %s: %v", duration, err)
}
fmt.Printf("\rFetching %d/%d %s elapsed (%.4f avg) %s remaining ...\033[K", cur, count, elapsed, avg, remaining)
req, err := c.ProductRequest(product.ItemNumber)
if err != nil {
log.Fatalf("Error creating product request: %v", err)
}
body, err := c.DoRequest(req)
if err != nil {
log.Fatalf("Error fetching product %q: %v", product, err)
}
_, err = file.Write(body)
if err != nil {
log.Fatalf("Error writing to file: %v", err)
}
}
}
}