feat: add video preview url in attachments for bilibili and youtube

This commit is contained in:
DIYgod
2025-01-21 18:32:09 +08:00
parent 7b88a1cc43
commit 5d9e0f557e
8 changed files with 70 additions and 18 deletions
+14 -1
View File
@@ -2,7 +2,7 @@ import { Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import JSONbig from 'json-bigint';
import utils from './utils';
import utils, { getLiveUrl, getVideoUrl } from './utils';
import { parseDate } from '@/utils/parse-date';
import { fallback, queryToBoolean } from '@/utils/readable-social';
import cacheIn from './cache';
@@ -161,6 +161,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
}
let url = '';
let text = '';
let videoPageUrl;
const major = data.module_dynamic?.major;
if (!major) {
return null;
@@ -179,6 +180,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
const id = useAvid ? `av${archive?.aid}` : archive?.bvid;
url = `https://www.bilibili.com/video/${id}`;
text = `视频地址:<a href=${url}>${url}</a>`;
videoPageUrl = getVideoUrl(archive?.bvid);
break;
}
case 'MAJOR_TYPE_COMMON':
@@ -215,6 +217,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
case 'MAJOR_TYPE_LIVE_RCMD': {
const live_play_info = JSON.parse(major.live_rcmd?.content || '{}')?.live_play_info;
url = `https://live.bilibili.com/${live_play_info?.room_id}`;
videoPageUrl = getLiveUrl(live_play_info?.room_id);
text = `直播间地址:<a href=${url}>${url}</a>`;
break;
}
@@ -224,6 +227,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
return {
url,
text,
videoPageUrl,
};
};
@@ -377,6 +381,15 @@ async function handler(ctx) {
link,
author,
category: category.length ? [...new Set(category)] : undefined,
attachments:
urlResult?.videoPageUrl || originUrlResult?.videoPageUrl
? [
{
url: urlResult?.videoPageUrl || originUrlResult?.videoPageUrl,
mime_type: 'text/html',
},
]
: undefined,
};
})
);
+12 -3
View File
@@ -1,6 +1,6 @@
import { Route, ViewType } from '@/types';
import got from '@/utils/got';
import utils from './utils';
import utils, { getVideoUrl } from './utils';
// https://www.bilibili.com/v/popular/rank/all
@@ -187,7 +187,7 @@ async function handler(ctx) {
// redirect old routes like /bilibili/ranking/0/3/1 or /bilibili/ranking/0/3/1/xxx
const embedArg = args.redirect2 ? '/' + args.redirect2 : '';
ctx.set('redirect', `/bilibili/ranking/${getRidIndexByRid(args.rid_index)}${embedArg}`);
return;
return null;
}
const ridIndex = ctx.req.param('rid_index') || '0';
@@ -216,7 +216,16 @@ async function handler(ctx) {
description: utils.renderUGCDescription(embed, item.pic, item.description || item.title, item.aid, undefined, item.bvid),
pubDate: item.create && new Date(item.create).toUTCString(),
author: item.author,
link: !item.create || (new Date(item.create) / 1000 > utils.bvidTime && item.bvid) ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
link: !item.create || (new Date(item.create).getTime() / 1000 > utils.bvidTime && item.bvid) ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
image: item.pic,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
},
]
: undefined,
})),
};
}
+4
View File
@@ -298,6 +298,9 @@ export const renderOGVDescription = (embed: boolean, img: string, description: s
return rendered;
};
export const getVideoUrl = (bvid?: string) => (bvid ? `https://www.bilibili.com/blackboard/newplayer.html?isOutside=true&autoplay=true&danmaku=true&muted=false&highQuality=true&bvid=${bvid}` : undefined);
export const getLiveUrl = (roomId?: string) => (roomId ? `https://www.bilibili.com/blackboard/live/live-activity-player.html?cid=${roomId}` : undefined);
export default {
lsid,
_uuid,
@@ -313,4 +316,5 @@ export default {
getBangumiItems,
renderUGCDescription,
renderOGVDescription,
getVideoUrl,
};
+9 -1
View File
@@ -1,7 +1,7 @@
import { Route, ViewType } from '@/types';
import got from '@/utils/got';
import cache from './cache';
import utils from './utils';
import utils, { getVideoUrl } from './utils';
import logger from '@/utils/logger';
export const route: Route = {
@@ -73,6 +73,14 @@ async function handler(ctx) {
link: item.created > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
author: name,
comments: item.comment,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
},
]
: undefined,
})),
};
}
+7 -1
View File
@@ -1,6 +1,6 @@
import { Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import utils from './utils';
import utils, { getVideoUrl } from './utils';
import { config } from '@/config';
import { parseDate } from '@/utils/parse-date';
import ofetch from '@/utils/ofetch';
@@ -106,6 +106,12 @@ async function handler(ctx) {
link: `https://www.youtube.com/watch?v=${videoId}`,
author: snippet.videoOwnerChannelTitle,
image: img.url,
attachments: [
{
url: getVideoUrl(videoId),
mime_type: 'text/html',
},
],
};
}),
};
+3
View File
@@ -152,6 +152,8 @@ export const getLive = (id, cache) =>
);
return res;
});
export const getVideoUrl = (id: string) => `https://www.youtube-nocookie.com/embed/${id}?controls=1&autoplay=1&mute=0`;
const youtubeUtils = {
getPlaylistItems,
getPlaylist,
@@ -164,5 +166,6 @@ const youtubeUtils = {
getSubscriptionsRecusive,
isYouTubeChannelId,
getLive,
getVideoUrl,
};
export default youtubeUtils;
+8 -1
View File
@@ -60,6 +60,13 @@ export type DataItem = {
itunes_duration?: number | string;
itunes_item_image?: string;
media?: Record<string, Record<string, string>>;
attachments?: {
url: string;
mime_type: string;
title?: string;
size_in_bytes?: number;
duration_in_seconds?: number;
}[];
_extra?: Record<string, any> & {
links?: {
@@ -273,7 +280,7 @@ interface RouteItem {
/**
* The handler function of the route
*/
handler: (ctx: Context) => Promise<Data> | Data;
handler: (ctx: Context) => Promise<Data | null> | Data | null;
/**
* An example URL of the route
+13 -11
View File
@@ -28,17 +28,19 @@ const json = (data: Data) => {
authors: typeof item.author === 'string' ? [{ name: item.author }] : item.author,
tags: typeof item.category === 'string' ? [item.category] : item.category,
language: item.language,
attachments: item.enclosure_url
? [
{
url: item.enclosure_url,
mime_type: item.enclosure_type,
title: item.enclosure_title,
size_in_bytes: item.enclosure_length,
duration_in_seconds: item.itunes_duration,
},
]
: undefined,
attachments:
item.attachments ||
(item.enclosure_url
? [
{
url: item.enclosure_url,
mime_type: item.enclosure_type,
title: item.enclosure_title,
size_in_bytes: item.enclosure_length,
duration_in_seconds: item.itunes_duration,
},
]
: undefined),
_extra: item._extra || undefined,
})),
};