From 004fabf908e719e6682cb97f7777e9f6c3114871 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sun, 31 Mar 2024 01:39:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(route):=20=E8=BF=81=E7=A7=BB=20bilibili?= =?UTF-8?q?=20=E7=94=A8=E6=88=B7=E5=8A=A8=E6=80=81=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=88=B0=E6=96=B0=E7=89=88=20web=20=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #14990 --- lib/routes/bilibili/api-interface.d.ts | 384 +++++++++++++++++++++++++ lib/routes/bilibili/dynamic.ts | 354 ++++++++++++----------- lib/routes/bilibili/utils.ts | 2 +- 3 files changed, 564 insertions(+), 176 deletions(-) create mode 100644 lib/routes/bilibili/api-interface.d.ts diff --git a/lib/routes/bilibili/api-interface.d.ts b/lib/routes/bilibili/api-interface.d.ts new file mode 100644 index 000000000..2ad1a39bd --- /dev/null +++ b/lib/routes/bilibili/api-interface.d.ts @@ -0,0 +1,384 @@ +interface Likeicon { + action_url: string; + end_url: string; + id: number; + start_url: string; +} +interface Basic { + comment_id_str: string; + comment_type: number; + jump_url?: string; + like_icon: Likeicon; + rid_str: string; +} +interface Containersize { + height: number; + width: number; +} +interface Posspec { + axis_x: number; + axis_y: number; + coordinate_pos: number; +} +interface Renderspec { + opacity: number; +} +interface Generalspec { + pos_spec: Posspec; + render_spec: Renderspec; + size_spec: Containersize; +} +interface AVATARLAYER {} +interface Webcssstyle { + borderRadius: string; + 'background-color'?: string; + border?: string; + boxSizing?: string; +} +interface Generalconfig { + web_css_style: Webcssstyle; +} +interface GENERALCFG { + config_type: number; + general_config: Generalconfig; +} +interface Tags { + AVATAR_LAYER?: AVATARLAYER; + GENERAL_CFG: GENERALCFG; + ICON_LAYER?: AVATARLAYER; +} +interface Layerconfig { + is_critical?: boolean; + tags: Tags; +} +interface Remote { + bfs_style: string; + url: string; +} +interface Imagesrc { + placeholder?: number; + remote?: Remote; + src_type: number; + local?: number; +} +interface Resimage { + image_src: Imagesrc; +} +interface Resource { + res_image: Resimage; + res_type: number; +} +interface Layer { + general_spec: Generalspec; + layer_config: Layerconfig; + resource: Resource; + visible: boolean; +} +interface Fallbacklayers { + is_critical_group: boolean; + layers: Layer[]; +} +interface Avatar { + container_size: Containersize; + fallback_layers: Fallbacklayers; + mid: string; +} +interface Fan { + color: string; + is_fan: boolean; + num_str: string; + number: number; +} +interface Decorate { + card_url: string; + fan: Fan; + id: number; + jump_url: string; + name: string; + type: number; +} +interface Officialverify { + desc: string; + type: number; +} +interface Pendant { + expire: number; + image: string; + image_enhance: string; + image_enhance_frame: string; + n_pid: number; + name: string; + pid: number; +} +interface Label { + bg_color: string; + bg_style: number; + border_color: string; + img_label_uri_hans: string; + img_label_uri_hans_static: string; + img_label_uri_hant: string; + img_label_uri_hant_static: string; + label_theme: string; + path: string; + text: string; + text_color: string; + use_img_label: boolean; +} +interface Vip { + avatar_subscript: number; + avatar_subscript_url: string; + due_date: number; + label: Label; + nickname_color: string; + status: number; + theme_type: number; + type: number; +} +interface Moduleauthor { + avatar: Avatar; + decorate?: Decorate; + face: string; + face_nft: boolean; + following: boolean; + jump_url: string; + label: string; + mid: number; + name: string; + official_verify: Officialverify; + pendant: Pendant; + pub_action: string; + pub_location_text?: string; + pub_time: string; + pub_ts: number; + type: AuthorType; + vip: Vip; +} +interface Jumpstyle { + icon_url: string; + text: string; +} +interface Button { + jump_style: Jumpstyle; + jump_url: string; + type: number; +} + +interface Common { + button: Button; + cover: string; + desc1: string; + desc2: string; + head_text: string; + id_str: string; + jump_url: string; + style: number; + sub_type: string; + title: string; +} +interface Additional { + common: Common; + type: string; +} +interface Richtextnode { + orig_text: string; + rid?: string; + text: string; + type: string; + jump_url?: string; + emoji?: Emoji; +} +interface Desc { + rich_text_nodes: Richtextnode[]; + text: string; +} +interface Pic { + height: number; + size: number; + url: string; + width: number; +} +interface Opus { + fold_action: string[]; + jump_url: string; + pics: Pic[]; + summary: Desc; + title?: string; +} +interface Badge { + bg_color: string; + color: string; + icon_url?: string; + text: string; +} +interface Stat { + danmaku: string; + play: string; +} +interface Pgc { + badge: Badge; + cover: string; + epid: number; + jump_url: string; + season_id: number; + stat: Stat; + stat_hidden: number; + sub_type: number; + title: string; + type: number; +} +interface Live { + badge: Badge; + cover: string; + desc_first: string; + desc_second: string; + id: number; + jump_url: string; + live_state: number; + reserve_type: number; + title: string; +} +interface Common2 { + badge: Badge; + biz_type: number; + cover: string; + desc: string; + id: string; + jump_url: string; + label: string; + sketch_id: string; + style: number; + title: string; +} +interface Archive { + aid: string; + badge: Badge; + bvid: string; + cover: string; + desc: string; + disable_preview: number; + duration_text: string; + jump_url: string; + stat: Stat; + title: string; + type: number; +} +interface Major { + opus?: Opus; + type: MajorType; + archive?: Archive; + pgc?: Pgc; + live?: Live; + common?: Common2; +} +interface Moduledynamic { + additional?: Additional; + desc?: Desc; + major?: Major; + topic?: any; +} +interface Emoji { + icon_url: string; + size: number; + text: string; + type: number; +} + +interface Item { + desc: Desc; + type: number; +} +interface Moduleinteraction { + items: Item[]; +} +interface Threepointitem { + label: string; + type: string; +} +interface Modulemore { + three_point_items: Threepointitem[]; +} +interface Comment { + count: number; + forbidden: boolean; +} +interface Like { + count: number; + forbidden: boolean; + status: boolean; +} +interface Modulestat { + comment: Comment; + forward: Comment; + like: Like; +} +interface Moduletag { + text: string; +} +interface Modules { + module_author: Moduleauthor; + module_dynamic: Moduledynamic; + module_interaction?: Moduleinteraction; + module_more?: Modulemore; + module_stat?: Modulestat; + module_tag?: Moduletag; +} + +export interface Orig { + basic: Basic; + id_str: string; + modules: Modules; + type: DynamicType; + visible: boolean; +} +export interface Item2 { + basic: Basic; + id_str: string; + modules: Modules; + type: DynamicType; + visible: boolean; + orig?: Orig; +} +export interface Data { + has_more: boolean; + items: Item2[]; + offset: string; + update_baseline: string; + update_num: number; +} +// https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space +export interface BilibiliWebDynamicResponse { + code: number; + message: string; + ttl: number; + data: Data; +} + +/** + * 作者类型 + * AUTHOR_TYPE_PGC:番剧 + * AUTHOR_TYPE_NORMAL:用户 + */ +export type AuthorType = 'AUTHOR_TYPE_PGC' | 'AUTHOR_TYPE_NORMAL'; + +/** + * 动态类型 + * DYNAMIC_TYPE_FORWARD:转发动态 + * DYNAMIC_TYPE_AV:视频投稿 + * DYNAMIC_TYPE_PGC_UNION:番剧 + * DYNAMIC_TYPE_DRAW:图文 + * DYNAMIC_TYPE_WORD:文本 + * DYNAMIC_TYPE_ARTICLE:专栏 + * DYNAMIC_TYPE_LIVE:直播 + * DYNAMIC_TYPE_COMMON_SQUARE:普通正方形(专题页) + */ +export type DynamicType = 'DYNAMIC_TYPE_FORWARD' | 'DYNAMIC_TYPE_AV' | 'DYNAMIC_TYPE_PGC_UNION' | 'DYNAMIC_TYPE_DRAW' | 'DYNAMIC_TYPE_WORD' | 'DYNAMIC_TYPE_ARTICLE'; + +/** + * 主要类型 + * MAJOR_TYPE_ARCHIVE:视频 + * MAJOR_TYPE_PGC:番剧 + * MAJOR_TYPE_OPUS:图文混排 + * MAJOR_TYPE_COMMON:普通 + * MAJOR_TYPE_LIVE:直播 + */ +export type MajorType = 'MAJOR_TYPE_ARCHIVE' | 'MAJOR_TYPE_PGC' | 'MAJOR_TYPE_OPUS' | 'MAJOR_TYPE_COMMON' | 'MAJOR_TYPE_LIVE'; diff --git a/lib/routes/bilibili/dynamic.ts b/lib/routes/bilibili/dynamic.ts index 7873ccb46..b514065ec 100644 --- a/lib/routes/bilibili/dynamic.ts +++ b/lib/routes/bilibili/dynamic.ts @@ -6,67 +6,7 @@ import utils from './utils'; import { parseDate } from '@/utils/parse-date'; import { fallback, queryToBoolean } from '@/utils/readable-social'; import cacheIn from './cache'; - -/** - @by CaoMeiYouRen 2020-05-05 添加注释 - 注意1:以下均以card为根对象 - 注意2:直接动态没有origin,转发动态有origin - 注意3:转发动态格式统一为: - - user.uname: 用户名 - - item.content: 正文 - - item.tips: 原动态结果(例如:源动态已被作者删除、图文资源已失效) - - origin: 与原动态一致 - 注意4:本总结并不保证完善,而且未来B站可能会修改接口,因此仅供参考 - - B站的动态种类繁多,大致可以总结为以下几种: - - 文字动态 - - user.uname: 用户名 - - item.content: 正文 - - 图文动态 - - user.name: 用户名 - - item.title: 标题 - - item.description: 简介 - - item.pictures: { img_src: String }[] 图片数组,图片地址在每项的 img_src 中 - - 视频动态 - - aid: av号(以card为根对象没有bv号) - - owner.name :用户名 - - pic: 封面 - - title: 视频标题 - - desc: 视频简介 - - 专栏动态 - - author.name: 用户名 - - image_urls: String[] 封面数组 - - id: cv号 - - title: 标题 - - summary: 简介 - - 音频动态 - - id: auId 音频id - - upper: 上传的用户名称 - - title: 音频标题 - - author: 音频作者 - - cover: 音频封面 - - 投票动态 - - user.uname: 用户名 - - item.content: 正文 - - 活动专题页 - - user.uname 用户名 - - vest.content 正文 - - sketch.title 活动标题 - - sketch.desc_text 活动简介 - - sketch.cover_url 活动封面 - - sketch.target_url 活动地址 - - 番剧/电视剧/电影等专题页 - - cover 单集封面 - - index_title 单集标题 - - url 视频地址 - - apiSeasonInfo.title 番剧名称 - - apiSeasonInfo.cover 番剧封面 - - 直播间动态 - - roomid 直播间id - - uname 用户名 - - title 直播间标题 - - cover 直播间封面 -*/ +import { BilibiliWebDynamicResponse, Item2, Modules } from './api-interface'; export const route: Route = { path: '/user/dynamic/:uid/:routeParams?', @@ -74,9 +14,19 @@ export const route: Route = { example: '/bilibili/user/dynamic/2267573', parameters: { uid: '用户 id, 可在 UP 主主页中找到', routeParams: '额外参数;请参阅以下说明和表格' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'BILIBILI_COOKIE_*', + optional: true, + description: `BILIBILI_COOKIE_{uid}: 用于用户关注动态系列路由,对应 uid 的 b 站用户登录后的 Cookie 值,\`{uid}\` 替换为 uid,如 \`BILIBILI_COOKIE_2267573\`,获取方式: +1. 打开 [https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=0&type=8](https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=0&type=8) +2. 打开控制台,切换到 Network 面板,刷新 +3. 点击 dynamic_new 请求,找到 Cookie +4. 视频和专栏,UP 主粉丝及关注只要求 \`SESSDATA\` 字段,动态需复制整段 Cookie`, + }, + ], requirePuppeteer: false, - antiCrawler: false, + antiCrawler: true, supportBT: false, supportPodcast: false, supportScihub: false, @@ -106,6 +56,97 @@ export const route: Route = { :::`, }; +const getTitle = (data: Modules) => data.module_dynamic.major?.opus?.title || data.module_dynamic.major?.archive?.title || data.module_dynamic.major?.pgc?.title; +const getDes = (data: Modules) => data.module_dynamic.desc?.text || data.module_dynamic.major?.opus?.summary?.text; + +const getOriginTitle = (data?: Modules) => data?.module_dynamic?.major?.opus?.title || data?.module_dynamic?.major?.archive?.title || data?.module_dynamic.major?.pgc?.title; +const getOriginDes = (data?: Modules) => data?.module_dynamic.desc?.text || data?.module_dynamic?.major?.opus?.summary?.text || data?.module_dynamic?.major?.archive?.desc; +const getOriginName = (data?: Modules) => data?.module_author?.name; +const getIframe = (data?: Modules, disableEmbed: boolean = false) => { + if (disableEmbed) { + return ''; + } + const aid = data?.module_dynamic?.major?.archive?.aid; + const bvid = data?.module_dynamic?.major?.archive?.bvid; + if (!aid) { + return ''; + } + return `
${utils.iframe(aid, null, bvid)}
`; +}; + +const getImgs = (data: Modules) => { + const imgUrls: string[] = []; + const dynamic = data.module_dynamic; + // 动态图片/专栏封面 + if (dynamic.major?.opus?.pics) { + imgUrls.push(...dynamic.major.opus.pics.map((e) => e.url)); + } + // 视频封面 + if (dynamic.major?.archive?.cover) { + imgUrls.push(dynamic.major.archive.cover); + } + // 番剧封面 + if (dynamic.major?.pgc?.cover) { + imgUrls.push(dynamic.major.pgc.cover); + } + // 直播间封面 + if (dynamic.major?.live?.cover) { + imgUrls.push(dynamic.major.live.cover); + } + // 专题页封面 + if (dynamic.major?.common?.cover) { + imgUrls.push(dynamic.major.common.cover); + } + return imgUrls.map((url) => ``); +}; + +const getUrl = (item?: Item2, useAvid = false) => { + const data = item?.modules; + if (!data) { + return null; + } + let url = ''; + let text = ''; + switch (data.module_dynamic.major?.type) { + case 'MAJOR_TYPE_ARCHIVE': { + const archive = data?.module_dynamic?.major?.archive; + const id = useAvid ? `av${archive?.aid}` : archive?.bvid; + url = `https://www.bilibili.com/video/${id}`; + text = `
视频地址:${url}`; + break; + } + case 'MAJOR_TYPE_PGC': { + const pgc = data?.module_dynamic?.major?.pgc; + url = `https://www.bilibili.com/bangumi/play/ep${pgc?.epid}&season_id=${pgc?.season_id}`; + text = `
番剧地址:${url}`; + break; + } + case 'MAJOR_TYPE_OPUS': + if (item?.type === 'DYNAMIC_TYPE_ARTICLE') { + url = `https:${data?.module_dynamic?.major?.opus?.jump_url}`; + text = `
专栏地址:${url}`; + } else if (item?.type === 'DYNAMIC_TYPE_DRAW') { + url = data?.module_dynamic?.major?.opus?.jump_url || ''; + text = `
图文地址:${url}`; + } + break; + case 'MAJOR_TYPE_COMMON': + url = data?.module_dynamic?.major?.common?.jump_url || ''; + text = `
地址:${url}`; + break; + case 'MAJOR_TYPE_LIVE': + url = `https://live.bilibili.com/${data?.module_dynamic?.major?.live?.id}`; + text = `
直播间地址:${url}`; + break; + default: + return null; + } + return { + url, + text, + }; +}; + async function handler(ctx) { const uid = ctx.req.param('uid'); const routeParams = Object.fromEntries(new URLSearchParams(ctx.req.param('routeParams'))); @@ -115,65 +156,39 @@ async function handler(ctx) { const useAvid = fallback(undefined, queryToBoolean(routeParams.useAvid), false); const directLink = fallback(undefined, queryToBoolean(routeParams.directLink), false); + const cookie = cacheIn.getCookie(); + const response = await got({ method: 'get', - url: `https://api.vc.bilibili.com/dynamic_svr/v2/dynamic_svr/space_history?host_uid=${uid}`, + url: `https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space`, + searchParams: { + host_mid: uid, + platform: 'web', + features: 'itemOpusStyle,listOnlyfans,opusBigCover,onlyfansVote', + }, headers: { Referer: `https://space.bilibili.com/${uid}/`, + Cookie: cookie, }, }); - const cards = JSONbig.parse(response.body).data.cards; + const body = JSONbig.parse(response.body); + if (body?.code === -352) { + throw new Error('风控校验失败!请手动配置 Cookie 后重试'); + } + const cards = (body as BilibiliWebDynamicResponse)?.data?.items; const usernameAndFace = await cacheIn.getUsernameAndFaceFromUID(uid); - const author = usernameAndFace[0] ?? cards[0]?.desc?.user_profile?.info.uname; - const face = usernameAndFace[1] ?? cards[0]?.desc?.user_profile?.info?.face; + const author = usernameAndFace[0] ?? cards[0]?.modules?.module_author?.name; + const face = usernameAndFace[1] ?? cards[0]?.modules?.module_author?.face; cache.set(`bili-username-from-uid-${uid}`, author); cache.set(`bili-userface-from-uid-${uid}`, face); - const getTitle = (data) => (data ? data.title || data.description || data.content || (data.vest && data.vest.content) || '' : ''); - const getDes = (data) => - data.dynamic || data.desc || data.description || data.content || data.summary || (data.vest && data.vest.content) + (data.sketch && `
${data.sketch.title}
${data.sketch.desc_text}`) || data.intro || ''; - const getOriginDes = (data) => (data && (data.apiSeasonInfo && data.apiSeasonInfo.title && `//转发自: ${data.apiSeasonInfo.title}`) + (data.index_title && `
${data.index_title}`)) || ''; - const getOriginName = (data) => data.uname || (data.author && data.author.name) || (data.upper && data.upper.name) || (data.user && (data.user.uname || data.user.name)) || (data.owner && data.owner.name) || ''; - const getOriginTitle = (data) => (data.title ? `${data.title}
` : ''); - const getIframe = (data) => (!disableEmbed && data && data.aid ? `

