From 3dbd20bbc70c57bd177aa89198db8c9fd4104d8a Mon Sep 17 00:00:00 2001 From: Arc <90333108+lastarc@users.noreply.github.com> Date: Sat, 27 Apr 2024 17:41:42 +0200 Subject: [PATCH] feat(bot): create embed-proxy api client --- bot/commands/media/tiktok.js | 25 ++---------------------- bot/embed-proxy-client.js | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 bot/embed-proxy-client.js diff --git a/bot/commands/media/tiktok.js b/bot/commands/media/tiktok.js index 5be15a6..8c2d8a6 100644 --- a/bot/commands/media/tiktok.js +++ b/bot/commands/media/tiktok.js @@ -4,6 +4,7 @@ const { execSync } = require('child_process'); const fs = require('node:fs'); const S3 = require('../../s3'); const crypto = require('crypto'); +const EmbedProxyClient = require('../../embed-proxy-client'); const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME; if (!S3_BUCKET_NAME) throw new Error('No bucket name provided'); @@ -11,9 +12,6 @@ if (!S3_BUCKET_NAME) throw new Error('No bucket name provided'); const S3_PUBLIC_BUCKET_URL = process.env.S3_PUBLIC_BUCKET_URL; if (!S3_PUBLIC_BUCKET_URL) throw new Error('No public bucket URL provided'); -const EMBED_PROXY_URL = process.env.EMBED_PROXY_URL; -if (!EMBED_PROXY_URL) throw new Error('No embed proxy URL provided'); - module.exports = { data: new SlashCommandBuilder() .setName('tiktok') @@ -107,26 +105,7 @@ module.exports = { } console.log(vurl, vwidth, vheight); - let embeddableUrl = ''; - - const v2Check = await fetch(`${EMBED_PROXY_URL}/v2/healthz`); - if (v2Check.ok) { - const v2Res = await fetch(`${EMBED_PROXY_URL}/v2/add`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - src: vurl, - width: vwidth, - height: vheight, - }), - }); - const v2Json = await v2Res.json(); - embeddableUrl = `${EMBED_PROXY_URL}/v2/~/${v2Json.slug}`; - } else { - embeddableUrl = `${EMBED_PROXY_URL}/?src=${encodeURIComponent(vurl)}&width=${vwidth}&height=${vheight}`; - } + const embeddableUrl = await EmbedProxyClient.add(vurl, vwidth, vheight); await replyMsg.edit(`${embeddableUrl}`); }, }; \ No newline at end of file diff --git a/bot/embed-proxy-client.js b/bot/embed-proxy-client.js new file mode 100644 index 0000000..234f87d --- /dev/null +++ b/bot/embed-proxy-client.js @@ -0,0 +1,37 @@ +class EmbedProxyClient { + constructor() { + this.endpoint = process.env.EMBED_PROXY_URL; + if (!this.endpoint) throw new Error('No embed proxy URL provided'); + } + + async add(src, width, height) { + return await this.checkV2Live() ? await this.v2Add(src, width, height) : this.v1Url(src, width, height); + } + + v1Url(src, width, height) { + return `${this.endpoint}/?src=${encodeURIComponent(src)}&width=${width}&height=${height}`; + } + + async checkV2Live() { + const res = await fetch(`${this.endpoint}/v2/healthz`); + return res.ok; + } + + async v2Add(src, width, height) { + const res = await fetch(`${this.endpoint}/v2/add`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + src, + width, + height, + }), + }); + const json = await res.json(); + return `${this.endpoint}/v2/~/${json.slug}`; + } +} + +module.exports = new EmbedProxyClient();