mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
little changes
This commit is contained in:
1 parent
862f326996
commit
b7252fccaf
41 files changed
+261
-228
No files matched your search
@@ -1,3 +1,12 @@
|
||||
/**
|
||||
* @file Tuna.mjs — Tuna — Voicemod Tuna API client for sound effect search and download
|
||||
* @module src.constants.audio.Tuna
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tuna API client for searching and downloading sound effects
|
||||
* from the Voicemod Tuna service (tuna-api.voicemod.net).
|
||||
*/
|
||||
import pkg from "follow-redirects";
|
||||
const { https } = pkg;
|
||||
|
||||
@@ -7,11 +16,9 @@ class Tuna {
|
||||
this.apiKey = auth.key;
|
||||
}
|
||||
get(path, params = {}) {
|
||||
return new Promise((resolve, rej) => {
|
||||
let query = "";
|
||||
for (let key in params) {
|
||||
query += ((query.length === 0) ? "?" : "&") + key + "=" + encodeURIComponent(params[key]);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const qs = new URLSearchParams(params).toString();
|
||||
const query = qs ? "?" + qs : "";
|
||||
const options = {
|
||||
method: "GET",
|
||||
hostname: "tuna-api.voicemod.net",
|
||||
@@ -19,21 +26,21 @@ class Tuna {
|
||||
headers: { "x-api-key": this.apiKey },
|
||||
maxRedirects: 20
|
||||
};
|
||||
const req = https.request(options, function(res) {
|
||||
const req = https.request(options, (res) => {
|
||||
const chunks = [];
|
||||
res.on("data", (chunk) => { chunks.push(chunk); });
|
||||
res.on("end", () => {
|
||||
try {
|
||||
resolve(JSON.parse(Buffer.concat(chunks).toString()));
|
||||
} catch (e) {
|
||||
rej(new Error(`Tuna API JSON parse error: ${e.message}`));
|
||||
reject(new Error(`Tuna API JSON parse error: ${e.message}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
req.on("error", (error) => { rej(error); });
|
||||
req.on("error", (error) => { reject(error); });
|
||||
req.setTimeout(15_000, () => {
|
||||
req.destroy();
|
||||
rej(new Error("Tuna API request timeout"));
|
||||
reject(new Error("Tuna API request timeout"));
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
@@ -41,9 +48,10 @@ class Tuna {
|
||||
search(query, page = 1, size = 10) {
|
||||
return this.get("/v1/sounds/search", { size, page, search: query }).then(results => {
|
||||
results.items = results.items.map(s => {
|
||||
s.download = function() {
|
||||
const oggPath = s.oggPath;
|
||||
s.download = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = https.get(this.oggPath, { maxRedirects: 20 }, (r) => { resolve(r); });
|
||||
const req = https.get(oggPath, { maxRedirects: 20 }, (r) => { resolve(r); });
|
||||
req.on("error", (err) => { reject(err); });
|
||||
req.setTimeout(15_000, () => { req.destroy(); reject(new Error("Tuna download timeout")); });
|
||||
});
|
||||
@@ -55,9 +63,10 @@ class Tuna {
|
||||
}
|
||||
getSound(id) {
|
||||
return this.get("/v1/sounds/" + id).then(sound => {
|
||||
sound.download = function() {
|
||||
const oggPath = sound.oggPath;
|
||||
sound.download = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = https.get(this.oggPath, { maxRedirects: 20 }, (result) => { resolve(result); });
|
||||
const req = https.get(oggPath, { maxRedirects: 20 }, (result) => { resolve(result); });
|
||||
req.on("error", (err) => { reject(err); });
|
||||
req.setTimeout(15_000, () => { req.destroy(); reject(new Error("Tuna download timeout")); });
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user