From ac68a8d4431e5eb3af3e031ed6ba34b2c7569366 Mon Sep 17 00:00:00 2001 From: Essem Date: Thu, 11 Jun 2026 19:06:23 -0500 Subject: [PATCH] mediadetect: Use Tenor ID from embed if available With a bit of modification, the IDs used for video embeds can be used to get a GIF as well. --- src/utils/mediadetect.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/utils/mediadetect.ts b/src/utils/mediadetect.ts index 78be36d4..5a4aca7d 100644 --- a/src/utils/mediadetect.ts +++ b/src/utils/mediadetect.ts @@ -102,19 +102,24 @@ async function getMedia( const host = url2.host; if (combined.includes(host)) { if (tenorURLs.includes(host) && url2.pathname.startsWith("/view/")) { - const tenorURL = url2; - if (!tenorURL.pathname.endsWith(".gif")) tenorURL.pathname += ".gif"; - - const redirectReq = await fetch(tenorURL, { method: "HEAD", redirect: "manual" }); - if (redirectReq.status !== 301 && redirectReq.status !== 302) return; - - const redirect = redirectReq.headers.get("location"); - if (!redirect) return; - - // format it into a "proper" raw link - const match = tenorRegex.exec(redirect); - if (!match) return; - payload.path = `https://c.tenor.com/${match[1]}/tenor.gif`; + if (mediaURL.hostname === "media.tenor.com") { + // if we have an existing media link, we can extract the ID from it and mess with it a little to get a GIF + const id = mediaURL.pathname.split("/")[1]; + payload.path = `https://c.tenor.com/${id.substring(0, id.length - 2) + "AC"}/tenor.gif`; + } else { + if (!url2.pathname.endsWith(".gif")) url2.pathname += ".gif"; + + const redirectReq = await fetch(url2, { method: "HEAD", redirect: "manual" }); + if (redirectReq.status !== 301 && redirectReq.status !== 302) return; + + const redirect = redirectReq.headers.get("location"); + if (!redirect) return; + + // format it into a "proper" raw link + const match = tenorRegex.exec(redirect); + if (!match) return; + payload.path = `https://c.tenor.com/${match[1]}/tenor.gif`; + } } else if (klipyURLs.includes(host)) { if (!process.env.KLIPY || process.env.KLIPY === "") return; if (!media2.includes("klipy.com/gifs/")) return; -- 2.51.2