Skip to content

Commit

Permalink
feat: add video preview url in attachments for bilibili and youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jan 21, 2025
1 parent 7b88a1c commit 5d9e0f5
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 18 deletions.
15 changes: 14 additions & 1 deletion lib/routes/bilibili/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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':
Expand Down Expand Up @@ -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;
}
Expand All @@ -224,6 +227,7 @@ const getUrl = (item?: Item2, useAvid = false) => {
return {
url,
text,
videoPageUrl,
};
};

Expand Down Expand Up @@ -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,
};
})
);
Expand Down
15 changes: 12 additions & 3 deletions lib/routes/bilibili/ranking.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 changes: 4 additions & 0 deletions lib/routes/bilibili/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -313,4 +316,5 @@ export default {
getBangumiItems,
renderUGCDescription,
renderOGVDescription,
getVideoUrl,
};
10 changes: 9 additions & 1 deletion lib/routes/bilibili/video.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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,
})),
};
}
8 changes: 7 additions & 1 deletion lib/routes/youtube/user.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
},
],
};
}),
};
Expand Down
3 changes: 3 additions & 0 deletions lib/routes/youtube/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -164,5 +166,6 @@ const youtubeUtils = {
getSubscriptionsRecusive,
isYouTubeChannelId,
getLive,
getVideoUrl,
};
export default youtubeUtils;
9 changes: 8 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: {
Expand Down Expand Up @@ -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
Expand Down
24 changes: 13 additions & 11 deletions lib/views/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})),
};
Expand Down

0 comments on commit 5d9e0f5

Please sign in to comment.