Skip to content

Commit

Permalink
fix: working w/ new API changes (thanks yt-dlp)
Browse files Browse the repository at this point in the history
  • Loading branch information
KodingDev committed Mar 28, 2024
1 parent 869a1b6 commit 458067f
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 245 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"hono": "^4.1.4",
"toucan-js": "^3.3.1"
"toucan-js": "^3.3.1",
"uuid": "^9.0.1"
}
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
*/

export const Constants = {
HOST_URL: "https://wip.koding.dev",
HOST_URL: "https://tiktxk.com",
};
22 changes: 10 additions & 12 deletions src/routes/tiktok.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { get } from "@/util/http";
import { Bindings, StatusError } from "@/types/cloudflare";
import { formatNumber } from "@/util/numbers";
import { Constants } from "@/constants";
import { GenericDiscordEmbed, respondDiscord } from "@/util/discord";
import { FC, Fragment } from "hono/jsx";
import { DiscordEmbedData, respondDiscord } from "@/util/discord";
import { Fragment } from "hono/jsx";

const DiscordEmbed: FC<{ data: AdaptedItemDetails }> = ({ data }) => {
const createDiscordEmbed = (data: AdaptedItemDetails): DiscordEmbedData => {
// Format the numbers
const likes = formatNumber(data.statistics.likes, 1);
const comments = formatNumber(data.statistics.comments, 1);
Expand All @@ -49,14 +49,12 @@ const DiscordEmbed: FC<{ data: AdaptedItemDetails }> = ({ data }) => {
const authorUrl = `https://tiktok.com/@${data.author.username}`;

// noinspection HtmlRequiredTitleElement
return (
<GenericDiscordEmbed
author={{ name: authorName, url: authorUrl }}
title={`❤️ ${likes} 💬 ${comments}`}
url={`https://tiktok.com/@${data.author.username}/video/${data.id}`}
component={previewComponent}
/>
);
return {
author: { name: authorName, url: authorUrl },
title: `❤️ ${likes} 💬 ${comments}`,
url: `https://tiktok.com/@${data.author.username}/video/${data.id}`,
component: previewComponent as unknown as Element | undefined,
};
};

const VideoPreview = ({ tiktok }: { tiktok: AdaptedItemDetails }) => (
Expand Down Expand Up @@ -110,7 +108,7 @@ export const addTikTokRoutes = (app: Hono<{ Bindings: Bindings }>) => {
const render = (c: Context, data: AdaptedItemDetails) =>
respondDiscord(
c,
() => <DiscordEmbed data={data} />,
() => createDiscordEmbed(data),
() =>
c.redirect(
`https://www.tiktok.com/@${data.author.username}/video/${data.id}`,
Expand Down
64 changes: 0 additions & 64 deletions src/util/client.ts

This file was deleted.

22 changes: 9 additions & 13 deletions src/util/discord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
*/

import { Constants } from "@/constants";
import { FC, JSXNode } from "hono/jsx";
import { FC } from "hono/jsx";
import { Context, HonoRequest } from "hono";
import { HtmlEscapedString } from "hono/dist/types/utils/html";

type DiscordEmbedData = {
export type DiscordEmbedData = {
title?: string;
color?: string;
description?: string;
Expand All @@ -32,17 +31,20 @@ type DiscordEmbedData = {
url?: string;
};

component?: JSXNode;
component?: Element;
};

export const GenericDiscordEmbed: FC = (embed: DiscordEmbedData) => {
export const GenericDiscordEmbed: FC<DiscordEmbedData> = (
embed: DiscordEmbedData,
) => {
// Build the embed url
const authorName = embed.author?.name ?? "TikTxk";
const authorUrl = embed.author?.url ?? Constants.HOST_URL;

const url = new URL(`${Constants.HOST_URL}/internal/embed`);
url.searchParams.set("authorName", authorName);
url.searchParams.set("authorUrl", authorUrl);
console.log(embed);

// noinspection HtmlRequiredTitleElement
return (
Expand Down Expand Up @@ -95,17 +97,11 @@ export const isDiscord = (req: HonoRequest): boolean => {
*/
export const respondDiscord = (
ctx: Context,
embed: () => DiscordEmbedData | JSX.Element,
embed: () => DiscordEmbedData,
handler: () => Response | Promise<Response>,
): Response | Promise<Response> => {
if (isDiscord(ctx.req)) {
// Get the embed data
const data = embed();

// noinspection SuspiciousTypeOfGuard
return ctx.html(
typeof data === "string" ? data : <GenericDiscordEmbed {...data} />,
);
return ctx.html(<GenericDiscordEmbed {...embed()} />);
} else {
return handler();
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { USER_AGENT } from "./tiktok";
import { Context } from "hono";

// I am not a 🤖
export const get = async (url: string) => {
return await fetch(url, {
headers: {
"User-Agent": USER_AGENT,
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
},
});
};
Expand Down
25 changes: 25 additions & 0 deletions src/util/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* tiktxk - Open Source, Privacy First TikTok Embeds
* Copyright (C) 2024 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 randomInt = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;

export const randomBigInt = (min: bigint, max: bigint) =>
BigInt(
Math.floor(Math.random() * (Number(max) - Number(min) + 1)) + Number(min),
);
Loading

0 comments on commit 458067f

Please sign in to comment.