From b22d4c0dce3293fddb100ac211e9a7595b3b343c Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Wed, 2 Feb 2022 20:01:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E6=B6=88=E8=B2=BB?= =?UTF-8?q?=E8=80=85=E5=A7=94=E5=93=A1=E6=9C=83=20(#8743)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 20 +++++++++++++ lib/v2/consumer/index.js | 55 +++++++++++++++++++++++++++++++++++ lib/v2/consumer/maintainer.js | 3 ++ lib/v2/consumer/radar.js | 13 +++++++++ lib/v2/consumer/router.js | 3 ++ 5 files changed, 94 insertions(+) create mode 100644 lib/v2/consumer/index.js create mode 100644 lib/v2/consumer/maintainer.js create mode 100644 lib/v2/consumer/radar.js create mode 100644 lib/v2/consumer/router.js diff --git a/docs/new-media.md b/docs/new-media.md index dae43a92b..bf23eeda9 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -3295,6 +3295,26 @@ column 为 third 时可选的 category: +## 消费者委员会 + +### 文章 + + + +分类 + +| 测试及调查 | 生活资讯 | 投诉实录 | 议题评论 | +| ---------- | -------- | --------- | -------- | +| test | life | complaint | topic | + +语言 + +| 简体中文 | 繁体中文 | +| -------- | -------- | +| sc | tc | + + + ## 辛華社 ### 首页 diff --git a/lib/v2/consumer/index.js b/lib/v2/consumer/index.js new file mode 100644 index 000000000..f32ea8fcc --- /dev/null +++ b/lib/v2/consumer/index.js @@ -0,0 +1,55 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'test'; + const language = ctx.params.language ?? 'tc'; + const keyword = ctx.params.keyword ?? ''; + + const rootUrl = 'https://www.consumer.org.hk'; + const currentUrl = `${rootUrl}/${language}/free-article/free-article-${category}?category=free-article-${category}&q=${keyword}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('.half-img-blk__title, .img-plate-blk__title') + .find('a') + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + pubDate: parseDate(item.parent().prev().find('li').first().text(), 'YYYY.MM'), + }; + }); + + 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); + + item.description = content('.ckec').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/consumer/maintainer.js b/lib/v2/consumer/maintainer.js new file mode 100644 index 000000000..8b9d4047f --- /dev/null +++ b/lib/v2/consumer/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:category?/:language?/:keyword?': ['nczitzk'], +}; diff --git a/lib/v2/consumer/radar.js b/lib/v2/consumer/radar.js new file mode 100644 index 000000000..4cfe58f12 --- /dev/null +++ b/lib/v2/consumer/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'consumer.org.hk': { + _name: '消费者委员会', + '.': [ + { + title: '文章', + docs: 'https://docs.rsshub.app/new-media.html#xiao-fei-zhe-wei-yuan-hui-wen-zhang', + source: ['/'], + target: '/consumer/:category?/:language?/:keyword?', + }, + ], + }, +}; diff --git a/lib/v2/consumer/router.js b/lib/v2/consumer/router.js new file mode 100644 index 000000000..fe6b14f1b --- /dev/null +++ b/lib/v2/consumer/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:category?/:language?/:keyword?', require('./index')); +};