-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
89 lines (70 loc) · 1.99 KB
/
service.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const fs = require('node:fs');
const dicts = {
ml: { datFile: 'grc-ml-ids.dat',
url: 'https://repos1.alpheios.net/exist/rest/db/xq/lexi-get.xq?lx=ml&lg=grc&out=html&n='
}
}
const parseDatFile = (rawDatFile) => {
const vocLines = rawDatFile.split('\n')
return vocLines.map((line => line.split('|')[1]))
}
const readDatFile = (datFile) => {
try {
const result = fs.readFileSync(datFile, 'utf8')
return result
} catch(err) {
console.error(err);
}
}
const httpGet = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.get(url, (resp) => {
let chunks = [];
resp.on('data', (chunk) => {
chunks.push(chunk);
});
resp.on('end', () => {
resolve(Buffer.concat(chunks));
});
}).on("error", (err) => {
reject(err);
});
});
}
const saveToFile = async (data, filename) => {
try {
fs.writeFileSync(filename, data, 'utf8');
console.log('Written - ', filename)
} catch (err) {
console.error(err);
}
}
const uploadFromRemote = async (index, url) => {
const wordUrl = `${url}${index}`
return httpGet(wordUrl)
}
const doUpload = async (vocName) => {
const dictData = dicts[vocName]
const rawDatFile = readDatFile(dictData.datFile)
const vocIndexes = parseDatFile(rawDatFile)
let i = 0
for (let index of vocIndexes) {
const fileName = `ml-xml\\${index.trim()}.xml`
if (fs.existsSync(fileName)) {
console.log('exists - ', fileName)
continue
}
const buf = await uploadFromRemote(index, dictData.url)
saveToFile(buf.toString('utf-8'), fileName)
await new Promise(resolve => setTimeout(resolve, 100))
// if (i===5) break
i++
}
}
doUpload('ml')