-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataToLocations.ts
38 lines (32 loc) · 1.01 KB
/
dataToLocations.ts
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
import fs from 'fs'
import geocode from './lib/geocode'
// @ts-expect-error ts-migrate(2532) FIXME: Object is possibly 'undefined'.
import data from './data.json'
// @ts-expect-error ts-migrate(2532) FIXME: Object is possibly 'undefined'.
import locations from './locations.json'
const promises: any = []
data.forEach((elem: any) => {
if (!locations[elem.locations]) {
process.stdout.write(`requesting location: ${elem.locations}\n`)
const p = geocode(`${elem.locations} San Francisco Bay Area, CA, USA`)
.then((res: any) => {
locations[elem.locations] = res
return res
})
.catch((reason: any) => {
process.stderr.write(`${reason}\n`)
})
promises.push(p)
} else {
process.stdout.write('HIT!\n')
}
})
Promise.all(promises).then(
(results) => {
process.stdout.write(`${results.length} item(s) received\n`)
fs.writeFileSync('./locations_data.json', JSON.stringify(locations))
},
(reason) => {
process.stderr.write(`Error: ${reason}\n`)
},
)