-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiktionaryFetch.js
71 lines (57 loc) · 1.44 KB
/
wiktionaryFetch.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
'use strict';
let axios = require('axios');
let pForever = require('p-forever');
let fs = require('fs');
let path = require('path');
let baseUrl = 'https://en.wiktionary.org/w/api.php';
let categories = [
'Category:English_lemmas',
'Category:English_non-lemma_forms'
];
let params = {
action :'query',
format :'json',
list :'categorymembers',
cmtitle:'Category:English_non-lemma_forms',
cmlimit:500
};
let fileName = Date.now() + '.csv';
let file = fs.createWriteStream(fileName, 'utf8');
let write = (string) => {
return new Promise((res, rej) => {
file.write(string, 'utf8', (err) => {
if(!err){
res();
} else {
rej(err);
}
file.removeAllListeners('error');
});
file.on('error', (err) => {
file.removeAllListeners('error');
rej(err);
});
});
};
let makeString = (words) => {
return words.join('\n') + '\n';
};
let counter = 0;
pForever(() => {
return axios.get(baseUrl, {params})
.then((res) => {
let data = res.data;
if (data.continue){
params.cmcontinue = data.continue.cmcontinue;
let words = data.query.categorymembers.map(x => x.title);
console.log(`Loop ${++counter}, got ${words.length} words, to ${words[words.length - 1]}`);
return write(makeString(words));
} else {
return pForever.end;
}
})
.catch((err) => {
console.log('GOT ERR');
console.log(err);
});
});