Skip to content

Commit 81a7aea

Browse files
committed
re-enabled url fetching support
1 parent 61b3f8f commit 81a7aea

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

  • services/speech_to_text

services/speech_to_text/v1.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
**/
1616

1717
module.exports = function (RED) {
18+
var request = require('request');
1819
var cfenv = require('cfenv');
1920
var temp = require('temp');
2021
var url = require('url');
@@ -83,14 +84,31 @@ module.exports = function (RED) {
8384

8485
// Function that is syncing up the asynchronous nature of the stream
8586
// so that the full file can be sent to the API.
86-
function stream_buffer(file, contents, cb) {
87+
var stream_buffer = function(file, contents, cb) {
8788
fs.writeFile(file, contents, function (err) {
8889
if (err) throw err;
8990
cb(fileType(contents).ext);
9091
});
9192
};
9293

9394

95+
// Function that is syncing up the asynchronous nature of the stream
96+
// so that the full file can be sent to the API.
97+
var stream_url = function(file, url, cb) {
98+
var wstream = fs.createWriteStream(file);
99+
100+
wstream.on('finish', function () {
101+
fs.readFile(file, function (err, buf) {
102+
if (err) {
103+
throw err;
104+
}
105+
cb(fileType(buf).ext)
106+
});
107+
});
108+
109+
request(url).pipe(wstream);
110+
};
111+
94112
// This is the Speech to Text Node
95113

96114
function Node (config) {
@@ -116,11 +134,14 @@ module.exports = function (RED) {
116134
if (r) {
117135
if (r.length && r[0].alternatives.length) {
118136
var index = r[0].alternatives.length - 1;
119-
msg.transcription = r[0].alternatives[index].transcript;
120137
msg.fullresult = r;
121138
}
139+
msg.transcription = '';
122140
r.forEach(function(a){
123141
// console.log(a.alternatives);
142+
a.alternatives.forEach(function(t){
143+
msg.transcription += t.transcript;
144+
})
124145
});
125146
}
126147
node.send(msg);
@@ -220,7 +241,7 @@ module.exports = function (RED) {
220241

221242
// This check is repeated just before the call to the service, but
222243
// its also performed here as a double check.
223-
if (!msg.payload instanceof Buffer) {
244+
if (!(msg.payload instanceof Buffer)) {
224245
if (typeof msg.payload === 'string' && !urlCheck(msg.payload)) {
225246
var message = 'Invalid URL.';
226247

@@ -259,20 +280,30 @@ module.exports = function (RED) {
259280

260281
performAction(audio, format, actionComplete, temp.cleanup);
261282
});
262-
263283
});
264284
} else if (urlCheck(msg.payload)) {
265-
//params['url'] = msg.payload;
266-
//performAction(params, feature, actionComplete);
285+
temp.open({suffix: '.audio'}, function(err, info){
286+
if (err) {
287+
this.status({fill:'red', shape:'ring', text:'unable to open url audio stream'});
288+
var message ='Node has been unable to open the url audio stream';
289+
node.error(message, msg);
290+
return;
291+
}
292+
293+
stream_url(info.path, msg.payload, function (format) {
294+
var audio = fs.createReadStream(info.path);
295+
296+
performAction(audio, format, actionComplete, temp.cleanup);
297+
});
298+
});
267299
} else {
268300
this.status({fill:'red', shape:'ring', text:'payload is invalid'});
269301
var message ='Payload must be either an audio buffer or a string representing a url';
270302
node.error(message, msg);
271303
return;
272304
}
273305

274-
//msg.transcription = 'Work is in progres ... 004';
275-
//node.send(msg);
306+
276307
});
277308
}
278309

@@ -282,4 +313,4 @@ module.exports = function (RED) {
282313
password: {type:'password'}
283314
}
284315
});
285-
};
316+
};

0 commit comments

Comments
 (0)