47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { TTSModule, TTSResponse } from '../tts';
|
|
|
|
import * as https from 'https';
|
|
|
|
import GOOGLE_TTS_VOICES from './google_voices.json';
|
|
const GOOGLE_TTS_ENDPOINT = 'translate.google.com';
|
|
const USER_AGENT =
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3';
|
|
|
|
const ttsGoogle: TTSModule = {
|
|
name: 'Google',
|
|
getVoices: async (): Promise<string[]> => GOOGLE_TTS_VOICES.voices,
|
|
|
|
generate: async (voice: string, text: string): Promise<TTSResponse> => {
|
|
const query = new URLSearchParams({
|
|
ie: 'UTF-8',
|
|
q: text,
|
|
tl: voice,
|
|
client: 'tw-ob'
|
|
});
|
|
|
|
const options: https.RequestOptions = {
|
|
hostname: GOOGLE_TTS_ENDPOINT,
|
|
path: `/translate_tts?${query.toString()}`,
|
|
method: 'GET',
|
|
headers: {
|
|
'User-Agent': USER_AGENT
|
|
}
|
|
};
|
|
|
|
return new Promise((resolve) => {
|
|
const req = https.get(options, (res) => {
|
|
const chunks: Buffer[] = [];
|
|
res.on('data', (chunk) => chunks.push(chunk));
|
|
res.on('end', () => resolve({ data: Buffer.concat(chunks) }));
|
|
});
|
|
|
|
req.on('error', (err) => resolve({ error: err.message }));
|
|
req.on('timeout', () => {
|
|
req.destroy();
|
|
resolve({ error: 'timed out' });
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
export default ttsGoogle; |