-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (65 loc) · 2.45 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
81
82
83
84
85
86
87
const Axios = require('axios');
const fs = require('fs-extra');
const path = require('path');
const INTERVAL = 5000; // 5 seconds between API calls
const COUNT = 20; // fetch 20 at a time. A value greater than this doesn't appear to do anything.
const PROGRESS_FILE = path.join(__dirname, 'progress.json');
const RESULTS_FILE = path.join(__dirname, 'results.json');
const loadJSON = async filename => {
try {
const file = await fs.readFile(filename);
return JSON.parse(file);
} catch (error) {
console.log(`${filename} doesn't exist or is not valid, skipping.`);
}
return null;
};
const saveJSON = async (filename, data) => {
await fs.writeFile(
filename,
JSON.stringify(data),
);
};
const run = async () => {
if (!process.env.DOMAIN) throw new Error(`You need to specify the DOMAIN that your group lives under in environment.`);
if (!process.env.GROUP_ID) throw new Error(`You need to specify a GROUP_ID in environment. Get this from the Mobilize address bar.`);
if (!process.env.COOKIE) throw new Error(`You need to specify a COOKIE in environment. This can be found in your session data in the browser. See README for formatting.`);
console.log(`Reading any existing information...`);
let results = await loadJSON(RESULTS_FILE);
// initialize results array on first load.
if (!results) results = [];
const progress = await loadJSON(PROGRESS_FILE);
// total users starts at infinity so the loop can begin.
let total_users = progress ? progress.total_users : Infinity;
let offset = progress ? progress.offset : 0;
while (offset < total_users) {
console.log(`⏬ ${offset}/${total_users}`);
const url = `https://${process.env.DOMAIN}.mobilize.io/members.json?offset=${offset}&count=${COUNT}&search=&group_id=${process.env.GROUP_ID}&filters=%7B%7D`;
const { data } = await Axios.request({
url,
headers: {
Cookie: process.env.COOKIE,
},
});
// append fetched users to results json
results = results.concat(data.users);
await saveJSON(
RESULTS_FILE,
results,
);
// update total users so the loop can continue with the next batch.
total_users = data.total_users;
offset += COUNT;
// save progress to json in case of an abort.
await saveJSON(
PROGRESS_FILE,
{ total_users, offset },
);
// wait a bit to be respectful to the Mobilize API
await new Promise(resolve => setTimeout(resolve, INTERVAL));
}
};
run().then(process.exit).catch(error => {
console.log(error);
process.exit(1);
});