-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (75 loc) · 2.55 KB
/
index.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
72
73
74
75
76
77
78
79
80
const rp = require('request-promise');
const fs = require('fs');
const json2csv = require('json2csv');
const co = require('co');
const _ = require('lodash');
const clui = require('clui');
const Progress = clui.Progress;
function initRequest(first, after) {
return new Promise((resolve, reject) => {
const url = 'https://neotracker.io/graphql';
const options = {
method: 'POST',
uri: url,
body: [{
"id": "25",
"variables": {
"first": first,
"after": after
}
}],
json: true // Automatically stringifies the body to JSON
}
rp(options)
.then(function (parsedBody) {
// POST succeeded...
if (first === 0 && after === 0) {
const count = parsedBody[0] && parsedBody[0].data && parsedBody[0].data.addresses ? parsedBody[0].data.addresses.count : 0;
resolve(count);
}
const edges = parsedBody[0] && parsedBody[0].data && parsedBody[0].data.addresses && parsedBody[0].data.addresses.edges && parsedBody[0].data.addresses.edges.length ? parsedBody[0].data.addresses.edges : [];
resolve(edges);
})
.catch(function (err) {
// POST failed...
console.log(err);
reject(err)
});
})
}
function* init() {
try {
let data = [];
const count = yield initRequest(0, 0);
const iterations = Math.ceil(count / 9999);
let thisProgressBar = new Progress(iterations);
for (var i = 0; i < iterations; i++) {
console.log(thisProgressBar.update(i, iterations));
const edges = yield initRequest(9999, 9999 * i);
data = [...data, ...edges];
}
const fields = ['node.hash', 'node.confirmed_coins.edges[0].node.value']
let csv = json2csv({
data: data,
fields: fields
});
const filename = `file${Date.now()}.csv`;
fs.writeFile(filename, csv, function (err) {
if (err) throw err;
console.log(`file saved with name - ${filename}`);
})
} catch (e) {
console.log(`error in generating file -${e}`);
throw e;
}
}
function handler() {
co(init())
.then(() => {
console.log('done successfully');
})
.catch((err) => {
console.log('error in setting global variables', err);
})
}
handler();