const fs = require('fs');
const https = require('https');
const crypto = require('crypto');
const authUtils = require('./utils/auth-utils');
const serverConfig = {
protocol: 'https:',
hostname: 'translate.rozetta-api.io',
port: 443
};
const authConfig = {
accessKey: 'ACCESS_KEY',
secretKey: 'SECRET_KEY',
nonce: Date.now().toString()
};
const data = {
text: 'こんにちは、いい天気ですね。',
targetLang: 'ja',
contractId: 'your contractId'
};
const speechFile = 'speech.wav';
const getSpeechResult = (serverConfig, authConfig, data) => {
const path = '/api/v1/translate/tts';
const signature = authUtils.generateSignature(
path,
authConfig.secretKey,
authConfig.nonce
);
const requestOptions = {
protocol: serverConfig.protocol,
host: serverConfig.hostname,
port: serverConfig.port,
method: 'POST',
path,
headers: {
accessKey: authConfig.accessKey,
signature,
nonce: authConfig.nonce,
'Content-Type': 'application/json'
}
};
return new Promise((resolve, reject) => {
const request = https.request(requestOptions, (response) => {
const writeStream = fs.createWriteStream(speechFile);
response.pipe(writeStream);
response.on('close', () => {
resolve();
});
});
request.on('error', (error) => {
reject(error);
});
request.write(JSON.stringify(data));
request.end();
});
};
const main = async () => {
try {
await getSpeechResult(serverConfig, authConfig, data);
console.log('Speech file downloaded to:');
console.log(speechFile);
} catch (error) {
console.error(error);
}
};
main();