${utils.iframe(data.aid)}
` : ''); - const getImgs = (data) => { - let imgs = ''; - // 动态图片 - if (data.pictures) { - for (let i = 0; i < data.pictures.length; i++) { - imgs += ``; - } - } - // 专栏封面 - if (data.image_urls) { - for (let i = 0; i < data.image_urls.length; i++) { - imgs += ``; - } - } - // 视频封面 - if (data.pic) { - imgs += ``; - } - // 音频/番剧/直播间封面/小视频封面 - if (data.cover && data.cover.unclipped) { - imgs += ``; - } else if (data.cover) { - imgs += ``; - } - // 专题页封面 - if (data.sketch && data.sketch.cover_url) { - imgs += ``; - } - return imgs; - }; - const items = await Promise.all( cards.map(async (item) => { - const parsed = JSONbig.parse(item.card); + // const parsed = JSONbig.parse(item.card); - const data = getTitle(parsed.item) ? parsed.item : parsed; - const origin = parsed.origin ? JSONbig.parse(parsed.origin) : null; + const data = item.modules; + const origin = item?.orig?.modules; // img let imgHTML = ''; @@ -181,93 +196,82 @@ async function handler(ctx) { imgHTML += getImgs(data); if (origin) { - imgHTML += getImgs(origin.item || origin); - } - // video小视频 - let videoHTML = ''; - if (data.video_playurl) { - videoHTML += ``; + imgHTML += getImgs(origin); } + // some rss readers disallow http content. // 部分 RSS 阅读器要求内容必须使用https传输 // bilibili short video does support https request, but https request may timeout ocassionally. // to maximize content availability, here add two source tags. // bilibili的API中返回的视频地址采用http,然而经验证,短视频地址支持https访问,但偶尔会返回超时错误(可能是网络原因)。 // 因此保险起见加入两个source标签 + // link let link = ''; - if (data.dynamic_id) { - link = `https://t.bilibili.com/${data.dynamic_id}`; - } else if (item.desc?.dynamic_id) { - link = `https://t.bilibili.com/${item.desc.dynamic_id}`; + if (item.id_str) { + link = `https://t.bilibili.com/${item.id_str}`; } // emoji - let data_content = getDes(data); + let data_content = getDes(data) || ''; // 换行处理 data_content = data_content.replaceAll('\r\n', '
').replaceAll('\n', '
'); - if (item.display.emoji_info && showEmoji) { - const emoji = item.display.emoji_info.emoji_details; - for (const item of emoji) { - data_content = data_content.replaceAll( - new RegExp(`\\${item.text}`.replaceAll(/\\?/g, '\\?'), 'g'), - `${item.text}` - ); + if (data.module_dynamic?.desc?.rich_text_nodes?.length && showEmoji) { + const nodes = data.module_dynamic?.desc?.rich_text_nodes; + for (const node of nodes) { + if (node?.emoji) { + const emoji = node.emoji; + data_content = data_content.replaceAll( + emoji.text, + `${emoji.text}` + ); + } } } - const getUrl = (data) => { - if (!data) { - return ''; - } - let url; - if (data.aid) { - const id = useAvid ? `av${data.aid}` : item?.desc?.bvid || item?.desc?.origin?.bvid; - url = `https://www.bilibili.com/video/${id}`; - directLink && (link = url); - return `
视频地址:${url}`; - } - if (data.image_urls) { - url = `https://www.bilibili.com/read/cv${data.id}`; - directLink && (link = url); - return `
专栏地址:${url}`; - } - if (data.upper) { - url = `https://www.bilibili.com/audio/au${data.id}`; - directLink && (link = url); - return `
音频地址:${url}`; - } - if (data.roomid) { - url = `https://live.bilibili.com/${data.roomid}`; - directLink && (link = url); - return `
直播间地址:${url}`; - } - if (data.sketch) { - url = data.sketch.target_url; - directLink && (link = url); - return `
活动地址:${url}`; - } - if (data.url) { - url = data.url; - directLink && (link = url); - return `
地址:${url}`; - } - return ''; - }; - if (data.image_urls && displayArticle) { - data_content = (await cacheIn.getArticleDataFromCvid(data.id, uid)).description; + if (item.type === 'DYNAMIC_TYPE_ARTICLE' && displayArticle) { + // 抓取专栏全文 + const cvid = data.module_dynamic.major?.opus?.jump_url?.match?.(/cv(\d+)/)?.[0]; + if (cvid) { + data_content = (await cacheIn.getArticleDataFromCvid(cvid, uid)).description || ''; + } } + const urlResult = getUrl(item, useAvid); + const originUrlResult = getUrl(item?.orig, useAvid); + let urlText = ''; + if (urlResult) { + urlText += urlResult.text; + if (directLink) { + link = urlResult.url; + } + } + if (originUrlResult) { + urlText += originUrlResult.text; + if (directLink) { + link = originUrlResult.url; + } + } + + let originDescription = ''; + const originName = getOriginName(origin); + const originTitle = getOriginTitle(origin); + const originDes = getOriginDes(origin); + if (originName) { + originDescription += `
//转发自: @${getOriginName(origin)}: `; + } + if (originTitle) { + originDescription += originTitle; + } + if (originDes) { + originDescription += `
${originDes}`; + } + const description = data_content; + const imgHTMLSource = imgHTML ? `
${imgHTML}` : ''; return { - title: getTitle(data), - description: (() => { - const description = data_content || getDes(data); - const originName = origin && getOriginName(origin) ? `

