UNPKG

uberduck.js

Version:

unofficial uberduck api, the code is TERRIBLE but it works.

106 lines (82 loc) 2.97 kB
const request = require('request'); const fs = require('fs'); var key, secret = ''; const download = (url, dest, cb) => { const file = fs.createWriteStream(dest); const sendReq = request.get(url); sendReq.on('response', () => sendReq.pipe(file)); file.on('finish', () => file.close(cb)); }; function timeout(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function req(url) { var final = null; request({url: url,method: 'GET',auth: {'user': key,'pass': secret}}, (err,res,b) => final = b ); while(!final) await timeout(20); return final; } async function getLink(uuid) { var finished = false; request({url: uuid,method: 'GET',auth: {'user': key,'pass': secret}}, async (err,res,b) =>{ if (!err && res.statusCode == 200) { while(!JSON.parse(b).path) { const newBody = await req(uuid); await timeout(2000); b = newBody; } finished = JSON.parse(b).path; } else throw new Error('Something happened while requesting to UberDuck, are you sure your key is correct?'); }); while(!finished) await timeout(20); return finished; } module.exports = { setDetails : function (k,s) { key = k; secret = s; }, downloadSpeak: async function(uuid, path) { if(key == '') throw new Error('info.key Cannot be null!'); if(secret == '') throw new Error('info.secret Cannot be null!'); var final = false; const a = await getLink(uuid); download(a, path, () => final = true); while(!final) await timeout(20); return final; }, requestSpeak: async function(voice = 'spongebob', speech = 'hello, world!'){ var finished = false; speech = speech.replace('"','\''); if(key == '') throw new Error('info.key Cannot be null!'); if(secret == '') throw new Error('info.secret Cannot be null!'); request({ url: 'https://api.uberduck.ai/speak', method: 'POST', body: `{"speech":"${speech}","voice":"${voice}"}`, auth: { 'user': key, 'pass': secret } }, async (error, response, body) => { if (!error && response.statusCode == 200) finished = `https://api.uberduck.ai/speak-status?uuid=${JSON.parse(body).uuid}`; else if(body.detail != null) throw new Error(body.detail); }); while(!finished) await timeout(20); return finished; } }