-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.42 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
const GoogleSpreadsheet = require('google-spreadsheet');
const {promisify} = require('util');
const fetch = require('node-fetch');
const credentials = require(`./service-credentials`);
const IO = require(`./in_out`);
(async () => {
const container = new GoogleSpreadsheet(IO.spreadsheetId);
await promisify(container.useServiceAccountAuth)(credentials);
const info = await promisify(container.getInfo)();
const sheet = info.worksheets[0];
const rows = await promisify(sheet.getRows)({
offset: 1,
limit: 1000,
orderby: 'key',
});
const nonEmptyRows = [];
rows.some((row) => {
if (!row.key && !row.pl && !row.en) {
return true;
}
const {pl, key, en} = row;
nonEmptyRows.push({
key, pl, en
});
return false;
});
await delayedExecution(nonEmptyRows);
})();
const delayedExecution = async (rows) => {
if (!rows.length) {
return Promise.resolve();
}
const row = rows.pop();
await fetchPost(row);
await new Promise((res) => setTimeout(() => res(), (Math.random() * 100) + 100));
return await delayedExecution(rows);
};
const fetchPost = (row) => {
return fetch(IO.apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"word": row.key,
"translations":
{
"pl": row.pl,
"en": row.en,
}
})
}).catch((e) => {
console.error('ERROR', e);
});
};