-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
64 lines (57 loc) · 2.01 KB
/
update.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
const fs = require('fs');
const fetch = require('node-fetch');
function formatDate (date, format) {
format = format.replace(/yyyy/g, date.getFullYear());
format = format.replace(/MM/g, ('0' + (date.getMonth() + 1)).slice(-2));
format = format.replace(/dd/g, ('0' + date.getDate()).slice(-2));
format = format.replace(/HH/g, ('0' + date.getHours()).slice(-2));
format = format.replace(/mm/g, ('0' + date.getMinutes()).slice(-2));
format = format.replace(/ss/g, ('0' + date.getSeconds()).slice(-2));
format = format.replace(/SSS/g, ('00' + date.getMilliseconds()).slice(-3));
return format;
}
async function fetchWAVdata(text, id, name) {
//console.log(`#fetchWAVdata: text: ${text}`);
//console.log(`#fetchWAVdata: id: ${id}`);
//console.log(`#fetchWAVdata: name: ${name}`);
const response = await fetch("http://153.126.153.39:3000/texttospeach", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text, id })
});
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
fs.writeFileSync(name, buffer);
}
async function updateWAV(cfile) {
var ncache = [];
var raw = fs.readFileSync(cfile);
var cache = JSON.parse(raw);
for (var i = 0; i < cache.length; i++) {
var elm = cache[i];
//console.log(old);
if (!elm.hasOwnProperty('txt')) continue;
if (!elm.hasOwnProperty('id')) elm.id = 4;
//if (!elm.hasOwnProperty('wav')) {
const date = new Date();
elm.wav = "cache/"+formatDate(date, 'yyyyMMddHHmmssSSS')+".wav";
//}
console.log(i+": "+elm.txt+" "+elm.id);
if (!fs.existsSync(elm.wav)) {
await fetchWAVdata(elm.txt, elm.id, elm.wav);
console.log(i+": "+elm.wav);
}
ncache.push(elm);
}
try {
fs.unlinkSync(cfile+"~");
fs.renameSync(cfile, cfile+"~");
} catch (err) {}
var json = JSON.stringify(ncache, null, 2);
fs.writeFileSync(cfile, json);
}
async function main() {
const cfile = 'config/cache.json';
await updateWAV(cfile);
}
main();