Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions apps/api/src/helpers/oembed/extractOgTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@ const extractOgTags = async (document: Document) => {

const urlTag = document.querySelector('meta[property="og:url"]');
const pageUrl = urlTag
? imageTag.getAttribute("content") || "https://tape.xyz"
? urlTag.getAttribute("content") || "https://tape.xyz"
: "https://tape.xyz";

const iframeHTML = await constructIframe(document);
const html = iframeHTML;

const metadata = {
const metadata: Record<string, unknown> = {
version: "1.0",
height: 113,
width: 200,
title,
author_name: title.match(/by\s(.*)\s•/)?.[1] || title,
author_url: pageUrl,
description,
type: "video",
type: iframeHTML ? "video" : "link",
provider_name: "Tape",
provider_url: "https://tape.xyz",
thumbnail_height: 360,
thumbnail_width: 480,
thumbnail_url: image,
html
thumbnail_url: image
};

if (iframeHTML) {
metadata.html = iframeHTML;
}

return metadata;
};

Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "dotenv/config";

import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { cors as corsMiddleware } from "hono/cors";
import { logger } from "hono/logger";

import { cors, originLogger } from "./middlewares";
Expand All @@ -25,6 +26,10 @@ const app = new Hono();

app.use(logger()).use(originLogger).use("*", cors);

// oEmbed endpoint needs wider CORS for external consumers
app.use("/oembed/*", corsMiddleware({ origin: "*" }));
app.use("/oembed", corsMiddleware({ origin: "*" }));

app
.get("/", (c) => c.text("nothing to see here, visit tape.xyz"))
.route("/did", did)
Expand Down
45 changes: 35 additions & 10 deletions apps/api/src/routes/oembed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ app.get("/", zValidator("query", validationSchema), async (c) => {
let url = reqUrlParams.get("url") as string;
const format = reqUrlParams.get("format") as string;

if (!url) {
return c.json(
{ success: false, message: "Missing required parameter: url" },
400
);
}

if (COMMON_REGEX.TAPE_WATCH.test(url)) {
// Fetch metatags directly from tape.xyz
const path = new URL(url).pathname;
Expand All @@ -36,40 +43,58 @@ app.get("/", zValidator("query", validationSchema), async (c) => {
const response = await fetch(url, {
headers: { "User-Agent": TAPE_USER_AGENT }
});

if (!response.ok) {
return c.json(
{ success: false, message: `Failed to fetch URL: ${response.status}` },
400
);
}

const html = await response.text();
const { document } = parseHTML(html);

const ogData = await extractOgTags(document);

if (format === "json") {
c.header("Cache-Control", CACHE_CONTROL.FOR_ONE_WEEK);
return c.json(ogData);
}

if (format === "xml") {
const escapeXml = (str: string) =>
String(str)
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");

c.res.headers.set("Content-Type", "application/xml");
c.header("Cache-Control", CACHE_CONTROL.FOR_ONE_WEEK);
return c.body(`<?xml version="1.0" encoding="utf-8"?>
<oembed>
<title>${ogData.title}</title>
<author_name>${ogData.author_name}</author_name>
<author_url>${ogData.author_url}</author_url>
<type>${ogData.type}</type>
<title>${escapeXml(ogData.title)}</title>
<author_name>${escapeXml(ogData.author_name)}</author_name>
<author_url>${escapeXml(ogData.author_url)}</author_url>
<type>${escapeXml(ogData.type)}</type>
<height>${ogData.height}</height>
<width>${ogData.width}</width>
<version>${ogData.version}</version>
<provider_name>${ogData.provider_name}</provider_name>
<provider_url>${ogData.provider_url}</provider_url>
<version>${escapeXml(ogData.version)}</version>
<provider_name>${escapeXml(ogData.provider_name)}</provider_name>
<provider_url>${escapeXml(ogData.provider_url)}</provider_url>
<thumbnail_height>${ogData.thumbnail_height}</thumbnail_height>
<thumbnail_width>${ogData.thumbnail_width}</thumbnail_width>
<thumbnail_url>${ogData.thumbnail_url}</thumbnail_url>
<html>${ogData.html}</html>
<thumbnail_url>${escapeXml(ogData.thumbnail_url)}</thumbnail_url>
<html>${escapeXml(ogData.html as string)}</html>
</oembed>`);
}

c.header("Cache-Control", CACHE_CONTROL.FOR_ONE_WEEK);
return c.json({ success: true, og: ogData });
} catch (error) {
console.error("[OEMBED] Error:", error);
return c.json({ success: false, message: ERROR_MESSAGE });
return c.json({ success: false, message: ERROR_MESSAGE }, 500);
}
});

Expand Down