From 22551e963fb7680f635bad3d5ad439ce46fc1333 Mon Sep 17 00:00:00 2001 From: Maooyer Date: Tue, 16 Apr 2024 23:09:42 +0800 Subject: [PATCH] feat(route/mihoyo): enhance miyoushe official post (#15257) * feat(route/mihoyo): enhance miyoushe offical post 1. get full text post rather than abstracts. 2. add post hashtags as rss.item.category. 3. Set appropriate link. * fix(route/mihoyo): fix __dirname is not defined error --- lib/routes/mihoyo/bbs/official.ts | 103 +++++++++++++++++++---- lib/routes/mihoyo/templates/official.art | 7 ++ 2 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 lib/routes/mihoyo/templates/official.art diff --git a/lib/routes/mihoyo/bbs/official.ts b/lib/routes/mihoyo/bbs/official.ts index 22c31c552346..3e76a2fd2eec 100644 --- a/lib/routes/mihoyo/bbs/official.ts +++ b/lib/routes/mihoyo/bbs/official.ts @@ -1,6 +1,12 @@ import { Route } from '@/types'; +import cache from '@/utils/cache'; import got from '@/utils/got'; -import { post2item } from './utils'; +import { art } from '@/utils/render'; +import path from 'node:path'; +import { parseDate } from '@/utils/parse-date'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); + // 游戏id const GITS_MAP = { 1: '崩坏三', @@ -18,6 +24,81 @@ const TYPE_MAP = { 3: '资讯', }; +// 游戏缩写 +const GAME_SHORT_MAP = { + 1: 'bh3', + 2: 'ys', + 3: 'bh2', + 4: 'wd', + 6: 'sr', + 8: 'zzz', +}; +// 游戏官方页所属分区 +const OFFICIAL_PAGE_MAP = { + 1: '6', + 2: '28', + 3: '31', + 4: '33', + 6: '53', + 8: '58', +}; + +const getNewsList = async ({ gids, type, page_size, last_id }) => { + const query = new URLSearchParams({ + gids, + type, + page_size, + last_id, + }).toString(); + const url = `https://bbs-api.miyoushe.com/post/wapi/getNewsList?${query}`; + const response = await got({ + method: 'get', + url, + }); + const list = response?.data?.data?.list; + return list; +}; + +const getPostContent = (list) => + 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, + }; + }); + }) + ); + export const route: Route = { path: '/bbs/official/:gids/:type?/:page_size?/:last_id?', categories: ['game'], @@ -49,23 +130,11 @@ export const route: Route = { async function handler(ctx) { const { gids, type = '2', page_size = '20', last_id = '' } = ctx.req.param(); - const query = new URLSearchParams({ - gids, - type, - page_size, - last_id, - }).toString(); - const url = `https://bbs-api.miyoushe.com/post/wapi/getNewsList?${query}`; - const response = await got({ - method: 'get', - url, - }); - const list = response?.data?.data?.list; - if (!list) { - throw new Error('未获取到数据!'); - } + + const list = await getNewsList({ gids, type, page_size, last_id }); + const items = await getPostContent(list); const title = `米游社 - ${GITS_MAP[gids] || ''} - ${TYPE_MAP[type] || ''}`; - const items = list.map((e) => post2item(e)); + const url = `https://www.miyoushe.com/${GAME_SHORT_MAP[gids]}/home/${OFFICIAL_PAGE_MAP[gids]}?type=${type}`; const data = { title, link: url, diff --git a/lib/routes/mihoyo/templates/official.art b/lib/routes/mihoyo/templates/official.art new file mode 100644 index 000000000000..8744cb1e6fa1 --- /dev/null +++ b/lib/routes/mihoyo/templates/official.art @@ -0,0 +1,7 @@ +{{ if hasCover }} + {{ each coverList c }} +
+ {{ /each }} +{{ /if }} + +{{@ content }}