From f037bf0492436c734c82c92aecb8913ef2b1016c Mon Sep 17 00:00:00 2001 From: Rongrong Date: Mon, 13 May 2024 12:46:38 +0800 Subject: [PATCH] fix(route/mihoyo/bbs/official): non-unique guid (#15567) Signed-off-by: Rongrong --- lib/routes/mihoyo/bbs/official.ts | 99 +++++++++++++++++++------------ 1 file changed, 60 insertions(+), 39 deletions(-) diff --git a/lib/routes/mihoyo/bbs/official.ts b/lib/routes/mihoyo/bbs/official.ts index 3e76a2fd2..2fd289ebf 100644 --- a/lib/routes/mihoyo/bbs/official.ts +++ b/lib/routes/mihoyo/bbs/official.ts @@ -5,6 +5,7 @@ import { art } from '@/utils/render'; import path from 'node:path'; import { parseDate } from '@/utils/parse-date'; import { getCurrentPath } from '@/utils/helpers'; +import logger from '@/utils/logger'; const __dirname = getCurrentPath(import.meta.url); // 游戏id @@ -43,6 +44,13 @@ const OFFICIAL_PAGE_MAP = { 8: '58', }; +class MiHoYoOfficialError extends Error { + constructor(message) { + super(message); + this.name = 'MiHoYoOfficialError'; + } +} + const getNewsList = async ({ gids, type, page_size, last_id }) => { const query = new URLSearchParams({ gids, @@ -59,45 +67,58 @@ const getNewsList = async ({ gids, type, page_size, last_id }) => { return list; }; -const getPostContent = (list) => +const getPostContent = async (row, default_gid = '2') => { + const post = row.post; + const post_id = post.post_id; + const query = new URLSearchParams({ + post_id, + }).toString(); + const url = `https://bbs-api.miyoushe.com/post/wapi/getPostFull?${query}`; + return await cache.tryGet(url, async () => { + const res = await got(url); + const fullRow = res?.data?.data?.post; + if (!fullRow) { + // throw an error to prevent an empty item from being cached and returned + throw new MiHoYoOfficialError(`mihoyo/bbs/official: getPostContent failed: ${url} - ${JSON.stringify(res)}`); + } + // default_gid should be useless since the above error-throwing line, but just in case + const gid = fullRow?.post?.game_id || default_gid; + const author = fullRow?.user?.nickname || ''; + const content = fullRow?.post?.content || ''; + const tags = fullRow?.topics?.map((item) => item.name) || []; + const description = art(path.join(__dirname, '../templates/official.art'), { + hasCover: post.has_cover, + coverList: row.cover_list, + content, + }); + return { + // 文章标题 + title: post.subject, + // 文章链接 + link: `https://www.miyoushe.com/${GAME_SHORT_MAP[gid]}/article/${post_id}`, + // 文章正文 + description, + // 文章发布日期 + pubDate: parseDate(post.created_at * 1000), + // 文章标签 + category: tags, + author, + }; + }); +}; + +const getPostContents = (list, default_gid = '2') => Promise.all( - list.map(async (row) => { - const post = row.post; - const post_id = post.post_id; - const query = new URLSearchParams({ - post_id, - }).toString(); - const url = `https://bbs-api.miyoushe.com/post/wapi/getPostFull?${query}`; - return await cache.tryGet(url, async () => { - const res = await got({ - method: 'get', - url, - }); - const gid = res?.data?.data?.post?.post?.game_id || '2'; - const author = res?.data?.data?.post?.user?.nickname || ''; - const content = res?.data?.data?.post?.post?.content || ''; - const tags = res?.data?.data?.post?.topics?.map((item) => item.name) || []; - const description = art(path.join(__dirname, '../templates/official.art'), { - hasCover: post.has_cover, - coverList: row.cover_list, - content, - }); - return { - // 文章标题 - title: post.subject, - // 文章链接 - link: `https://www.miyoushe.com/${GAME_SHORT_MAP[gid]}/article/${post_id}`, - // 文章正文 - description, - // 文章发布日期 - pubDate: parseDate(post.created_at * 1000), - // 文章标签 - category: tags, - author, - }; - }); - }) - ); + list.map((item) => + getPostContent(item, default_gid).catch((error) => { + if (error instanceof MiHoYoOfficialError) { + logger.error(error.message); + return null; // skip it now and pray that it will be available next time + } + throw error; + }) + ) + ).then((items) => items.filter(Boolean)); export const route: Route = { path: '/bbs/official/:gids/:type?/:page_size?/:last_id?', @@ -132,7 +153,7 @@ async function handler(ctx) { const { gids, type = '2', page_size = '20', last_id = '' } = ctx.req.param(); const list = await getNewsList({ gids, type, page_size, last_id }); - const items = await getPostContent(list); + const items = await getPostContents(list, gids); const title = `米游社 - ${GITS_MAP[gids] || ''} - ${TYPE_MAP[type] || ''}`; const url = `https://www.miyoushe.com/${GAME_SHORT_MAP[gids]}/home/${OFFICIAL_PAGE_MAP[gids]}?type=${type}`; const data = {