get-bilibili-rank
Retrieve Bilibili video rankings across categories like animation, music, and gaming to analyze content trends and popular videos.
Instructions
获取哔哩哔哩视频排行榜,包含全站、动画、音乐、游戏等多个分区的热门视频,反映当下年轻人的内容消费趋势
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| type | No | 排行榜分区 |
Implementation Reference
- src/tools/bilibili.ts:165-172 (handler)Core handler function that orchestrates the primary and backup methods to fetch Bilibili video rankings.const getBilibiliRank = async (type: number) => { try { return await mainGetBilibiliRank(type); } catch (error) { logger.error(error); return await backupGetBilibiliRank(type); } };
- src/tools/bilibili.ts:6-25 (schema)Zod input schema defining the optional 'type' parameter for selecting different Bilibili ranking categories.const bilibiliRequestSchema = z.object({ type: z .union([ z.literal(0).describe('全站'), z.literal(1).describe('动画'), z.literal(3).describe('音乐'), z.literal(4).describe('游戏'), z.literal(5).describe('娱乐'), z.literal(188).describe('科技'), z.literal(119).describe('鬼畜'), z.literal(129).describe('舞蹈'), z.literal(155).describe('时尚'), z.literal(160).describe('生活'), z.literal(168).describe('国创相关'), z.literal(181).describe('影视'), ]) .optional() .default(0) .describe('排行榜分区'), });
- src/tools/bilibili.ts:174-182 (registration)Tool registration via defineToolConfig, defining name, description, schema reference, and inline handler that parses args and calls the core function.export default defineToolConfig({ name: 'get-bilibili-rank', description: '获取哔哩哔哩视频排行榜,包含全站、动画、音乐、游戏等多个分区的热门视频,反映当下年轻人的内容消费趋势', zodSchema: bilibiliRequestSchema, func: async (args: unknown) => { const { type } = bilibiliRequestSchema.parse(args); return getBilibiliRank(type); }, });
- src/tools/bilibili.ts:99-137 (helper)Primary helper function performing the HTTP request to Bilibili's ranking API v2 with WBI authentication and data transformation.const mainGetBilibiliRank = async (type: number) => { const wbiData = await getBiliWbi(); const resp = await http.get<{ code: number; data: { list: any[]; }; message: string; }>(`https://api.bilibili.com/x/web-interface/ranking/v2?rid=${type}&type=all&${wbiData}`, { headers: { Referer: 'https://www.bilibili.com/ranking/all', 'User-Agent': UA, Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1', }, }); if (resp.data.code !== 0) { throw new Error(resp.data.message); } return resp.data.data.list.map((item) => ({ title: item.title, description: item.desc || '该视频暂无简介', cover: item.pic, author: item.owner?.name, publishTime: dayjs.unix(item.pubdate).toISOString(), view: item.stat?.view || 0, link: item.short_link_v2 || `https://www.bilibili.com/video/${item.bvid}`, })); };
- src/tools/bilibili.ts:86-97 (helper)Helper function to retrieve or generate cached WBI query parameters required for authenticated Bilibili API calls.const getBiliWbi = async (): Promise<string> => { const CACHE_KEY = 'bilibili-wbi'; const cachedData = cacheStorage.getItem(CACHE_KEY); if (cachedData) { return cachedData; } const { imgKey, subKey } = await getWbiKeys(); const params = { foo: '114', bar: '514', baz: '1919810' }; const query = encodeWbi(params, imgKey, subKey); cacheStorage.setItem(CACHE_KEY, query); return query; };