From cfd8de9fc3c4dc0fbb1726afe06e8ebffd029b92 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 22 Feb 2025 00:11:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(gcores):=20=E6=9C=BA=E6=A0=B8=E7=BD=91?= =?UTF-8?q?=E4=B8=93=E9=A2=98=20(#18367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(gcores): 机核网专题 * fix typo Co-authored-by: Tony --------- --- lib/routes/gcores/collection.ts | 161 ------------------------------- lib/routes/gcores/collections.ts | 129 +++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 161 deletions(-) delete mode 100644 lib/routes/gcores/collection.ts create mode 100644 lib/routes/gcores/collections.ts diff --git a/lib/routes/gcores/collection.ts b/lib/routes/gcores/collection.ts deleted file mode 100644 index f6455e249..000000000 --- a/lib/routes/gcores/collection.ts +++ /dev/null @@ -1,161 +0,0 @@ -import { Route } from '@/types'; -import cache from '@/utils/cache'; -/* refer to: ./tag.js (author: @StevenREC0) */ -import got from '@/utils/got'; - -export const route: Route = { - path: '/collections/:collection', - categories: ['new-media', 'popular'], - example: '/gcores/collections/64', - parameters: { collection: '专题id,可在专题页面的 URL 中找到,如 游戏开发设计心得分享 -- 64' }, - features: { - requireConfig: false, - requirePuppeteer: false, - antiCrawler: true, - supportBT: false, - supportPodcast: false, - supportScihub: false, - }, - radar: [ - { - source: ['gcores.com/collections/:collection'], - }, - ], - name: '专题文章', - maintainers: ['kudryavka1013'], - handler, -}; - -async function handler(ctx) { - // get params - const collection = ctx.req.param('collection'); - - // feed info - const baseUrl = `https://www.gcores.com/gapi/v1/collections/${collection}`; - const baseRes = await got(baseUrl); - const feedTitle = baseRes.data.data.attributes.title; - const feedUrl = `https://www.gcores.com/collections/${collection}`; - - // resolve articles - const dataUrl = `${baseUrl}/articles?sort=-published-at`; - const res = await got(dataUrl); - const list = res.data.data; - - function wrapInlineTag(strArr, startIdx, endIdx, preString, sufString) { - if (startIdx === endIdx - 1) { - strArr[startIdx] = `${preString}${strArr[startIdx]}${sufString}`; - } else { - strArr[startIdx] = `${preString}${strArr[startIdx]}`; - strArr[endIdx - 1] = `${strArr[endIdx - 1]}${sufString}`; - } - } - - const items = list.map((item) => { - const articleUrl = `https://www.gcores.com/${item.type}/${item.id}`; - return cache.tryGet(articleUrl, () => { - let cover = ''; - if (item.attributes.cover) { - cover = ``; - } else if (item.attributes.thumb) { - cover = ``; - } - - const contentBlocks = JSON.parse(item.attributes.content); - const { blocks, entityMap } = contentBlocks; - - function convertBlockToContent(block) { - const { type, text, entityRanges, inlineStyleRanges } = block; - let formattedText = text; - if (entityRanges.length || inlineStyleRanges.length) { - // split string and insert HTML tag - const strArr = [...text]; - let isMedia = false; - if (entityRanges.length) { - for (const entityRange of entityRanges) { - const { key, offset, length } = entityRange; - const startIdx = offset, - endIdx = offset + length; - const { data, type } = entityMap[key]; - switch (type) { - case 'LINK': - wrapInlineTag(strArr, startIdx, endIdx, ``, ``); - break; - case 'IMAGE': - formattedText = `
${data.caption ? `
${data.caption}
` : ''}
`; - isMedia = true; - break; - case 'EMBED': - formattedText = `
${data.content}${data.caption ? `
${data.caption}
` : ''}
`; - isMedia = true; - break; - case 'GALLERY': - formattedText = `
${data.images.map((image) => `
`).join('')}
`; - isMedia = true; - break; - default: - break; - } - } - } - if (inlineStyleRanges.length) { - for (const inlineStyleRange of inlineStyleRanges) { - const { style, offset, length } = inlineStyleRange; - const startIdx = offset, - endIdx = offset + length; - switch (style) { - case 'BOLD': - wrapInlineTag(strArr, startIdx, endIdx, ``, ``); - break; - case 'UNDERLINE': - wrapInlineTag(strArr, startIdx, endIdx, ``, ``); - break; - case 'ITALIC': - wrapInlineTag(strArr, startIdx, endIdx, ``, ``); - break; - default: - break; - } - } - } - formattedText = isMedia ? formattedText : strArr.join(''); - } - - /* 未兼容列表,仅展示为段落,有兴趣可以补全:unordered-list-item、ordered-list-item */ - switch (type) { - case 'unstyled': - return `

${formattedText}

`; - case 'header-one': - return `

${formattedText}

`; - case 'header-two': - return `

${formattedText}

`; - case 'header-three': - return `

${formattedText}

`; - case 'header-four': - return `

${formattedText}

`; - case 'header-five': - return `
${formattedText}
`; - case 'header-six': - return `
${formattedText}
`; - case 'atomic': - return formattedText; - default: - return `

${formattedText}

`; - } - } - const content = blocks.map((block) => convertBlockToContent(block)); - - return { - title: item.attributes.title, - description: cover + content.join(''), - link: articleUrl, - }; - }); - }); - - // return data - return { - title: feedTitle, - link: feedUrl, - item: items, - }; -} diff --git a/lib/routes/gcores/collections.ts b/lib/routes/gcores/collections.ts new file mode 100644 index 000000000..ba6237a8b --- /dev/null +++ b/lib/routes/gcores/collections.ts @@ -0,0 +1,129 @@ +import { type Data, type Route, ViewType } from '@/types'; + +import { getCurrentPath } from '@/utils/helpers'; +import { type Context } from 'hono'; + +import { baseUrl, processItems } from './util'; + +export const __dirname = getCurrentPath(import.meta.url); + +let viewType: ViewType = ViewType.Articles; + +export const handler = async (ctx: Context): Promise => { + const { id, tab } = ctx.req.param(); + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10); + + const targetUrl: string = new URL(`collections/${id}${tab ? `?tab=${tab}` : ''}`, baseUrl).href; + const apiUrl: string = new URL(`gapi/v1/collections/${id}/${tab ?? 'originals'}`, baseUrl).href; + + const query = { + 'page[limit]': limit, + sort: '-published-at', + include: 'category,user,media', + 'filter[list-all]': 1, + 'filter[is-news]': tab === 'news' ? 1 : 0, + }; + + if (tab === 'radios') { + viewType = ViewType.Audios; + } else if (tab === 'videos') { + viewType = ViewType.Videos; + } + + return await processItems(limit, query, apiUrl, targetUrl); +}; + +export const route: Route = { + path: '/collections/:id/:tab?', + name: '专题', + url: 'www.gcores.com', + maintainers: ['kudryavka1013', 'nczitzk'], + handler, + example: '/gcores/collections/64/articles', + parameters: { + id: { + description: '专题 ID,可在对应专题页 URL 中找到', + }, + tab: { + description: '类型,默认为空,即全部,可在对应专题页 URL 中找到', + options: [ + { + label: '全部', + value: '', + }, + { + label: '播客', + value: 'radios', + }, + { + label: '文章', + value: 'articles', + }, + { + label: '资讯', + value: 'news', + }, + { + label: '视频', + value: 'videos', + }, + ], + }, + }, + description: `:::tip +若订阅 [文章 - 文章](https://www.gcores.com/collections/64?tab=articles),网址为 \`https://www.gcores.com/collections/64?tab=articles\`,请截取 \`https://www.gcores.com/collections/\` 到末尾的部分 \`64\` 作为 \`id\` 参数填入,截取 \`articles\` 作为 \`tab\` 参数填入,此时目标路由为 [\`/gcores/collections/64/articles\`](https://rsshub.app/gcores/collections/64/articles)。 +::: + +| 全部 | 播客 | 文章 | 资讯 | 视频 | +| ---- | ------ | -------- | ---- | ------ | +| | radios | articles | news | videos | +`, + categories: ['game'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.gcores.com/collections/:id'], + target: (params, url) => { + const urlObj: URL = new URL(url); + const id: string = params.id; + const tab: string | undefined = urlObj.searchParams.get('tab') ?? undefined; + + return `/gcores/collections/${id}/${tab ? `/${tab}` : ''}`; + }, + }, + { + title: '全部', + source: ['www.gcores.com/collections/:id'], + target: '/collections/:id', + }, + { + title: '播客', + source: ['www.gcores.com/collections/:id'], + target: '/collections/:id/radios', + }, + { + title: '文章', + source: ['www.gcores.com/collections/:id'], + target: '/collections/:id/articles', + }, + { + title: '资讯', + source: ['www.gcores.com/collections/:id'], + target: '/collections/:id/news', + }, + { + title: '视频', + source: ['www.gcores.com/collections/:id'], + target: '/collections/:id/videos', + }, + ], + view: viewType, +};