//转发自: @${getOriginName(origin)}: ${getOriginTitle(origin.item || origin)}${getDes(origin.item || origin)}` : getOriginDes(origin); - const imgHTMLSource = imgHTML ? `
${imgHTML}` : ''; - const videoHTMLSource = videoHTML ? `
${videoHTML}` : ''; - return `${description}${originName}
${getUrl(data)}${getUrl(origin)}${getIframe(data)}${getIframe(origin)}${imgHTMLSource}${videoHTMLSource}`; - })(), - pubDate: item.desc?.timestamp ? parseDate(item.desc.timestamp, 'X') : null, + title: getTitle(data) || '', + description: `${description}${originDescription}
${urlText}${getIframe(data, disableEmbed)}${getIframe(origin, disableEmbed)}${imgHTMLSource}`, + pubDate: data.module_author?.pub_ts ? parseDate(data.module_author.pub_ts, 'X') : undefined, link, author, }; diff --git a/lib/routes/bilibili/utils.ts b/lib/routes/bilibili/utils.ts index 613e3a153..15fd78850 100644 --- a/lib/routes/bilibili/utils.ts +++ b/lib/routes/bilibili/utils.ts @@ -2,7 +2,7 @@ import { config } from '@/config'; import md5 from '@/utils/md5'; import CryptoJS from 'crypto-js'; -function iframe(aid, page, bvid) { +function iframe(aid: any, page?: any, bvid?: any) { return ``; From 5a4dc4820b82b2352c580e618603658de7d5f22f Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sun, 31 Mar 2024 13:59:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(route):=20=E6=A0=B9=E6=8D=AE=20bilibili?= =?UTF-8?q?=20=E5=8A=A8=E6=80=81=E6=96=87=E6=A1=A3=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=B1=BB=E5=9E=8B=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/bilibili/api-interface.d.ts | 162 ++++++++++++++++++++----- lib/routes/bilibili/dynamic.ts | 146 +++++++++++++++------- 2 files changed, 232 insertions(+), 76 deletions(-) diff --git a/lib/routes/bilibili/api-interface.d.ts b/lib/routes/bilibili/api-interface.d.ts index 2ad1a39bd..eae9f0e40 100644 --- a/lib/routes/bilibili/api-interface.d.ts +++ b/lib/routes/bilibili/api-interface.d.ts @@ -262,19 +262,93 @@ interface Archive { title: string; type: number; } -interface Major { - opus?: Opus; - type: MajorType; - archive?: Archive; - pgc?: Pgc; - live?: Live; - common?: Common2; +interface UgcSeason { + aid: number; // 视频AV号 + badge: Badge; // 角标信息 + cover: string; // 视频封面 + desc: string; // 视频简介 + disable_preview: number; // 0 + duration_text: string; // 时长 + jump_url: string; // 跳转URL + stat: Stat; // 统计信息 + title: string; // 视频标题 } + +interface Article { + covers: string[]; // 封面图数组,最多三张 + desc: string; // 文章摘要 + id: number; // 文章CV号 + jump_url: string; // 文章跳转地址 + label: string; // 文章阅读量 + title: string; // 文章标题 +} + +interface DrawItem { + height: number; // 图片高度 + size: number; // 图片大小,单位KB + src: string; // 图片URL + tags: any[]; // 标签数组 + width: number; // 图片宽度 +} + +interface Draw { + id: number; // 对应相簿id + items: DrawItem[]; // 图片信息列表 +} + +interface LiveRcmd { + content: string; // 直播间内容JSON + reserve_type: number; // 0 +} + +interface Course { + badge: Badge; // 角标信息 + cover: string; // 封面图URL + desc: string; // 更新状态描述 + id: number; // 课程id + jump_url: string; // 跳转URL + sub_title: string; // 课程副标题 + title: string; // 课程标题 +} + +interface Music { + cover: string; // 音频封面 + id: number; // 音频AUID + jump_url: string; // 跳转URL + label: string; // 音频分类 + title: string; // 音频标题 +} + +interface None { + tips: string; // 动态失效显示文案 +} + +interface Major { + type: MajorType; + ugc_season?: UgcSeason; // 合集信息 + article?: Article; // 专栏 + draw?: Draw; // 带图动态 + archive?: Archive; // 视频信息 + live_rcmd?: LiveRcmd; // 直播状态 + common?: Common2; // 一般类型 + opus?: Opus; // 图文动态 + pgc?: Pgc; // 剧集信息 + courses?: Course; // 课程信息 + music?: Music; // 音频信息 + live?: Live; // 直播信息 + none?: None; // 动态失效 +} +interface Topic { + id: number; // 话题id + jump_url: string; // 跳转URL + name: string; // 话题名称 +} + interface Moduledynamic { additional?: Additional; desc?: Desc; major?: Major; - topic?: any; + topic?: Topic; } interface Emoji { icon_url: string; @@ -315,8 +389,8 @@ interface Moduletag { text: string; } interface Modules { - module_author: Moduleauthor; - module_dynamic: Moduledynamic; + module_author?: Moduleauthor; + module_dynamic?: Moduledynamic; module_interaction?: Moduleinteraction; module_more?: Modulemore; module_stat?: Modulestat; @@ -355,30 +429,56 @@ export interface BilibiliWebDynamicResponse { /** * 作者类型 - * AUTHOR_TYPE_PGC:番剧 - * AUTHOR_TYPE_NORMAL:用户 + * 更多类型请参考:https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/dynamic/dynamic_enum.md#%E4%BD%9C%E8%80%85%E7%B1%BB%E5%9E%8B */ -export type AuthorType = 'AUTHOR_TYPE_PGC' | 'AUTHOR_TYPE_NORMAL'; +export type AuthorType = 'AUTHOR_TYPE_PGC' | 'AUTHOR_TYPE_NORMAL' | 'AUTHOR_TYPE_UGC_SEASON' | 'AUTHOR_TYPE_NONE'; /** - * 动态类型 - * DYNAMIC_TYPE_FORWARD:转发动态 - * DYNAMIC_TYPE_AV:视频投稿 - * DYNAMIC_TYPE_PGC_UNION:番剧 - * DYNAMIC_TYPE_DRAW:图文 - * DYNAMIC_TYPE_WORD:文本 - * DYNAMIC_TYPE_ARTICLE:专栏 - * DYNAMIC_TYPE_LIVE:直播 - * DYNAMIC_TYPE_COMMON_SQUARE:普通正方形(专题页) + * 动态类型 + * 更多类型请参考:https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/dynamic/dynamic_enum.md#%E5%8A%A8%E6%80%81%E7%B1%BB%E5%9E%8B */ -export type DynamicType = 'DYNAMIC_TYPE_FORWARD' | 'DYNAMIC_TYPE_AV' | 'DYNAMIC_TYPE_PGC_UNION' | 'DYNAMIC_TYPE_DRAW' | 'DYNAMIC_TYPE_WORD' | 'DYNAMIC_TYPE_ARTICLE'; - +export type DynamicType = + | 'DYNAMIC_TYPE_NONE' // 无效动态, 示例: 716510857084796964 + | 'DYNAMIC_TYPE_FORWARD' // 动态转发 + | 'DYNAMIC_TYPE_AV' // 投稿视频 + | 'DYNAMIC_TYPE_PGC' // 剧集(番剧、电影、纪录片) + | 'DYNAMIC_TYPE_COURSES' // 课程 + | 'DYNAMIC_TYPE_WORD' // 纯文字动态, 示例: 718377531474968613 + | 'DYNAMIC_TYPE_DRAW' // 带图动态, 示例: 718384798557536290 + | 'DYNAMIC_TYPE_ARTICLE' // 投稿专栏, 示例: 718372214316990512 + | 'DYNAMIC_TYPE_MUSIC' // 音乐 + | 'DYNAMIC_TYPE_COMMON_SQUARE' // 装扮, 剧集点评, 普通分享, 示例: 551309621391003098, 716503778995470375, 716481612656672789 + | 'DYNAMIC_TYPE_COMMON_VERTICAL' // 垂直动态 + | 'DYNAMIC_TYPE_LIVE' // 直播间分享, 示例: 216042859353895488 + | 'DYNAMIC_TYPE_MEDIALIST' // 收藏夹, 示例: 534428265320147158 + | 'DYNAMIC_TYPE_COURSES_SEASON' // 课程, 示例: 717906712866062340 + | 'DYNAMIC_TYPE_COURSES_BATCH' // 课程批次 + | 'DYNAMIC_TYPE_AD' // 广告 + | 'DYNAMIC_TYPE_APPLET' // 小程序 + | 'DYNAMIC_TYPE_SUBSCRIPTION' // 订阅 + | 'DYNAMIC_TYPE_LIVE_RCMD' // 直播开播, 示例: 718371505648435205 + | 'DYNAMIC_TYPE_BANNER' // 横幅 + | 'DYNAMIC_TYPE_UGC_SEASON' // 合集更新, 示例: 718390979031203873 + | 'DYNAMIC_TYPE_SUBSCRIPTION_NEW'; // 新订阅 /** - * 主要类型 - * MAJOR_TYPE_ARCHIVE:视频 - * MAJOR_TYPE_PGC:番剧 - * MAJOR_TYPE_OPUS:图文混排 - * MAJOR_TYPE_COMMON:普通 - * MAJOR_TYPE_LIVE:直播 + * 动态主体类型 + * 更多类型请参考:https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/dynamic/dynamic_enum.md#%E5%8A%A8%E6%80%81%E4%B8%BB%E4%BD%93%E7%B1%BB%E5%9E%8B */ -export type MajorType = 'MAJOR_TYPE_ARCHIVE' | 'MAJOR_TYPE_PGC' | 'MAJOR_TYPE_OPUS' | 'MAJOR_TYPE_COMMON' | 'MAJOR_TYPE_LIVE'; +export type MajorType = + | 'MAJOR_TYPE_NONE' // 动态失效, 示例: 716510857084796964 + | 'MAJOR_TYPE_NONE' // 转发动态, 示例: 866756840240709701 + | 'MAJOR_TYPE_OPUS' // 图文动态, 示例: 870176712256651305 + | 'MAJOR_TYPE_ARCHIVE' // 视频, 示例: 716526237365829703 + | 'MAJOR_TYPE_PGC' // 剧集更新, 示例: 645981661420322824 + | 'MAJOR_TYPE_COURSES' // 课程 + | 'MAJOR_TYPE_DRAW' // 带图动态, 示例: 716358050743582725 + | 'MAJOR_TYPE_ARTICLE' // 文章 + | 'MAJOR_TYPE_MUSIC' // 音频更新 + | 'MAJOR_TYPE_COMMON' // 一般类型, 示例: 716481612656672789 + | 'MAJOR_TYPE_LIVE' // 直播间分享, 示例: 267505569812738175 + | 'MAJOR_TYPE_MEDIALIST' // 收藏夹 + | 'MAJOR_TYPE_APPLET' // 小程序 + | 'MAJOR_TYPE_SUBSCRIPTION' // 订阅 + | 'MAJOR_TYPE_LIVE_RCMD' // 直播状态 + | 'MAJOR_TYPE_UGC_SEASON' // 合计更新, 示例: 716509100448415814 + | 'MAJOR_TYPE_SUBSCRIPTION_NEW'; // 新订阅 diff --git a/lib/routes/bilibili/dynamic.ts b/lib/routes/bilibili/dynamic.ts index b514065ec..0df01415f 100644 --- a/lib/routes/bilibili/dynamic.ts +++ b/lib/routes/bilibili/dynamic.ts @@ -56,11 +56,49 @@ export const route: Route = { :::`, }; -const getTitle = (data: Modules) => data.module_dynamic.major?.opus?.title || data.module_dynamic.major?.archive?.title || data.module_dynamic.major?.pgc?.title; -const getDes = (data: Modules) => data.module_dynamic.desc?.text || data.module_dynamic.major?.opus?.summary?.text; +const getTitle = (data: Modules) => { + const major = data.module_dynamic?.major; + if (!major) { + return ''; + } + if (major.type === 'MAJOR_TYPE_NONE') { + return major.none?.tips; + } + if (major.type === 'MAJOR_TYPE_COURSES') { + return `${major.courses?.title} - ${major.courses?.sub_title}`; + } + const type = major.type.replace('MAJOR_TYPE_', '').toLowerCase(); + return major[type]?.title; +}; +const getDes = (data: Modules) => { + let desc = ''; + if (data.module_dynamic?.desc?.text) { + desc += data.module_dynamic.desc.text; + } + const major = data.module_dynamic?.major; + // 普通转发 + if (!major) { + return desc; + } + // 普通分享 + if (major?.common?.desc) { + desc += desc ? `
//转发自: ${major.common.desc}` : major.common.desc; + return desc; + } + // 直播动态 + if (major?.live) { + return `${major.live?.desc_first}
${major.live?.desc_second}`; + } + // 图文动态 + if (major?.opus) { + return major?.opus?.summary?.text; + } + const type = major.type.replace('MAJOR_TYPE_', '').toLowerCase(); + return major[type]?.desc; +}; -const getOriginTitle = (data?: Modules) => data?.module_dynamic?.major?.opus?.title || data?.module_dynamic?.major?.archive?.title || data?.module_dynamic.major?.pgc?.title; -const getOriginDes = (data?: Modules) => data?.module_dynamic.desc?.text || data?.module_dynamic?.major?.opus?.summary?.text || data?.module_dynamic?.major?.archive?.desc; +const getOriginTitle = (data?: Modules) => data && getTitle(data); +const getOriginDes = (data?: Modules) => data && getDes(data); const getOriginName = (data?: Modules) => data?.module_author?.name; const getIframe = (data?: Modules, disableEmbed: boolean = false) => { if (disableEmbed) { @@ -76,26 +114,25 @@ const getIframe = (data?: Modules, disableEmbed: boolean = false) => { const getImgs = (data: Modules) => { const imgUrls: string[] = []; - const dynamic = data.module_dynamic; - // 动态图片/专栏封面 - if (dynamic.major?.opus?.pics) { - imgUrls.push(...dynamic.major.opus.pics.map((e) => e.url)); + const major = data.module_dynamic?.major; + if (!major) { + return ''; } - // 视频封面 - if (dynamic.major?.archive?.cover) { - imgUrls.push(dynamic.major.archive.cover); + // 动态图片 + if (major.opus?.pics?.length) { + imgUrls.push(...major.opus.pics.map((e) => e.url)); } - // 番剧封面 - if (dynamic.major?.pgc?.cover) { - imgUrls.push(dynamic.major.pgc.cover); + // 专栏封面 + if (major.article?.covers?.length) { + imgUrls.push(...major.article.covers); } - // 直播间封面 - if (dynamic.major?.live?.cover) { - imgUrls.push(dynamic.major.live.cover); + // 相簿 + if (major.draw?.items?.length) { + imgUrls.push(...major.draw.items.map((e) => e.src)); } - // 专题页封面 - if (dynamic.major?.common?.cover) { - imgUrls.push(dynamic.major.common.cover); + const type = major.type.replace('MAJOR_TYPE_', '').toLowerCase(); + if (major[type]?.cover) { + imgUrls.push(major[type].cover); } return imgUrls.map((url) => ``); }; @@ -107,35 +144,55 @@ const getUrl = (item?: Item2, useAvid = false) => { } let url = ''; let text = ''; - switch (data.module_dynamic.major?.type) { + const major = data.module_dynamic?.major; + if (!major) { + return null; + } + switch (major?.type) { + case 'MAJOR_TYPE_UGC_SEASON': + url = major?.ugc_season?.jump_url || ''; + text = `
合集地址:${url}`; + break; + case 'MAJOR_TYPE_ARTICLE': + url = `https://www.bilibili.com/read/cv${major?.article?.id}`; + text = `
专栏地址:${url}`; + break; case 'MAJOR_TYPE_ARCHIVE': { - const archive = data?.module_dynamic?.major?.archive; + const archive = major?.archive; const id = useAvid ? `av${archive?.aid}` : archive?.bvid; url = `https://www.bilibili.com/video/${id}`; text = `
视频地址:${url}`; break; } - case 'MAJOR_TYPE_PGC': { - const pgc = data?.module_dynamic?.major?.pgc; - url = `https://www.bilibili.com/bangumi/play/ep${pgc?.epid}&season_id=${pgc?.season_id}`; - text = `
番剧地址:${url}`; + case 'MAJOR_TYPE_COMMON': + url = major?.common?.jump_url || ''; + text = `
地址:${url}`; break; - } case 'MAJOR_TYPE_OPUS': if (item?.type === 'DYNAMIC_TYPE_ARTICLE') { - url = `https:${data?.module_dynamic?.major?.opus?.jump_url}`; + url = `https:${major?.opus?.jump_url}`; text = `
专栏地址:${url}`; } else if (item?.type === 'DYNAMIC_TYPE_DRAW') { - url = data?.module_dynamic?.major?.opus?.jump_url || ''; + url = `https:${major?.opus?.jump_url}`; text = `
图文地址:${url}`; } break; - case 'MAJOR_TYPE_COMMON': - url = data?.module_dynamic?.major?.common?.jump_url || ''; - text = `
地址:${url}`; + case 'MAJOR_TYPE_PGC': { + const pgc = major?.pgc; + url = `https://www.bilibili.com/bangumi/play/ep${pgc?.epid}&season_id=${pgc?.season_id}`; + text = `
剧集地址:${url}`; + break; + } + case 'MAJOR_TYPE_COURSES': + url = `https://www.bilibili.com/cheese/play/ss${major?.courses?.id}`; + text = `
课程地址:${url}`; + break; + case 'MAJOR_TYPE_MUSIC': + url = `https://www.bilibili.com/audio/au${major?.music?.id}`; + text = `
音频地址:${url}`; break; case 'MAJOR_TYPE_LIVE': - url = `https://live.bilibili.com/${data?.module_dynamic?.major?.live?.id}`; + url = `https://live.bilibili.com/${major?.live?.id}`; text = `
直播间地址:${url}`; break; default: @@ -175,16 +232,16 @@ async function handler(ctx) { if (body?.code === -352) { throw new Error('风控校验失败!请手动配置 Cookie 后重试'); } - const cards = (body as BilibiliWebDynamicResponse)?.data?.items; + const items = (body as BilibiliWebDynamicResponse)?.data?.items; const usernameAndFace = await cacheIn.getUsernameAndFaceFromUID(uid); - const author = usernameAndFace[0] ?? cards[0]?.modules?.module_author?.name; - const face = usernameAndFace[1] ?? cards[0]?.modules?.module_author?.face; + const author = usernameAndFace[0] ?? items[0]?.modules?.module_author?.name; + const face = usernameAndFace[1] ?? items[0]?.modules?.module_author?.face; cache.set(`bili-username-from-uid-${uid}`, author); cache.set(`bili-userface-from-uid-${uid}`, face); - const items = await Promise.all( - cards.map(async (item) => { + const rssItems = await Promise.all( + items.map(async (item) => { // const parsed = JSONbig.parse(item.card); const data = item.modules; @@ -213,15 +270,15 @@ async function handler(ctx) { } // emoji - let data_content = getDes(data) || ''; + let description = getDes(data) || ''; // 换行处理 - data_content = data_content.replaceAll('\r\n', '
').replaceAll('\n', '
'); + description = description.replaceAll('\r\n', '
').replaceAll('\n', '
'); if (data.module_dynamic?.desc?.rich_text_nodes?.length && showEmoji) { const nodes = data.module_dynamic?.desc?.rich_text_nodes; for (const node of nodes) { if (node?.emoji) { const emoji = node.emoji; - data_content = data_content.replaceAll( + description = description.replaceAll( emoji.text, `${emoji.text}` ); @@ -231,9 +288,9 @@ async function handler(ctx) { if (item.type === 'DYNAMIC_TYPE_ARTICLE' && displayArticle) { // 抓取专栏全文 - const cvid = data.module_dynamic.major?.opus?.jump_url?.match?.(/cv(\d+)/)?.[0]; + const cvid = data.module_dynamic?.major?.opus?.jump_url?.match?.(/cv(\d+)/)?.[0]; if (cvid) { - data_content = (await cacheIn.getArticleDataFromCvid(cvid, uid)).description || ''; + description = (await cacheIn.getArticleDataFromCvid(cvid, uid)).description || ''; } } @@ -266,7 +323,6 @@ async function handler(ctx) { if (originDes) { originDescription += `
${originDes}`; } - const description = data_content; const imgHTMLSource = imgHTML ? `
${imgHTML}` : ''; return { title: getTitle(data) || '', @@ -283,6 +339,6 @@ async function handler(ctx) { link: `https://space.bilibili.com/${uid}/dynamic`, description: `${author} 的 bilibili 动态`, image: face, - item: items, + item: rssItems, }; }