-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
193 lines (160 loc) · 3.63 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
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
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
type Config struct {
TrackingCodes map[string]string `json:"tracking_codes"`
Trackers struct {
ParcelsApp ParcelsAppConfig `json:"parcels_app"`
} `json:"trackers"`
UpdateEvery time.Duration `json:"update_every"`
}
func NewConfig(path string) (Config, error) {
var cfg Config
file, openErr := os.Open(path)
if openErr != nil {
return cfg, openErr
}
content, readErr := io.ReadAll(file)
if readErr != nil {
return cfg, readErr
}
return cfg, json.Unmarshal(content, &cfg)
}
type (
ParcelsAppTracker struct {
cfg ParcelsAppConfig
}
ParcelsAppConfig struct {
ChromePath string `json:"node_path"`
}
)
func NewParcelsAppTracker(cfg ParcelsAppConfig) ParcelsAppTracker {
return ParcelsAppTracker{cfg: cfg}
}
func (t ParcelsAppTracker) TrackParcels(trackingCodes map[string]string) (chan string, error) {
results := make(chan string, len(trackingCodes))
go func() {
var wg sync.WaitGroup
for label, code := range trackingCodes {
wg.Add(1)
go func(l, c string) {
defer wg.Done()
res, _ := t.TrackParcel(l, c)
results <- fmt.Sprintf(
"{\"label\":\"%s\",\"code\":\"%s\",\"results\":%s}",
l, c, string(res),
)
}(label, code)
}
wg.Wait()
close(results)
}()
return results, nil
}
func (t ParcelsAppTracker) TrackParcel(label, trackingCode string) ([]byte, error) {
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", "chrome"),
chromedp.Flag("disable-gpu", true),
chromedp.Flag("no-sandbox", true),
)
if t.cfg.ChromePath != "" {
opts = append(opts, chromedp.ExecPath(t.cfg.ChromePath))
}
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel := chromedp.NewContext(
allocCtx,
)
defer cancel()
var requestID network.RequestID
done := make(chan struct{})
chromedp.ListenTarget(ctx, func(ev interface{}) {
switch event := ev.(type) {
case *network.EventRequestWillBeSent:
if strings.Contains(event.Request.URL, "api/v2/parcels") {
requestID = event.RequestID
}
case *network.EventLoadingFinished:
if requestID == event.RequestID {
close(done)
}
}
})
err := chromedp.Run(
ctx,
chromedp.Navigate("https://parcelsapp.com/widget"),
chromedp.SetValue("#track-input", trackingCode),
chromedp.Click("#track-button", chromedp.NodeVisible),
)
<-done
var responseBody []byte
if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
var err error
responseBody, err = network.GetResponseBody(requestID).Do(ctx)
return err
})); err != nil {
log.Fatal(err)
}
return responseBody, err
}
func main() {
cfg, err := NewConfig("config.json")
if err != nil {
panic(err)
}
parcelsAppTracker := NewParcelsAppTracker(cfg.Trackers.ParcelsApp)
resultsChan, _ := parcelsAppTracker.TrackParcels(cfg.TrackingCodes)
fmt.Print("[")
count := len(cfg.TrackingCodes)
i := 0
for res := range resultsChan {
fmt.Print(res)
if i < count-1 {
fmt.Print(",")
}
i++
}
fmt.Print("]")
}
func every(
ctx context.Context,
cancel context.CancelFunc,
timeout time.Duration,
f func(),
) chan struct{} {
done := make(chan struct{})
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
defer func() { done <- struct{}{} }()
f()
if timeout == 0 {
return
}
for {
select {
case <-sigs:
cancel()
return
case <-time.After(timeout):
f()
continue
}
}
}()
return done
}