Skip to content

Commit

Permalink
feat(route): bilibili live keyframe (#18334)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Feb 11, 2025
1 parent 905062f commit 201fac2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions lib/routes/bilibili/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ const getLiveIDFromShortID = (shortID) => {
});
};

const getUsernameFromLiveID = (liveID) => {
const key = `bili-username-from-liveID-${liveID}`;
const getUserInfoFromLiveID = (liveID) => {
const key = `bili-userinfo-from-liveID-${liveID}`;
return cache.tryGet(key, async () => {
const { data: nameResponse } = await got(`https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${liveID}`, {
headers: {
Referer: `https://live.bilibili.com/${liveID}`,
},
});
return nameResponse.data.info.uname;
return nameResponse.data.info;
});
};

Expand Down Expand Up @@ -280,7 +280,7 @@ export default {
getUsernameFromUID,
getUsernameAndFaceFromUID,
getLiveIDFromShortID,
getUsernameFromLiveID,
getUserInfoFromLiveID,
getVideoNameFromId,
getCidFromId,
getAidFromBvid,
Expand Down
26 changes: 14 additions & 12 deletions lib/routes/bilibili/live-room.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Route } from '@/types';
import got from '@/utils/got';
import { DataItem, Route } from '@/types';
import ofetch from '@/utils/ofetch';
import cache from './cache';
import { decodeHTML } from 'entities';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

export const route: Route = {
path: '/live/room/:roomID',
Expand Down Expand Up @@ -32,33 +35,32 @@ async function handler(ctx) {
if (Number.parseInt(roomID, 10) < 10000) {
roomID = await cache.getLiveIDFromShortID(roomID);
}
const name = await cache.getUsernameFromLiveID(roomID);
const info = await cache.getUserInfoFromLiveID(roomID);

const response = await got({
method: 'get',
url: `https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${roomID}&from=room`,
const response = await ofetch(`https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${roomID}&from=room`, {
headers: {
Referer: `https://live.bilibili.com/${roomID}`,
},
});
const data = response.data.data;
const data = response.data;

const liveItem = [];
const liveItem: DataItem[] = [];

if (data.live_status === 1) {
liveItem.push({
title: `${data.title} ${data.live_time}`,
description: `${data.title}<br>${data.description}`,
pubDate: new Date(data.live_time.replace(' ', 'T') + '+08:00').toUTCString(),
description: `<img src="${data.keyframe}"><br>${decodeHTML(data.description)}`,
pubDate: timezone(parseDate(data.live_time), 8),
guid: `https://live.bilibili.com/${roomID} ${data.live_time}`,
link: `https://live.bilibili.com/${roomID}`,
});
}

return {
title: `${name} 直播间开播状态`,
title: `${info.uname} 直播间开播状态`,
link: `https://live.bilibili.com/${roomID}`,
description: `${name} 直播间开播状态`,
description: `${info.uname} 直播间开播状态`,
image: info.face,
item: liveItem,
allowEmpty: true,
};
Expand Down

0 comments on commit 201fac2

Please sign in to comment.