-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
132 lines (117 loc) · 3.83 KB
/
node_helper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Module : MMM-GoogleTTS
//
"use strict"
const fs = require("fs")
const path = require("path")
const exec = require("child_process").exec
const textToSpeech = require("@google-cloud/text-to-speech")
const NodeHelper = require("node_helper")
const getToday = () => {
let date = new Date()
return date.getFullYear() + String(date.getMonth() + 1).padStart(2, "0") + String(date.getDate()).padStart(2, "0")
}
module.exports = NodeHelper.create({
start: function() {
this.config = {}
this.tmpFile = ""
this.countFile = ""
this.today = getToday()
this.count = 0
this.client = null
process.env.GOOGLE_APPLICATION_CREDENTIALS = path.resolve(__dirname, "account.json")
},
initialize: function(config) {
console.log("[GGLTTS] Initialized.")
this.config = config
this.credentials = path.resolve(__dirname, this.config.credentialPath)
this.client = new textToSpeech.TextToSpeechClient({keyFilename:this.credentials})
this.tmpFile = path.resolve(__dirname, "temp_output")
this.countFile = path.resolve(__dirname, "api_count.json")
var obj = JSON.parse(fs.readFileSync(this.countFile), "utf8")
if (obj.hasOwnProperty(this.today)) {
this.count = obj[this.today]
console.log("[GGLTTS] Today's quota used:", this.count)
} else {
var data = {}
data[this.today] = this.count
fs.writeFile(this.countFile, JSON.stringify(data), "utf8", ()=>{
console.log("[GGLTTS] Today's quota initialized")
})
}
},
socketNotificationReceived: function(noti, payload) {
switch(noti) {
case "CONFIG":
this.initialize(payload)
break
case "SAY":
this.say(payload)
break
}
},
say: function(obj) {
if (obj.content) {
if (this.config.dailyCharLimit <= this.count + obj.content.length) {
console.log("[GGLTTS] Today's quota limited:", this.count, this.config.dailyCharLimit)
this.sendSocketNotification("SAY_LIMIT_ERROR", { obj })
return
}
}
var request = {
input: {},
voice: {},
audioConfig: {}
}
if (obj.type === "text") {
request.input.text = obj.content
} else {
request.input.ssml = obj.content
}
request.voice.languageCode = obj.languageCode
request.voice.ssmlGender = obj.ssmlGender
request.voice.name = obj.voiceName
request.audioConfig.audioEncoding = this.config.audioEncoding
var command = this.config.playCommand.replace("%OUTPUTFILE%", this.tmpFile)
this.client.synthesizeSpeech(request, (error, response) => {
if (error) {
console.log("[GGLTTS] Synthesize Error:", error)
this.sendSocketNotification("SAY_ERROR", { obj, error })
return
}
var quota = {}
if (this.today === getToday()) {
this.count = this.count + obj.content.length
quota[this.today] = this.count
} else {
this.today = getToday()
quota[this.today] = obj.content.length
}
fs.writeFile(this.countFile, JSON.stringify(quota), "utf8", (e)=>{
if (e) {
console.log("[GGLTTS] Quota file write error:", e)
} else {
console.log("[GGLTTS] Today's quota used:", this.count)
}
})
fs.writeFile(this.tmpFile, response.audioContent, "binary", (e) => {
if (e) {
console.log("[GGLTTS] File Error:", e)
this.sendSocketNotification("SAY_ERROR", { obj, error:e })
return
}
this.sendSocketNotification("SAY_STARTING")
exec(command, (er)=>{
if (er) {
console.log("[GGLTTS] Playing Sound Error:", er)
this.sendSocketNotification("SAY_ERROR", { obj, error:er })
}
fs.unlink(this.tmpFile, ()=>{
this.sendSocketNotification("SAY_ENDING", obj)
})
return
})
})
})
},
})