From 502daae0c1574e21cd76387feefd0173b8bca519 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 18 May 2025 11:54:06 +0800 Subject: [PATCH] feat: video duration --- lib/routes/bilibili/dynamic.ts | 2 ++ lib/routes/bilibili/ranking.ts | 2 ++ lib/routes/bilibili/video.ts | 2 ++ lib/utils/helpers.test.ts | 9 ++++++++- lib/utils/helpers.ts | 19 +++++++++++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/routes/bilibili/dynamic.ts b/lib/routes/bilibili/dynamic.ts index e93632521..ad599ba2a 100644 --- a/lib/routes/bilibili/dynamic.ts +++ b/lib/routes/bilibili/dynamic.ts @@ -7,6 +7,7 @@ import { parseDate } from '@/utils/parse-date'; import { fallback, queryToBoolean } from '@/utils/readable-social'; import cacheIn from './cache'; import { BilibiliWebDynamicResponse, Item2, Modules } from './api-interface'; +import { parseDuration } from '@/utils/helpers'; export const route: Route = { path: '/user/dynamic/:uid/:routeParams?', @@ -387,6 +388,7 @@ async function handler(ctx) { { url: urlResult?.videoPageUrl || originUrlResult?.videoPageUrl, mime_type: 'text/html', + duration_in_seconds: data.module_dynamic?.major?.archive?.duration_text ? parseDuration(data.module_dynamic.major.archive.duration_text) : undefined, }, ] : undefined, diff --git a/lib/routes/bilibili/ranking.ts b/lib/routes/bilibili/ranking.ts index 06cca8282..59b1652e5 100644 --- a/lib/routes/bilibili/ranking.ts +++ b/lib/routes/bilibili/ranking.ts @@ -1,6 +1,7 @@ import { Route, ViewType } from '@/types'; import got from '@/utils/got'; import utils, { getVideoUrl } from './utils'; +import { parseDuration } from '@/utils/helpers'; // https://www.bilibili.com/v/popular/rank/all @@ -223,6 +224,7 @@ async function handler(ctx) { { url: getVideoUrl(item.bvid), mime_type: 'text/html', + duration_in_seconds: parseDuration(item.duration), }, ] : undefined, diff --git a/lib/routes/bilibili/video.ts b/lib/routes/bilibili/video.ts index 137bbc85c..af0375a8b 100644 --- a/lib/routes/bilibili/video.ts +++ b/lib/routes/bilibili/video.ts @@ -7,6 +7,7 @@ import cache from './cache'; import utils, { getVideoUrl } from './utils'; import logger from '@/utils/logger'; import type { BilibiliWebDynamicResponse } from './api-interface'; +import { parseDuration } from '@/utils/helpers'; export const route: Route = { path: '/user/video/:uid/:embed?', @@ -89,6 +90,7 @@ async function handler(ctx: Context) { { url: getVideoUrl(bvid), mime_type: 'text/html', + duration_in_seconds: parseDuration(dynamic?.duration_text), }, ] : undefined, diff --git a/lib/utils/helpers.test.ts b/lib/utils/helpers.test.ts index 23ac83ca0..b5029365f 100644 --- a/lib/utils/helpers.test.ts +++ b/lib/utils/helpers.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { getRouteNameFromPath, getSearchParamsString } from '@/utils/helpers'; +import { getRouteNameFromPath, getSearchParamsString, parseDuration } from '@/utils/helpers'; describe('helpers', () => { it('getRouteNameFromPath', () => { @@ -17,4 +17,11 @@ describe('helpers', () => { searchParams.append('ids[]', '2'); expect(getSearchParamsString(searchParams)).toBe('ids%5B%5D=1&ids%5B%5D=2'); }); + + it('parseDuration', () => { + expect(parseDuration('01:01:01')).toBe(3661); + expect(parseDuration('01:01')).toBe(61); + expect(parseDuration('00:01')).toBe(1); + expect(parseDuration('59')).toBe(59); + }); }); diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index b8c2f6995..851dba769 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -40,3 +40,22 @@ export function getSearchParamsString(searchParams: any) { const searchParamsString = isPureObject(searchParams) ? stringifyQuery(searchParams) : null; return searchParamsString ?? new URLSearchParams(searchParams).toString(); } + +/** + * parse duration string to seconds + * @param {string} timeStr - duration string like "01:01:01" / "01:01" / "59" + * @returns {number} - total seconds + */ +export function parseDuration(timeStr: string) { + const clean = timeStr.trim().replaceAll(/[^\d:]/g, ''); + return clean + .split(':') + .reverse() + .reduce((total, part, idx) => { + const n = Number(part); + if (Number.isNaN(n)) { + throw new TypeError(`Invalid segment: ${part}`); + } + return total + n * Math.pow(60, idx); + }, 0); +}