Skip to content

Commit

Permalink
feat: swap stats and title
Browse files Browse the repository at this point in the history
links on stats were previously the author which made no sense
  • Loading branch information
okdargy committed Jun 8, 2024
1 parent 44071da commit b2a0def
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
19 changes: 12 additions & 7 deletions src/templates/pages/LiveResponse.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import MetaHelper from '../../util/metaHelper'
import { LiveRoom } from '../../types/Live'
import MetaHelper from '@/util/metaHelper'
import { LiveRoom } from '@/types/Live'
import { formatNumber, formatTime } from '@/util/format'

export function LiveResponse(data: LiveRoom): JSX.Element {
let title = ''

title += `👀 ${formatNumber(String(data.liveRoomUserInfo.liveRoom.liveRoomStats.userCount))} `
title += `🕒 ${formatTime(data.liveRoomUserInfo.liveRoom.startTime)} `

return (
<>
{MetaHelper(
[
{
name: 'og:title',
content: `🔴 ${data.liveRoomUserInfo.user.nickname} (@${data.liveRoomUserInfo.user.nickname})`
content: title.trim()
},
{
name: 'theme-color',
Expand All @@ -24,7 +30,7 @@ export function LiveResponse(data: LiveRoom): JSX.Element {
},
{
name: 'twitter:title',
content: `🔴 ${data.liveRoomUserInfo.user.nickname} (@${data.liveRoomUserInfo.user.nickname})` // Nickname (@username)
content: `${data.liveRoomUserInfo.user.nickname} (@${data.liveRoomUserInfo.user.nickname})` // Nickname (@username)
},
{
name: 'og:url',
Expand All @@ -36,13 +42,12 @@ export function LiveResponse(data: LiveRoom): JSX.Element {
},
{
name: 'og:image',
content: data.liveRoomUserInfo.user.avatarMedium
content: data.liveRoomUserInfo.user.avatarLarger
}
],
{
unique_id: data.liveRoomUserInfo.user.uniqueId,
viewers: data.liveRoomUserInfo.liveRoom.liveRoomStats.userCount,
startTime: data.liveRoomUserInfo.liveRoom.startTime
nickname: data.liveRoomUserInfo.user.nickname
}
)}
</>
Expand Down
17 changes: 12 additions & 5 deletions src/templates/pages/VideoResponse.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MetaHelper from '../../util/metaHelper'
import { ItemStruct } from '../../types/Web'
import { formatNumber } from '../../util/format'

export function VideoResponse(data: ItemStruct): JSX.Element {
let videoUrl = 'https://fxtiktok-rewrite.dargy.workers.dev/generate/video/' + data.id
Expand Down Expand Up @@ -85,13 +86,22 @@ export function VideoResponse(data: ItemStruct): JSX.Element {
}
}

let title = ''

title += `❤️ ${formatNumber(data.stats.diggCount.toString())} `
title += `💬 ${formatNumber(data.stats.commentCount.toString())} `
title += `🔁 ${data.stats.shareCount.toString()} `
if (data.imagePost) {
title += `🖼️ ${data.imagePost.images.length.toString()} `
}

return (
<>
{MetaHelper(
[
{
name: 'og:title',
content: `${data.author.nickname} (@${data.author.uniqueId})` // Nickname (@username)
content: title.trim()
},
{
name: 'theme-color',
Expand Down Expand Up @@ -120,11 +130,8 @@ export function VideoResponse(data: ItemStruct): JSX.Element {
...videoMeta
],
{
likes: data.stats.diggCount,
comments: data.stats.commentCount,
shares: data.stats.shareCount,
unique_id: data.author.uniqueId,
images: data.imagePost ? data.imagePost.images.length : 0
nickname: data.author.nickname
}
)}
</>
Expand Down
30 changes: 30 additions & 0 deletions src/util/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export function formatNumber(value: string): string {
if (value === '0') return '0'

// parse num to int
const num = parseInt(value)
if (isNaN(num)) return value

if (num < 1000) return num.toString()
if (num < 10000) return (num / 1000).toFixed(1) + 'k'
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'
}

export function formatTime(time: number): string {
const timeElapsed = Date.now() - time * 1000 // time elapsed in milliseconds
const minutes = Math.floor(timeElapsed / 60000)
const hours = Math.floor(minutes / 60)
const days = Math.floor(hours / 24)

if (days > 0) {
return `${days}d ${hours % 24}h`
} else if (hours > 0) {
return `${hours}h ${minutes % 60}m`
} else {
return `${minutes}m`
}
}
46 changes: 3 additions & 43 deletions src/util/generateAlternate.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,5 @@
import { Context } from 'hono'

function formatNumber(value: string): string {
if (value === '0') return '0'

// parse num to int
const num = parseInt(value)
if (isNaN(num)) return value

if (num < 1000) return num.toString()
if (num < 10000) return (num / 1000).toFixed(1) + 'k'
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'
}

function formatTime(time: number): string {
const timeElapsed = Date.now() - time * 1000 // time elapsed in milliseconds
const minutes = Math.floor(timeElapsed / 60000)
const hours = Math.floor(minutes / 60)
const days = Math.floor(hours / 24)

if (days > 0) {
return `${days}d ${hours % 24}h`
} else if (hours > 0) {
return `${hours}h ${minutes % 60}m`
} else {
return `${minutes}m`
}
}

export default function generateAlternate(c: Context): {
version: string
type: string
Expand All @@ -40,23 +9,14 @@ export default function generateAlternate(c: Context): {
provider_url: string
title: string
} {
const { likes, comments, shares, unique_id, images, viewers, startTime } = c.req.query()

let author_name = ''
if (likes) author_name += `❤️ ${formatNumber(likes)} `
if (comments) author_name += `💬 ${formatNumber(comments)} `
if (shares) author_name += `📤 ${formatNumber(shares)} `
if (images) author_name += `🖼️ ${images} `
if (viewers) author_name += `👀 ${formatNumber(viewers)} `
if (startTime && !isNaN(Number(startTime))) author_name += `🕒 ${formatTime(parseInt(startTime))} `
if (author_name.length > 0 && author_name[author_name.length - 1] === ' ') author_name = author_name.slice(0, -1) // remove trailing space
const { unique_id, nickname } = c.req.query()

return {
version: '1.0',
type: 'link',
author_name: author_name,
author_name: `${nickname} (@${unique_id})`,
author_url: `https://www.tiktok.com/${unique_id ? '@' + unique_id : ''}`,
provider_name: 'fxTikTok',
provider_name: 'fxTikTok - Embed with s/i/n',
provider_url: 'https://github.com/okdargy/fxTikTok',
title: `TikTok by @${unique_id}`
}
Expand Down

0 comments on commit b2a0def

Please sign in to comment.