Skip to content

Commit

Permalink
Revert "chore: clean up (add videoRoutes.ts)"
Browse files Browse the repository at this point in the history
This reverts commit 3fd4a73.
  • Loading branch information
okdargy committed Jan 10, 2024
1 parent cb7ab50 commit e4280ba
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 90 deletions.
132 changes: 105 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Hono, Handler } from 'hono'
import { cache } from 'hono/cache'
import { HandlerResponse } from 'hono/types'

import { getVideoInfo } from '@/services/tiktok'
import generateAlternate from '@/util/generateAlternate'
import { handleVideo} from '@/videoRoutes'
import { grabAwemeId, getVideoInfo } from './services/tiktok'
import { VideoResponse, ErrorResponse } from './templates'
import generateAlternate from './util/generateAlternate'

const app = new Hono()

app.get('/test/:videoId', async (c) => {
const { videoId } = c.req.param()
const awemeId = await getVideoInfo(videoId)

if(awemeId instanceof Error) {
return new Response((awemeId as Error).message, { status: 500 })
}

return new Response(JSON.stringify(awemeId), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
})

app.get('/', (c) => {
return new Response('', {
status: 302,
Expand All @@ -16,32 +33,59 @@ app.get('/', (c) => {
})
})

const routes = [
{
path: '/:videoId',
handler: handleVideo
},
{
path: '/*/video/:videoId',
handler: handleVideo
},
{
path: '/t/:videoId',
handler: handleVideo
const returnHTMLResponse = (content: string, status?: number): HandlerResponse<Response> => {
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<Response> {
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

const { videoId } = c.req.param()
let id = videoId;

// If the user agent is a bot, redirect to the TikTok page
if (!BOT_REGEX.test(c.req.header('User-Agent') || '')) {
return new Response('', {
status: 302,
headers: {
'Location': 'https://www.tiktok.com' + `${awemeIdPattern.test(videoId) ? c.req.path : '/t/' + videoId}`
}
})
}
]

// temp-fix: add trailing slash to all routes
routes.forEach(route => {
app.get(route.path, route.handler)
app.get(route.path + '/', route.handler)
})

// Cache all generate requests for 1 hour
app.get('/generate/*', cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
}))
// If the videoId needs to be validated, do it here
if (!awemeIdPattern.test(videoId)) {
try {
const awemeId = await grabAwemeId(videoId)
id = awemeId
} catch(e) {
const responseContent = await ErrorResponse((e as Error).message);
return returnHTMLResponse(responseContent, 201) as Response;
}
}

try {
const videoInfo = await getVideoInfo(id)

if (videoInfo instanceof Error) {
const responseContent = await ErrorResponse((videoInfo as Error).message);
return returnHTMLResponse(responseContent, 201) as Response;
}

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;
}
}

app.get('/generate/alternate', (c) => {
const content = JSON.stringify(generateAlternate(c));
Expand All @@ -54,6 +98,19 @@ app.get('/generate/alternate', (c) => {
})
})

function getExpiry(url: URL) {
const hex = new URL(url).pathname.split('/')[2];
return new Date(parseInt(hex, 16) * 1000);
}

app.get(
'/generate/*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
})
)

app.get('/generate/video/:videoId', async (c) => {
const { videoId } = c.req.param()
const data = await getVideoInfo(videoId);
Expand Down Expand Up @@ -92,4 +149,25 @@ app.get('/generate/image/:videoId', async (c) => {
}
})

const routes = [
{
path: '/:videoId',
handler: handleVideo
},
{
path: '/*/video/:videoId',
handler: handleVideo
},
{
path: '/t/:videoId',
handler: handleVideo
}
]

// temp-fix: add trailing slash to all routes
routes.forEach(route => {
app.get(route.path, route.handler)
app.get(route.path + '/', route.handler)
})

export default app
58 changes: 0 additions & 58 deletions src/videoRoutes.ts

This file was deleted.

3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"types": [
"@cloudflare/workers-types"
],
"paths": {
"@/*": [ "./src/*" ]
},
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
},
Expand Down
2 changes: 0 additions & 2 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name = "fxtiktok-rewrite"
compatibility_date = "2023-01-01"
main = "src/index.ts"

node_compat = true

# [vars]
# MY_VARIABLE = "production_value"

Expand Down

1 comment on commit e4280ba

@okdargy
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urgent: generate imgae/video/meta endpoints are not working

Please sign in to comment.