-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeocoder.go
500 lines (463 loc) · 9.86 KB
/
geocoder.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"strings"
"github.com/boltdb/bolt"
"github.com/pmezard/apec/jstruct"
"github.com/pquerna/ffjson/ffjson"
)
var (
QuotaError = errors.New("payment required")
geoCacheBucket = []byte("c")
geoPointBucket = []byte("p")
geoMetaBucket = []byte("m")
geoBuckets = [][]byte{
geoCacheBucket,
geoPointBucket,
geoMetaBucket,
}
geocoderVersion = 2
)
type Location struct {
City string
County string
State string
Country string
PostCode string
Lat float64
Lon float64
}
func (l *Location) String() string {
values := []struct {
Field string
Value string
}{
{"city", l.City},
{"postcode", l.PostCode},
{"county", l.County},
{"state", l.State},
{"country", l.Country},
}
s := ""
written := false
for _, v := range values {
if v.Value == "" {
continue
}
if written {
s += ", "
}
s += fmt.Sprintf("%s: %s", v.Field, v.Value)
written = true
}
return s
}
func buildLocation(loc *jstruct.Location) *Location {
var p *Location
if loc != nil && len(loc.Results) > 0 && loc.Results[0].Geometry != nil {
r := loc.Results[0].Component
g := loc.Results[0].Geometry
p = &Location{
City: r.City,
PostCode: r.PostCode,
County: r.County,
State: r.State,
Country: r.Country,
Lat: g.Lat,
Lon: g.Lon,
}
}
return p
}
type Cache struct {
db *bolt.DB
}
func OpenCache(path string) (*Cache, error) {
exists, err := isFile(path)
if err != nil {
return nil, err
}
db, err := bolt.Open(path, 0666, nil)
if err != nil {
return nil, err
}
err = db.Update(func(tx *bolt.Tx) error {
for _, bucket := range geoBuckets {
_, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return err
}
}
return nil
})
if err != nil {
db.Close()
return nil, err
}
c := &Cache{
db: db,
}
if !exists {
err = c.SetVersion(geocoderVersion)
if err != nil {
c.Close()
return nil, err
}
}
return c, nil
}
func (c *Cache) Close() error {
return c.db.Close()
}
func writeBinaryString(w io.Writer, buf []byte, s string) error {
binary.PutVarint(buf, int64(len(s)))
_, err := w.Write(buf[:binary.MaxVarintLen32])
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
}
func readBinaryString(r io.Reader, buf []byte) (string, error) {
_, err := io.ReadFull(r, buf[:binary.MaxVarintLen32])
if err != nil {
return "", err
}
l64, n := binary.Varint(buf[:binary.MaxVarintLen32])
if n <= 0 {
return "", fmt.Errorf("could not decode string length: %d", n)
}
l := int(l64)
if len(buf) < l {
buf = make([]byte, l)
}
_, err = io.ReadFull(r, buf[:l])
if err != nil {
return "", err
}
return string(buf[:l]), nil
}
func writeBinaryLocation(w io.Writer, pos *Location) (err error) {
checkErr := func(e error) {
if err == nil && e != nil {
err = e
}
}
buf := make([]byte, binary.MaxVarintLen32)
checkErr(writeBinaryString(w, buf, pos.City))
checkErr(writeBinaryString(w, buf, pos.County))
checkErr(writeBinaryString(w, buf, pos.State))
checkErr(writeBinaryString(w, buf, pos.Country))
e := binary.Write(w, binary.LittleEndian,
&struct{ Lat, Lon float64 }{Lat: pos.Lat, Lon: pos.Lon})
if err == nil {
err = e
}
return
}
func readBinaryLocation(r io.Reader) (loc *Location, err error) {
checkErr := func(s string, e error) string {
if err == nil && e != nil {
err = e
}
return s
}
buf := make([]byte, binary.MaxVarintLen32)
loc = &Location{}
loc.City = checkErr(readBinaryString(r, buf))
loc.County = checkErr(readBinaryString(r, buf))
loc.State = checkErr(readBinaryString(r, buf))
loc.Country = checkErr(readBinaryString(r, buf))
pos := &struct{ Lat, Lon float64 }{}
e := binary.Read(r, binary.LittleEndian, pos)
if err == nil {
err = e
}
loc.Lat = pos.Lat
loc.Lon = pos.Lon
return
}
func (c *Cache) Put(key string, data []byte, pos *Location) error {
return c.db.Update(func(tx *bolt.Tx) error {
k := []byte(key)
err := tx.Bucket(geoCacheBucket).Put(k, data)
if err != nil {
return err
}
w := bytes.NewBuffer(nil)
if pos != nil {
err = writeBinaryLocation(w, pos)
if err != nil {
return err
}
}
return tx.Bucket(geoPointBucket).Put(k, w.Bytes())
})
}
func (c *Cache) Get(key string) ([]byte, error) {
var data []byte
err := c.db.View(func(tx *bolt.Tx) error {
data = tx.Bucket(geoCacheBucket).Get([]byte(key))
return nil
})
return data, err
}
func (c *Cache) GetLocation(key string) (*Location, bool, error) {
var p *Location
found := false
err := c.db.View(func(tx *bolt.Tx) error {
data := tx.Bucket(geoPointBucket).Get([]byte(key))
found = data != nil
if len(data) == 0 {
return nil
}
point, err := readBinaryLocation(bytes.NewBuffer(data))
if err != nil {
return err
}
p = point
return nil
})
return p, found, err
}
func (c *Cache) List() ([]string, error) {
keys := []string{}
err := c.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(geoCacheBucket)
size := bucket.Stats().KeyN
keys = make([]string, 0, size)
return bucket.ForEach(func(k, v []byte) error {
keys = append(keys, string(k))
return nil
})
})
return keys, err
}
func (c *Cache) Version() (int, error) {
version := 0
err := c.db.View(func(tx *bolt.Tx) error {
data := tx.Bucket(geoMetaBucket).Get([]byte("version"))
if data != nil {
v, n := binary.Varint(data)
if n > 0 {
version = int(v)
}
}
return nil
})
return version, err
}
func (c *Cache) SetVersion(version int) error {
return c.db.Update(func(tx *bolt.Tx) error {
buf := make([]byte, 10)
n := binary.PutVarint(buf, int64(version))
return tx.Bucket(geoMetaBucket).Put([]byte("version"), buf[:n])
})
}
type Geocoder struct {
key string
cache *Cache
}
func NewGeocoder(key, cacheDir string) (*Geocoder, error) {
cache, err := OpenCache(cacheDir)
if err != nil {
return nil, err
}
defer func() {
if cache != nil {
cache.Close()
}
}()
version, err := cache.Version()
if err != nil {
return nil, err
}
if version != geocoderVersion {
return nil, fmt.Errorf("please upgrade geocoder cache from %d to %d",
version, geocoderVersion)
}
g := &Geocoder{
key: key,
cache: cache,
}
cache = nil
return g, nil
}
func NewOldGeocoder(key, cacheDir string) (*Geocoder, error) {
cache, err := OpenCache(cacheDir)
if err != nil {
return nil, err
}
defer func() {
if cache != nil {
cache.Close()
}
}()
version, err := cache.Version()
if err != nil {
return nil, err
}
if version != geocoderVersion {
return nil, fmt.Errorf("please upgrade geocoder cache from %d to %d",
version, geocoderVersion)
}
g := &Geocoder{
key: key,
cache: cache,
}
cache = nil
return g, nil
}
func (g *Geocoder) Close() error {
return g.cache.Close()
}
func makeKeyAndCountryCode(q, code string) (string, string) {
code = strings.ToLower(code)
if code == "" {
code = "unk"
}
return q + "-" + code, code
}
func (g *Geocoder) geocodeFromCache(q, countryCode string) (*jstruct.Location, error) {
key, countryCode := makeKeyAndCountryCode(q, countryCode)
data, err := g.cache.Get(key)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, nil
}
res := &jstruct.Location{}
err = ffjson.Unmarshal(data, res)
res.Cached = true
return res, err
}
func (g *Geocoder) GetCachedLocation(q, countryCode string) (*Location, bool, error) {
key, _ := makeKeyAndCountryCode(q, countryCode)
return g.cache.GetLocation(key)
}
func (g *Geocoder) Geocode(q, countryCode string, offline bool) (
*jstruct.Location, error) {
res, err := g.geocodeFromCache(q, countryCode)
if err != nil || res != nil || offline {
return res, err
}
r, err := g.rawGeocode(q, countryCode)
if err != nil {
return nil, err
}
defer r.Close()
data, err := ioutil.ReadAll(&io.LimitedReader{
R: r,
N: 4 * 1024 * 1024,
})
if err != nil {
return nil, err
}
res = &jstruct.Location{}
err = ffjson.Unmarshal(data, res)
if err != nil {
return nil, err
}
key, _ := makeKeyAndCountryCode(q, countryCode)
err = g.cache.Put(key, data, buildLocation(res))
if err != nil {
return nil, err
}
return res, err
}
func (g *Geocoder) rawGeocode(q, countryCode string) (io.ReadCloser, error) {
u := fmt.Sprintf("http://api.opencagedata.com/geocode/v1/json?q=%s&key=%s",
url.QueryEscape(q), url.QueryEscape(g.key))
if countryCode != "" {
u += "&countrycode=" + url.QueryEscape(countryCode)
}
rsp, err := http.Get(u)
if err != nil {
return nil, err
}
if rsp.StatusCode != 200 {
rsp.Body.Close()
if rsp.StatusCode == 402 {
return nil, QuotaError
}
return nil, fmt.Errorf("geocoding failed with %s", rsp.Status)
}
return rsp.Body, nil
}
func shuffle(values []string) {
if len(values) < 2 {
return
}
for i := len(values) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
values[i], values[j] = values[j], values[i]
}
}
var (
geocodeCmd = app.Command("geocode", "geocode offers without location")
)
func geocode(cfg *Config) error {
key := cfg.GeocodingKey()
if key == "" {
return fmt.Errorf("geocoding key is not set, please configure APEC_GEOCODING_KEY")
}
geocoder, err := NewGeocoder(key, cfg.Geocoder())
if err != nil {
return err
}
defer geocoder.Close()
store, err := OpenStore(cfg.Store())
if err != nil {
return err
}
defer store.Close()
ids, err := store.List()
if err != nil {
return err
}
shuffle(ids)
for _, id := range ids {
loc, _, err := store.GetLocation(id)
if err != nil {
return err
}
if loc != nil {
continue
}
offer, err := getStoreOffer(store, id)
if err != nil {
return err
}
if offer == nil {
continue
}
pos, _, off, err := geocodeOffer(geocoder, offer.Location, false, 100)
if err != nil {
return err
}
if pos == nil {
continue
}
if off {
break
}
err = store.PutLocation(id, pos, offer.Date)
if err != nil {
return err
}
}
err = store.Close()
if err != nil {
return err
}
return geocoder.Close()
}