Skip to content

Commit 5d9e0f5

Browse files
committed
feat: add video preview url in attachments for bilibili and youtube
1 parent 7b88a1c commit 5d9e0f5

File tree

8 files changed

+70
-18
lines changed

8 files changed

+70
-18
lines changed

lib/routes/bilibili/dynamic.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Route, ViewType } from '@/types';
22
import cache from '@/utils/cache';
33
import got from '@/utils/got';
44
import JSONbig from 'json-bigint';
5-
import utils from './utils';
5+
import utils, { getLiveUrl, getVideoUrl } from './utils';
66
import { parseDate } from '@/utils/parse-date';
77
import { fallback, queryToBoolean } from '@/utils/readable-social';
88
import cacheIn from './cache';
@@ -161,6 +161,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
161161
}
162162
let url = '';
163163
let text = '';
164+
let videoPageUrl;
164165
const major = data.module_dynamic?.major;
165166
if (!major) {
166167
return null;
@@ -179,6 +180,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
179180
const id = useAvid ? `av${archive?.aid}` : archive?.bvid;
180181
url = `https://www.bilibili.com/video/${id}`;
181182
text = `视频地址:<a href=${url}>${url}</a>`;
183+
videoPageUrl = getVideoUrl(archive?.bvid);
182184
break;
183185
}
184186
case 'MAJOR_TYPE_COMMON':
@@ -215,6 +217,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
215217
case 'MAJOR_TYPE_LIVE_RCMD': {
216218
const live_play_info = JSON.parse(major.live_rcmd?.content || '{}')?.live_play_info;
217219
url = `https://live.bilibili.com/${live_play_info?.room_id}`;
220+
videoPageUrl = getLiveUrl(live_play_info?.room_id);
218221
text = `直播间地址:<a href=${url}>${url}</a>`;
219222
break;
220223
}
@@ -224,6 +227,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
224227
return {
225228
url,
226229
text,
230+
videoPageUrl,
227231
};
228232
};
229233

@@ -377,6 +381,15 @@ async function handler(ctx) {
377381
link,
378382
author,
379383
category: category.length ? [...new Set(category)] : undefined,
384+
attachments:
385+
urlResult?.videoPageUrl || originUrlResult?.videoPageUrl
386+
? [
387+
{
388+
url: urlResult?.videoPageUrl || originUrlResult?.videoPageUrl,
389+
mime_type: 'text/html',
390+
},
391+
]
392+
: undefined,
380393
};
381394
})
382395
);

lib/routes/bilibili/ranking.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Route, ViewType } from '@/types';
22
import got from '@/utils/got';
3-
import utils from './utils';
3+
import utils, { getVideoUrl } from './utils';
44

55
// https://www.bilibili.com/v/popular/rank/all
66

@@ -187,7 +187,7 @@ async function handler(ctx) {
187187
// redirect old routes like /bilibili/ranking/0/3/1 or /bilibili/ranking/0/3/1/xxx
188188
const embedArg = args.redirect2 ? '/' + args.redirect2 : '';
189189
ctx.set('redirect', `/bilibili/ranking/${getRidIndexByRid(args.rid_index)}${embedArg}`);
190-
return;
190+
return null;
191191
}
192192

193193
const ridIndex = ctx.req.param('rid_index') || '0';
@@ -216,7 +216,16 @@ async function handler(ctx) {
216216
description: utils.renderUGCDescription(embed, item.pic, item.description || item.title, item.aid, undefined, item.bvid),
217217
pubDate: item.create && new Date(item.create).toUTCString(),
218218
author: item.author,
219-
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}`,
219+
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}`,
220+
image: item.pic,
221+
attachments: item.bvid
222+
? [
223+
{
224+
url: getVideoUrl(item.bvid),
225+
mime_type: 'text/html',
226+
},
227+
]
228+
: undefined,
220229
})),
221230
};
222231
}

lib/routes/bilibili/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ export const renderOGVDescription = (embed: boolean, img: string, description: s
298298
return rendered;
299299
};
300300

