Skip to content

Commit

Permalink
fixed expiring playback urls
Browse files Browse the repository at this point in the history
  • Loading branch information
KodingDev committed Sep 24, 2022
1 parent d0a2860 commit 8c87540
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* tiktxk - Open Source, Privacy First TikTok Embeds
* Copyright (C) 2022 Britmoji
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export const Constants = {
HOST_URL: "https://tiktxk.com",
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { StatusCode } from "hono/utils/http-status";
import { addTikTokRoutes } from "./routes/tiktok";
import { addIndexRoutes } from "./routes";
import { addEmbedRoutes } from "@/routes/embed";
import { addMetaRoutes } from "@/routes/meta";

const app = new Hono();

Expand All @@ -32,6 +33,7 @@ app.use("*", etag(), logger());
// Add routes
addIndexRoutes(app);
addEmbedRoutes(app);
addMetaRoutes(app);
addTikTokRoutes(app);

// Add error handlers
Expand Down
71 changes: 71 additions & 0 deletions src/routes/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* tiktxk - Open Source, Privacy First TikTok Embeds
* Copyright (C) 2022 Britmoji
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Hono } from "hono";
import { tiktok } from "@/util/tiktok";
import { StatusError } from "@/types/cloudflare";

export const addMetaRoutes = (app: Hono) => {
// Video metadata
app.get("/meta/:videoId/video", async (c) => {
const videoId = c.req.param("videoId");
const details = await tiktok.details(videoId);

if (!details) {
throw new StatusError(404);
}

return c.redirect(details.itemInfo.itemStruct.video.downloadAddr);
});

// Thumbnail metadata
app.get("/meta/:videoId/thumbnail", async (c) => {
const videoId = c.req.param("videoId");
const details = await tiktok.details(videoId);

if (!details) {
throw new StatusError(404);
}

return c.redirect(details.itemInfo.itemStruct.video.cover);
});

// Audio metadata
app.get("/meta/:videoId/audio", async (c) => {
const videoId = c.req.param("videoId");
const details = await tiktok.details(videoId);

if (!details) {
throw new StatusError(404);
}

return c.redirect(details.itemInfo.itemStruct.music.playUrl);
});

// All metadata
app.get("/meta/:videoId", async (c) => {
const videoId = c.req.param("videoId");
const details = await tiktok.details(videoId);

if (!details) {
throw new StatusError(404);
}

return c.json(details);
});
};
9 changes: 7 additions & 2 deletions src/routes/tiktok.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import { StatusError } from "@/types/cloudflare";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jsx } from "hono/jsx";
import { formatNumber } from "@/util/numbers";
import { Constants } from "@/constants";

const DiscordEmbed = ({ data }: { data: ItemDetails }) => {
const videoId = data.itemInfo.itemStruct.id;
const video = data.itemInfo.itemStruct.video;
const stats = data.itemInfo.itemStruct.stats;
const author = data.itemInfo.itemStruct.author.uniqueId;
Expand All @@ -45,7 +47,10 @@ const DiscordEmbed = ({ data }: { data: ItemDetails }) => {
/>

{/* Video Metadata */}
<meta property="og:video" content={video.downloadAddr} />
<meta
property="og:video"
content={`${Constants.HOST_URL}/meta/${videoId}/video`}
/>
<meta property="og:video:type" content={`video/${video.format}`} />
<meta property="og:video:width" content={video.width} />
<meta property="og:video:height" content={video.height} />
Expand All @@ -54,7 +59,7 @@ const DiscordEmbed = ({ data }: { data: ItemDetails }) => {
{/* The additional oembed is pulled by Discord to enable improved embeds. */}
<link
rel="alternate"
href={`https://tiktxk.com/internal/embed?username=${author}`}
href={`${Constants.HOST_URL}/internal/embed?username=${author}`}
type="application/json+oembed"
/>
</head>
Expand Down
3 changes: 3 additions & 0 deletions src/util/tiktok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export interface ItemDetails {
height: number;
width: number;
};
music: {
playUrl: string;
};
};
};
}
Expand Down
Loading

0 comments on commit 8c87540

Please sign in to comment.