From dde57522ae1f8fae855ec0270f0bf81405142596 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Thu, 7 Jul 2022 18:57:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E6=98=9F=E6=B4=B2?= =?UTF-8?q?=E7=BD=91=20(#10167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/traditional-media.md | 27 ++++++++++++ lib/v2/sinchew/index.js | 68 +++++++++++++++++++++++++++++ lib/v2/sinchew/maintainer.js | 5 +++ lib/v2/sinchew/radar.js | 25 +++++++++++ lib/v2/sinchew/router.js | 4 ++ lib/v2/sinchew/templates/images.art | 4 ++ 6 files changed, 133 insertions(+) create mode 100644 lib/v2/sinchew/index.js create mode 100644 lib/v2/sinchew/maintainer.js create mode 100644 lib/v2/sinchew/radar.js create mode 100644 lib/v2/sinchew/router.js create mode 100644 lib/v2/sinchew/templates/images.art diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 3e0afb0e5..1b0666fad 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -1740,6 +1740,33 @@ category 对应的关键词有 +## 星洲网 + +### 首页 + + + +### 最新 + + + +### 分类 + + + +| 头条 | 国内 | 国际 | 言路 | 财经 | 地方 | 副刊 | 娱乐 | 体育 | 百格 | 星角攝 | 好运来 | +| -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --- | --- | + +::: tip 提示 + +若订阅单级分类 [头条](https://www.sinchew.com.my/category/头条),其 URL 为 [https://www.sinchew.com.my/category/ 头条](https://www.sinchew.com.my/category/头条),则路由为 [`/sinchew/category/头条`](https://rsshub.app/sinchew/category/头条)。 + +若订阅多级分类 [国际 > 天下事](https://www.sinchew.com.my/category/国际/天下事),其 URL 为 [https://www.sinchew.com.my/category/ 国际 / 天下事](https://www.sinchew.com.my/category/国际/天下事),则路由为 [`/sinchew/category/国际/天下事`](https://rsshub.app/sinchew/category/国际/天下事)。 + +::: + + + ## 央视新闻 ### 新闻联播 diff --git a/lib/v2/sinchew/index.js b/lib/v2/sinchew/index.js new file mode 100644 index 000000000..36934f4a0 --- /dev/null +++ b/lib/v2/sinchew/index.js @@ -0,0 +1,68 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const rootUrl = 'https://www.sinchew.com.my'; + const currentUrl = `${rootUrl}${ctx.path === '/' ? '' : ctx.path}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('.title .internalLink') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 20) + .toArray() + .map((item) => { + item = $(item); + + const link = item.attr('href'); + + return { + title: item.attr('data-title'), + link: /^http/.test(link) ? link : `${rootUrl}${link}`, + pubDate: timezone(parseDate(item.text()), +8), + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + content('.ads-frame, .read-more-msg').remove(); + + content('figure').each(function () { + content(this).replaceWith( + art(path.join(__dirname, 'templates/images.art'), { + image: content(this).find('img').attr('src'), + caption: content(this).find('figcaption').text(), + }) + ); + }); + + item.description = content('.article-page-content').html(); + item.pubDate = timezone(parseDate(content('meta[property="article:published_time"]').attr('content')), +8); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/sinchew/maintainer.js b/lib/v2/sinchew/maintainer.js new file mode 100644 index 000000000..ffd79b20f --- /dev/null +++ b/lib/v2/sinchew/maintainer.js @@ -0,0 +1,5 @@ +module.exports = { + '/category/:category?': ['nczitzk'], + '/latest': ['nczitzk'], + '/': ['nczitzk'], +}; diff --git a/lib/v2/sinchew/radar.js b/lib/v2/sinchew/radar.js new file mode 100644 index 000000000..e4e855bbf --- /dev/null +++ b/lib/v2/sinchew/radar.js @@ -0,0 +1,25 @@ +module.exports = { + 'sinchew.com.my': { + _name: '星洲网', + '.': [ + { + title: '首页', + docs: 'https://docs.rsshub.app/traditional-media.html#xing-zhou-wang-shou-ye', + source: ['/'], + target: '/sinchew', + }, + { + title: '最新', + docs: 'https://docs.rsshub.app/traditional-media.html#xing-zhou-wang-zui-xin', + source: ['/latest', '/'], + target: '/sinchew/latest', + }, + { + title: '分类', + docs: 'https://docs.rsshub.app/traditional-media.html#xing-zhou-wang-fen-lei', + source: ['/category/:category', '/'], + target: (params, url) => `/sinchew/category/${new URL(url).toString().match(/\/category\/(.*)$/)[1]}`, + }, + ], + }, +}; diff --git a/lib/v2/sinchew/router.js b/lib/v2/sinchew/router.js new file mode 100644 index 000000000..4cf382f6c --- /dev/null +++ b/lib/v2/sinchew/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get(/([\w/-]+)?/, require('./index')); + router.get('', require('./index')); +}; diff --git a/lib/v2/sinchew/templates/images.art b/lib/v2/sinchew/templates/images.art new file mode 100644 index 000000000..4e53d4340 --- /dev/null +++ b/lib/v2/sinchew/templates/images.art @@ -0,0 +1,4 @@ +
+ +
{{ caption }}
+
\ No newline at end of file