-
Notifications
You must be signed in to change notification settings - Fork 10
/
siteinfo-updater.js
55 lines (50 loc) · 1.31 KB
/
siteinfo-updater.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
/* eslint-env node */
'use strict';
const http = require('http');
const fs = require('fs');
const DATA_URL = 'http://wedata.net/databases/AutoPagerize/items_all.json';
const FILE = 'siteinfo.json';
const KNOWN_KEYS = [
'url',
'nextLink',
'insertBefore',
'pageElement',
];
console.log('Fetching ' + DATA_URL);
http.get(DATA_URL, r => {
const data = [];
let size = 0;
r.on('data', str => {
data.push(str);
size += str.length;
process.stdout.write('\r' + Math.round(size / 1024) + ' kiB');
});
r.on('end', () => {
const json = sanitize(JSON.parse(data.join('')));
fs.writeFileSync(FILE, JSON.stringify(json, null, ' '));
console.log();
console.log('Done');
});
});
function sanitize(data) {
return (Array.isArray(data) ? data : [])
.map(x => x && x.data && x.data.url && pickKnownKeys(x.data, x.resource_url))
.filter(Boolean)
.sort((a, b) => a.id - b.id);
}
function pickKnownKeys(entry, resourceUrl) {
for (const key of Object.keys(entry)) {
if (!KNOWN_KEYS.includes(key)) {
const newEntry = {};
for (const k of KNOWN_KEYS) {
const v = entry[k];
if (v !== undefined)
newEntry[k] = v;
}
entry = newEntry;
break;
}
}
entry.id = Number(resourceUrl.slice(resourceUrl.lastIndexOf('/') + 1));
return entry;
}