diff --git a/src/index.ts b/src/index.ts index dca04c0..792a858 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ -import { Hono, Handler } from 'hono' +import { Hono } from 'hono' import { cache } from 'hono/cache' -import { HandlerResponse } from 'hono/types' import { grabAwemeId, getVideoInfo } from './services/tiktok' import { VideoResponse, ErrorResponse } from './templates' import generateAlternate from './util/generateAlternate' +import { returnHTMLResponse, returnDirectResponse } from './util/ResponseHelper' const app = new Hono() @@ -33,16 +33,6 @@ app.get('/', (c) => { }) }) -const returnHTMLResponse = (content: string, status?: number): HandlerResponse => { - return new Response(content, { - status: status || 200, - headers: { - 'Content-Type': 'text/html; charset=utf-8', - 'Cache-Control': 'public, max-age=3600' - } - }) -} - async function handleVideo(c: any): Promise { const awemeIdPattern = /^\d{1,19}$/; const BOT_REGEX = /bot|facebook|embed|got|firefox\/92|curl|wget|go-http|yahoo|generator|whatsapp|discord|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node/gi @@ -60,7 +50,7 @@ async function handleVideo(c: any): Promise { }) } - // If the videoId needs to be validated, do it here + // If the videoId doesn't match the awemeIdPattern, that means we have shortened TikTok link and we need to grab the awemeId if (!awemeIdPattern.test(videoId)) { try { const awemeId = await grabAwemeId(videoId) @@ -79,8 +69,14 @@ async function handleVideo(c: any): Promise { return returnHTMLResponse(responseContent, 201) as Response; } - const responseContent = await VideoResponse(videoInfo); - return returnHTMLResponse(responseContent) as Response; + const url = new URL(c.req.url); + + if(url.hostname.includes('d.tnktok.com')) { + return returnDirectResponse(videoInfo) as Response; + } else { + const responseContent = await VideoResponse(videoInfo); + return returnHTMLResponse(responseContent) as Response; + } } catch(e) { const responseContent = await ErrorResponse((e as Error).message); return returnHTMLResponse(responseContent, 201) as Response; @@ -158,6 +154,10 @@ const routes = [ path: '/*/video/:videoId', handler: handleVideo }, + { + path: '/*/photo/:videoId', + handler: handleVideo + }, { path: '/t/:videoId', handler: handleVideo diff --git a/src/util/ResponseHelper.ts b/src/util/ResponseHelper.ts new file mode 100644 index 0000000..1eb5f19 --- /dev/null +++ b/src/util/ResponseHelper.ts @@ -0,0 +1,29 @@ +import { HandlerResponse } from "hono/types" +import { AwemeList } from "../types/Services" +export const returnHTMLResponse = (content: string, status?: number): HandlerResponse => { + return new Response(content, { + status: status || 200, + headers: { + 'Content-Type': 'text/html; charset=utf-8', + 'Cache-Control': 'public, max-age=3600' + } + }) +} + +export const returnDirectResponse = (videoInfo: AwemeList): HandlerResponse => { + if(videoInfo.video.duration !== 0) { + return new Response('', { + status: 302, + headers: { + 'Location': `https://tnktok.com/generate/video/` + videoInfo.aweme_id + } + }) + } else { + return new Response('', { + status: 302, + headers: { + 'Location': videoInfo.video.cover.url_list[0] + } + }) + } +} \ No newline at end of file diff --git a/src/util/generateAlternate.tsx b/src/util/generateAlternate.tsx index f352bc7..b7d9f51 100644 --- a/src/util/generateAlternate.tsx +++ b/src/util/generateAlternate.tsx @@ -12,8 +12,8 @@ function formatNumber(value: string): string { if (num < 1000000) return (num / 1000).toFixed(1) + 'k' if (num < 10000000) return (num / 1000000).toFixed(1) + 'M' if (num < 1000000000) return (num / 1000000).toFixed(1) + 'M' - if (num < 10000000000) return (num / 1000000000).toFixed(1) + 'b' - return (num / 1000000000).toFixed(0) + 'b' + if (num < 10000000000) return (num / 1000000000).toFixed(1) + 'B' + return (num / 1000000000).toFixed(0) + 'B' }