From 5d9e0f557e4b6a31d4c62b76bd2d8bab77482f57 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 21 Jan 2025 18:31:34 +0800 Subject: [PATCH] feat: add video preview url in attachments for bilibili and youtube --- lib/routes/bilibili/dynamic.ts | 15 ++++++++++++++- lib/routes/bilibili/ranking.ts | 15 ++++++++++++--- lib/routes/bilibili/utils.ts | 4 ++++ lib/routes/bilibili/video.ts | 10 +++++++++- lib/routes/youtube/user.ts | 8 +++++++- lib/routes/youtube/utils.ts | 3 +++ lib/types.ts | 9 ++++++++- lib/views/json.ts | 24 +++++++++++++----------- 8 files changed, 70 insertions(+), 18 deletions(-) diff --git a/lib/routes/bilibili/dynamic.ts b/lib/routes/bilibili/dynamic.ts index 101c1c34f..e93632521 100644 --- a/lib/routes/bilibili/dynamic.ts +++ b/lib/routes/bilibili/dynamic.ts @@ -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 = `视频地址:${url}`; + 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 = `直播间地址:${url}`; 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, }; }) ); diff --git a/lib/routes/bilibili/ranking.ts b/lib/routes/bilibili/ranking.ts index ab2924601..06cca8282 100644 --- a/lib/routes/bilibili/ranking.ts +++ b/lib/routes/bilibili/ranking.ts @@ -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, })), }; } diff --git a/lib/routes/bilibili/utils.ts b/lib/routes/bilibili/utils.ts index 3189adccb..d67213229 100644 --- a/lib/routes/bilibili/utils.ts +++ b/lib/routes/bilibili/utils.ts @@ -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, }; diff --git a/lib/routes/bilibili/video.ts b/lib/routes/bilibili/video.ts index 9693a0d1b..543ecb2bc 100644 --- a/lib/routes/bilibili/video.ts +++ b/lib/routes/bilibili/video.ts @@ -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, })), }; } diff --git a/lib/routes/youtube/user.ts b/lib/routes/youtube/user.ts index 2d080019b..5d0fe5a11 100644 --- a/lib/routes/youtube/user.ts +++ b/lib/routes/youtube/user.ts @@ -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', + }, + ], }; }), }; diff --git a/lib/routes/youtube/utils.ts b/lib/routes/youtube/utils.ts index dcf297c35..0b468409a 100644 --- a/lib/routes/youtube/utils.ts +++ b/lib/routes/youtube/utils.ts @@ -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; diff --git a/lib/types.ts b/lib/types.ts index 56f99ce77..5fb66bb4a 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -60,6 +60,13 @@ export type DataItem = { itunes_duration?: number | string; itunes_item_image?: string; media?: Record>; + attachments?: { + url: string; + mime_type: string; + title?: string; + size_in_bytes?: number; + duration_in_seconds?: number; + }[]; _extra?: Record & { links?: { @@ -273,7 +280,7 @@ interface RouteItem { /** * The handler function of the route */ - handler: (ctx: Context) => Promise | Data; + handler: (ctx: Context) => Promise | Data | null; /** * An example URL of the route diff --git a/lib/views/json.ts b/lib/views/json.ts index 23e8f847a..c6f00226f 100644 --- a/lib/views/json.ts +++ b/lib/views/json.ts @@ -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, })), };