Skip to content

Commit

Permalink
feat(bot): create embed-proxy api client
Browse files Browse the repository at this point in the history
  • Loading branch information
lastarc committed Apr 27, 2024
1 parent 60cc725 commit 3dbd20b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
25 changes: 2 additions & 23 deletions bot/commands/media/tiktok.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ 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');

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')
Expand Down Expand Up @@ -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}`);
},
};
37 changes: 37 additions & 0 deletions bot/embed-proxy-client.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 3dbd20b

Please sign in to comment.