-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (79 loc) · 2.03 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
88
89
90
91
92
import { fetchFollowersAndCursor } from "./fetch.js";
import { appendDataToCSV, delay, parseList } from "./util.js";
import { addRows } from "./post-data.js";
import { args } from "./args.js";
async function main({
initialCursor,
userId,
fetchAll,
fetchCount,
uploadThreshold,
minDelay,
csvFilename,
fieldsToSave,
randomOffset,
}) {
const userList = [];
const totalFetchCount = fetchAll ? 10000000 : fetchCount;
let updatedCursor = initialCursor;
let totalFetched = 0;
while (totalFetched < totalFetchCount) {
const { cursor, list } = await fetchFollowersAndCursor(
updatedCursor,
100,
userId
);
updatedCursor = cursor;
userList.push(...list);
totalFetched += list.length;
console.log(`Fetched ${totalFetched} users...`);
if (userList.length >= uploadThreshold) {
console.log(
`\nAdding to ${csvFilename || "google sheets"}...\nCursor: ${cursor}\n`
);
const parsedList = parseList(userList, fieldsToSave);
csvFilename
? appendDataToCSV(parsedList, csvFilename)
: addRows(parsedList);
userList.splice(0, userList.length);
}
if (!cursor || list.length === 0 || totalFetched >= totalFetchCount) break;
const delayInMs = Math.floor(Math.random() * (randomOffset + 1)) + minDelay;
await delay(delayInMs);
}
if (userList.length) {
console.log(
`\nAdding to ${
csvFilename || "google sheets"
}...\nCursor: ${updatedCursor}\n`
);
const parsedList = parseList(userList, fieldsToSave);
csvFilename
? appendDataToCSV(parsedList, csvFilename)
: addRows(parsedList);
}
console.log(`Done fetching ${totalFetched} users.`);
}
const {
cursor,
userId,
fetchAll,
fetchCount,
uploadThreshold,
minDelay,
saveToGoogleSheets,
csvFilename,
fieldsToSave,
randomOffset,
} = args;
main({
initialCursor: cursor,
userId,
fetchAll,
fetchCount,
uploadThreshold,
minDelay,
csvFilename: saveToGoogleSheets ? null : csvFilename,
fieldsToSave,
randomOffset,
});