301+
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);
302+
export const getLiveUrl = (roomId?: string) => (roomId ? `https://www.bilibili.com/blackboard/live/live-activity-player.html?cid=${roomId}` : undefined);
303+
301304
export default {
302305
lsid,
303306
_uuid,
@@ -313,4 +316,5 @@ export default {
313316
getBangumiItems,
314317
renderUGCDescription,
315318
renderOGVDescription,
319+
getVideoUrl,
316320
};

lib/routes/bilibili/video.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Route, ViewType } from '@/types';
22
import got from '@/utils/got';
33
import cache from './cache';
4-
import utils from './utils';
4+
import utils, { getVideoUrl } from './utils';
55
import logger from '@/utils/logger';
66

77
export const route: Route = {
@@ -73,6 +73,14 @@ async function handler(ctx) {
7373
link: item.created > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
7474
author: name,
7575
comments: item.comment,
76+
attachments: item.bvid
77+
? [
78+
{
79+
url: getVideoUrl(item.bvid),
80+
mime_type: 'text/html',
81+
},
82+
]
83+
: undefined,
7684
})),
7785
};
7886
}

lib/routes/youtube/user.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Route, ViewType } from '@/types';
22
import cache from '@/utils/cache';
3-
import utils from './utils';
3+
import utils, { getVideoUrl } from './utils';
44
import { config } from '@/config';
55
import { parseDate } from '@/utils/parse-date';
66
import ofetch from '@/utils/ofetch';
@@ -106,6 +106,12 @@ async function handler(ctx) {
106106
link: `https://www.youtube.com/watch?v=${videoId}`,
107107
author: snippet.videoOwnerChannelTitle,
108108
image: img.url,
109+
attachments: [
110+
{
111+
url: getVideoUrl(videoId),
112+
mime_type: 'text/html',
113+
},
114+
],
109115
};
110116
}),
111117
};

lib/routes/youtube/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ export const getLive = (id, cache) =>
152152
);
153153
return res;
154154
});
155+
export const getVideoUrl = (id: string) => `https://www.youtube-nocookie.com/embed/${id}?controls=1&autoplay=1&mute=0`;
156+
155157
const youtubeUtils = {
156158
getPlaylistItems,
157159
getPlaylist,
@@ -164,5 +166,6 @@ const youtubeUtils = {
164166
getSubscriptionsRecusive,
165167
isYouTubeChannelId,
166168
getLive,
169+
getVideoUrl,
167170
};
168171
export default youtubeUtils;

lib/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export type DataItem = {
6060
itunes_duration?: number | string;
6161
itunes_item_image?: string;
6262
media?: Record<string, Record<string, string>>;
63+
attachments?: {
64+
url: string;
65+
mime_type: string;
66+
title?: string;
67+
size_in_bytes?: number;
68+
duration_in_seconds?: number;
69+
}[];
6370

6471
_extra?: Record<string, any> & {
6572
links?: {
@@ -273,7 +280,7 @@ interface RouteItem {
273280
/**
274281
* The handler function of the route
275282
*/
276-
handler: (ctx: Context) => Promise<Data> | Data;
283+
handler: (ctx: Context) => Promise<Data | null> | Data | null;
277284

278285
/**
279286
* An example URL of the route

lib/views/json.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ const json = (data: Data) => {
2828
authors: typeof item.author === 'string' ? [{ name: item.author }] : item.author,
2929
tags: typeof item.category === 'string' ? [item.category] : item.category,
3030
language: item.language,
31-
attachments: item.enclosure_url
32-
? [
33-
{
34-
url: item.enclosure_url,
35-
mime_type: item.enclosure_type,
36-
title: item.enclosure_title,
37-
size_in_bytes: item.enclosure_length,
38-
duration_in_seconds: item.itunes_duration,
39-
},
40-
]
41-
: undefined,
31+
attachments:
32+
item.attachments ||
33+
(item.enclosure_url
34+
? [
35+
{
36+
url: item.enclosure_url,
37+
mime_type: item.enclosure_type,
38+
title: item.enclosure_title,
39+
size_in_bytes: item.enclosure_length,
40+
duration_in_seconds: item.itunes_duration,
41+
},
42+
]
43+
: undefined),
4244
_extra: item._extra || undefined,
4345
})),
4446
};

0 commit comments

Comments
 (0)