From d9332c2bae0b8351c3f1dea082b2b5ad6fdf9675 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Wed, 19 Jul 2023 23:35:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E7=BA=AA=E5=A6=96=20(#128?= =?UTF-8?q?24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 纪妖 * fix: use wordpress restapi * update lib/v2/cbaigui/index.js --------- --- docs/new-media.md | 18 +++++ lib/v2/cbaigui/index.js | 100 ++++++++++++++++++++++++++++ lib/v2/cbaigui/maintainer.js | 3 + lib/v2/cbaigui/radar.js | 18 +++++ lib/v2/cbaigui/router.js | 3 + lib/v2/cbaigui/templates/figure.art | 3 + lib/v2/cbaigui/utils.js | 18 +++++ 7 files changed, 163 insertions(+) create mode 100644 lib/v2/cbaigui/index.js create mode 100644 lib/v2/cbaigui/maintainer.js create mode 100644 lib/v2/cbaigui/radar.js create mode 100644 lib/v2/cbaigui/router.js create mode 100644 lib/v2/cbaigui/templates/figure.art create mode 100644 lib/v2/cbaigui/utils.js diff --git a/docs/new-media.md b/docs/new-media.md index 38e1e97be..538dfaaec 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2754,6 +2754,24 @@ others = 热点新闻 + 滚动新闻 +## 纪妖 + +### 通用 + + + +::: tip 提示 + +若订阅 [标签:妖](https://www.cbaigui.com/post-tag/妖),网址为 。截取 `https://www.cbaigui.com` 到末尾的部分 `/post-tag/妖` 作为参数,此时路由为 [`/cbaigui/post-tag/妖`](https://rsshub.app/cbaigui/post-tag/妖)。 + +若订阅 [分类:埃及](https://www.cbaigui.com/post-category/世界/非洲/埃及),网址为 。截取 `https://www.cbaigui.com` 到末尾的部分 `/post-category/世界/非洲/埃及` 作为参数,此时路由为 [`/cbaigui/post-category/世界/非洲/埃及`](https://rsshub.app/cbaigui/post-category/世界/非洲/埃及)。 + +若订阅 [词条:白泽图](https://www.cbaigui.com/post-category/词条/白泽图),网址为 。截取 `https://www.cbaigui.com` 到末尾的部分 `/post-category/词条/白泽图` 作为参数,此时路由为 [`/cbaigui/post-category/词条/白泽图`](https://rsshub.app/cbaigui/post-category/词条/白泽图)。 + +::: + + + ## 加美财经 diff --git a/lib/v2/cbaigui/index.js b/lib/v2/cbaigui/index.js new file mode 100644 index 000000000..19552976a --- /dev/null +++ b/lib/v2/cbaigui/index.js @@ -0,0 +1,100 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +const { rootUrl, apiSlug, GetFilterId } = require('./utils'); + +module.exports = async (ctx) => { + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 50; + + let filterName; + + const currentUrl = new URL(ctx.path.replace(/^\/cbaigui/, ''), rootUrl).href; + let apiUrl = new URL(`${apiSlug}/posts?_embed=true&per_page=${limit}`, rootUrl).href; + + const filterMatches = ctx.path.match(/^\/post-(tag|category)\/(.*)$/); + + if (filterMatches) { + filterName = decodeURI(filterMatches[2].split('/').pop()); + const filterType = filterMatches[1] === 'tag' ? 'tags' : 'categories'; + const filterId = await GetFilterId(filterType, filterName); + + if (filterId) { + apiUrl = new URL(`${apiSlug}/posts?_embed=true&per_page=${limit}&${filterType}=${filterId}`, rootUrl).href; + } + } + + const { data: response } = await got(apiUrl); + + const items = response.slice(0, limit).map((item) => { + const terminologies = item._embedded['wp:term']; + + const content = cheerio.load(item.content?.rendered ?? item.content); + + // To handle lazy-loaded images from external sites. + + content('figure').each(function () { + const image = content(this).find('img'); + const src = image.prop('data-actualsrc') ?? image.prop('data-original'); + const width = image.prop('data-rawwidth'); + const height = image.prop('data-rawheight'); + + content(this).replaceWith( + art(path.join(__dirname, 'templates/figure.art'), { + src, + width, + height, + }) + ); + }); + + // To remove watermarks on images. + + content('p img').each(function () { + const image = content(this); + const src = image.prop('src').split('!')[0]; + const width = image.prop('width'); + const height = image.prop('height'); + + content(this).replaceWith( + art(path.join(__dirname, 'templates/figure.art'), { + src, + width, + height, + }) + ); + }); + + return { + title: item.title?.rendered ?? item.title, + link: item.link, + description: content.html(), + author: item._embedded.author.map((a) => a.name).join('/'), + category: [].concat(...terminologies[0], ...terminologies[1]).map((c) => c.name), + guid: item.guid?.rendered ?? item.guid, + pubDate: parseDate(item.date_gmt), + updated: parseDate(item.modified_gmt), + }; + }); + + const { data: currentResponse } = await got(currentUrl); + + const $ = cheerio.load(currentResponse); + + const icon = $('link[rel="apple-touch-icon"]').first().prop('href'); + + ctx.state.data = { + item: items, + title: `纪妖${filterName ? ` - ${filterName}` : ''}`, + link: currentUrl, + description: $('meta[name="description"]').prop('content'), + language: 'zh-cn', + image: $('meta[name="msapplication-TileImage"]').prop('content'), + icon, + logo: icon, + subtitle: $('p.site-description').text(), + author: $('p.site-title').text(), + }; +}; diff --git a/lib/v2/cbaigui/maintainer.js b/lib/v2/cbaigui/maintainer.js new file mode 100644 index 000000000..57f872453 --- /dev/null +++ b/lib/v2/cbaigui/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:path+': ['nczitzk'], +}; diff --git a/lib/v2/cbaigui/radar.js b/lib/v2/cbaigui/radar.js new file mode 100644 index 000000000..a1c385e68 --- /dev/null +++ b/lib/v2/cbaigui/radar.js @@ -0,0 +1,18 @@ +module.exports = { + 'cbaigui.com': { + _name: '纪妖', + '.': [ + { + title: '通用', + docs: 'https://docs.rsshub.app/new-media.html#ji-yao-tong-yong', + source: ['/'], + target: (params, url) => { + url = new URL(url); + const path = url.href.match(/\.com(.*?)/)[1]; + + return `/cbaigui${path}`; + }, + }, + ], + }, +}; diff --git a/lib/v2/cbaigui/router.js b/lib/v2/cbaigui/router.js new file mode 100644 index 000000000..4443c20f6 --- /dev/null +++ b/lib/v2/cbaigui/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get(/([\w-/]+)?/, require('./')); +}; diff --git a/lib/v2/cbaigui/templates/figure.art b/lib/v2/cbaigui/templates/figure.art new file mode 100644 index 000000000..6571027e2 --- /dev/null +++ b/lib/v2/cbaigui/templates/figure.art @@ -0,0 +1,3 @@ +
+ +
\ No newline at end of file diff --git a/lib/v2/cbaigui/utils.js b/lib/v2/cbaigui/utils.js new file mode 100644 index 000000000..b12e1d413 --- /dev/null +++ b/lib/v2/cbaigui/utils.js @@ -0,0 +1,18 @@ +const got = require('@/utils/got'); + +const rootUrl = 'https://cbaigui.com'; +const apiSlug = 'wp-json/wp/v2'; + +const GetFilterId = async (type, name) => { + const filterApiUrl = new URL(`${apiSlug}/${type}?search=${name}`, rootUrl).href; + + const { data: filterResponse } = await got(filterApiUrl); + + return filterResponse.filter((f) => f.name === name).pop()?.id ?? undefined; +}; + +module.exports = { + rootUrl, + apiSlug, + GetFilterId, +};