feat: video duration

This commit is contained in:
DIYgod
2025-05-18 11:54:06 +08:00
parent 3cfbe13c4f
commit 502daae0c1
5 changed files with 33 additions and 1 deletions
+2
View File
@@ -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,
+2
View File
@@ -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,
+2
View File
@@ -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,
+8 -1
View File
@@ -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);
});
});
+19
View File
@@ -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);
}