-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.js
More file actions
203 lines (184 loc) · 6.13 KB
/
export.js
File metadata and controls
203 lines (184 loc) · 6.13 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const fs = require('fs');
const path = require('path');
// const repl = require('repl');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const axios = require('axios');
const axiosRetry = require('axios-retry');
const cliProgress = require('cli-progress');
const colors = require('ansi-colors');
// Lever occasionally throws rate limit errors despite our best efforts to
// request at a threshold below their limiting. Add retries that also pause
// request queue processing to give Lever a momentary break before resuming.
axiosRetry(axios, {
retries: 5,
retryCondition: (error) => {
if (error?.response?.status === 429) {
pauseLaunchingRequests(5000);
return true;
}
return axiosRetry.isNetworkOrIdempotentRequestError(error);
},
retryDelay: (retryCount) => retryCount * retryCount * 1000,
});
const API_ROOT = 'https://api.lever.co/v1'
const REQUEST_PER_SECOND = 10;
const DATA_DIRECTORY = path.join(__dirname, 'data');
const OUTPUT_JSON_FILE = path.join(DATA_DIRECTORY, 'lever-export.json');
const stats = {
total: 0,
completed: 0,
};
const progressBar = new cliProgress.SingleBar({
format: 'Lever Data Export |' + colors.cyan('{bar}') + '| {percentage}% || {value}/{total} entries || {duration_formatted}, ETA: {eta_formatted}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
function addToStatsTotal(count) {
stats.total += count;
progressBar.setTotal(stats.total);
}
function addToStatsCompleted(count) {
stats.completed += count;
progressBar.update(stats.completed);
}
const launchQueue = [];
let launchAllowed = true;
let launchPauseTimeoutId;
function startLaunchingRequests() {
// Start processing the request queue
setInterval(() => {
if (launchAllowed && launchQueue.length > 0) {
launchQueue.shift().start();
}
}, 1000 / REQUEST_PER_SECOND);
}
function pauseLaunchingRequests(delayMs) {
launchAllowed = false;
// If the launch was already paused, then clear the prior delay so it doesn't
// interfere with this most recent one.
if (launchPauseTimeoutId) {
clearTimeout(launchPauseTimeoutId);
}
launchPauseTimeoutId = setTimeout(() => {
launchAllowed = true;
launchPauseTimeoutId = undefined;
}, delayMs);
}
async function queueRequest(path) {
return new Promise((resolve, reject) => {
launchQueue.push({
start: () => { resolve(); }
});
}).then(() => {;
return axios.get(
`https://api.lever.co/v1${path}`,
{ auth: { username: process.env.LEVER_API_KEY, password: '' }
}).catch(error => console.log(error));;
});
}
// Request remaining pages, optionally calling the observe method each time
// a page is successfully fetched.
async function requestRemainingPages(pathWithLimitParam, observePageData, offsetParam = '') {
let resources = [];
return queueRequest(`${pathWithLimitParam}${offsetParam}`)
.then((response) => {
resources = response.data.data;
if (observePageData) {
observePageData(resources);
}
return response.data.hasNext
? requestRemainingPages(pathWithLimitParam, observePageData, `&offset=${response.data.next}`)
: [];
})
.then((remainingResources) => {
return resources.concat(remainingResources);
});
}
async function populateFullOpportunity(opportunity) {
let fullOpportunity = { ...opportunity };
const fetchPromises = [
requestRemainingPages(`/opportunities/${opportunity.id}/feedback?limit=100`)
.then((feedback) => {
fullOpportunity = { ...fullOpportunity, feedback };
}),
requestRemainingPages(`/opportunities/${opportunity.id}/panels?limit=100`)
.then((panels) => {
fullOpportunity = { ...fullOpportunity, panels };
}),
requestRemainingPages(`/opportunities/${opportunity.id}/notes?limit=100`)
.then((notes) => {
fullOpportunity = { ...fullOpportunity, notes };
}),
requestRemainingPages(`/opportunities/${opportunity.id}/offers?expand=creator&limit=100`)
.then((offers) => {
fullOpportunity = { ...fullOpportunity, offers };
}),
requestRemainingPages(`/opportunities/${opportunity.id}/forms?limit=100`)
.then((forms) => {
fullOpportunity = { ...fullOpportunity, forms };
}),
requestRemainingPages(`/opportunities/${opportunity.id}/files`)
.then((files) => {
fullOpportunity = { ...fullOpportunity, files };
}),
requestRemainingPages(`/opportunities/${opportunity.id}/resumes`)
.then((resumes) => {
fullOpportunity = { ...fullOpportunity, resumes };
}),
];
if (opportunity.archived && opportunity.archived.reason) {
fetchPromises.push(
queueRequest(`/archive_reasons/${opportunity.archived.reason}`)
.then((response) => {
fullOpportunity = {
...fullOpportunity,
archivedReason: response.data.data
};
})
);
}
if (opportunity.applications && opportunity.applications.length > 0) {
fetchPromises.push(
queueRequest(`/opportunities/${opportunity.id}/applications/${opportunity.applications[0].id}?expand=posting`)
.then((response) => {
fullOpportunity = {
...fullOpportunity,
applications: [
response.data.data
]
};
})
);
}
return Promise.all(fetchPromises).then(() => fullOpportunity);
}
async function main() {
await exec(`mkdir -p ${DATA_DIRECTORY}`);
progressBar.start(0, 0);
requestRemainingPages(
'/opportunities?expand=applications&expand=stage&expand=owner&expand=sourcedBy&expand=contact&expand=followers&limit=100',
(page) => {
addToStatsTotal(page.length);
}
).then((partialOpportunities) => {
return Promise.all(
partialOpportunities.map(o => {
return populateFullOpportunity(o).then(o => {
addToStatsCompleted(1);
return o;
});
})
);
}).then((opportunities) => {
progressBar.stop();
fs.writeFileSync(
OUTPUT_JSON_FILE,
JSON.stringify(opportunities, undefined, 2)
);
process.exit(0);
});
startLaunchingRequests();
}
main();