-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharvestFromCoordinatesList.js
71 lines (63 loc) · 1.6 KB
/
harvestFromCoordinatesList.js
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
/*
harvest from coordinates list
Reads coordinates list from external file (CSV or JSON)
*/
const fs = require('fs')
const LineByLineReader = require('line-by-line')
const request = require('request-promise-native')
//goSmallOnly()
go()
// !!! flat files only !!!
async function go() {
const lr = new LineByLineReader('missed.csv')
let i = 0
let items = []
lr.on('line', async coordinates => {
i++
const item = {
tool: 'component',
coordinates: coordinates.replace(/\"/g, '')
}
items.push(item)
if (i % 50 === 0) {
lr.pause()
console.log(`sending ${items.length} items`)
await sendHarvestRequest(items)
items = []
lr.resume()
}
}).on('end', async () => {
console.log(`sending final ${items.length} items`)
await sendHarvestRequest(items)
//await sendHarvestRequestAtCrawler(items)
})
}
// !!! small JSON array files only !!!
async function goSmallOnly() {
const coordinates = JSON.parse(
fs
.readFileSync('coordinates.json')
.toString()
.trim()
)
const items = []
for (let i = 0; i < coordinates.length; i++) {
const item = {
tool: 'package',
coordinates: coordinates[i]
}
items.push(item)
}
const chunkSize = 500
for (let i = 0; i < items.length; i += chunkSize) {
console.log('requesting ' + i + ' of ' + items.length)
await sendHarvestRequest(items.slice(i, i + chunkSize))
}
}
async function sendHarvestRequest(payload) {
const response = await request('https://api.clearlydefined.io/harvest', {
method: 'post',
json: payload
})
console.log(response)
}