-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-namedays.ts
70 lines (61 loc) · 1.38 KB
/
generate-namedays.ts
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
const https = require('https');
const fs = require('fs');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const url = 'https://hu.wikipedia.org/w/api.php?action=parse&page=Magyar_n%C3%A9vnapok_list%C3%A1ja_d%C3%A1tum_szerint&prop=text&format=json';
function transform(items) {
return items.map(
(month) => {
const days = [...month.querySelectorAll('ol li')];
return days.map(
(day) => {
const names = (
day.textContent
.split(',')
.map(
name => (
name
.trim()
.replace('*', '')
.replace('((', '')
.replace('))', '')
.split(' ')
.filter(Boolean)
.flat()
)
)
.filter(Boolean)
.flat()
);
return names;
},
);
},
);
}
https.get(url, (res) => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const { parse } = JSON.parse(rawData);
const dom = new JSDOM(parse.text['*']);
const monthTrs = (
[
...dom.window.document.querySelectorAll('table tbody tr td'),
]
.filter(td => !!td.querySelector('ol'))
);
const result = transform(monthTrs);
fs.writeFile('src/namedays.json', JSON.stringify(result, undefined, '\t'), () => {
console.log('Saved src/namedays.json');
});
}
catch(e) {
console.log(e.message);
}
});
});