From b921c04bfb6a110a75170ebb410f1f05546c9f9f Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Tue, 18 Apr 2023 03:12:13 +0800 Subject: [PATCH] feat(route): add Aqara News (#12337) * feat(route): add Aqara News * docs: add news * fix typo --- docs/other.md | 6 +++++ lib/v2/aqara/maintainer.js | 3 +++ lib/v2/aqara/news.js | 53 ++++++++++++++++++++++++++++++++++++++ lib/v2/aqara/radar.js | 13 ++++++++++ lib/v2/aqara/router.js | 3 +++ 5 files changed, 78 insertions(+) create mode 100644 lib/v2/aqara/maintainer.js create mode 100644 lib/v2/aqara/news.js create mode 100644 lib/v2/aqara/radar.js create mode 100644 lib/v2/aqara/router.js diff --git a/docs/other.md b/docs/other.md index 76dfd540d2..9cdf7a12e2 100644 --- a/docs/other.md +++ b/docs/other.md @@ -76,6 +76,12 @@ pageClass: routes 见 [#app-store-mac-app-store](/program-update.html#app-store-mac-app-store) +## Aqara + +### News + + + ## AutoTrader ### 搜索结果 diff --git a/lib/v2/aqara/maintainer.js b/lib/v2/aqara/maintainer.js new file mode 100644 index 0000000000..169cca9bcb --- /dev/null +++ b/lib/v2/aqara/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news': ['nczitzk'], +}; diff --git a/lib/v2/aqara/news.js b/lib/v2/aqara/news.js new file mode 100644 index 0000000000..f9a77fde6f --- /dev/null +++ b/lib/v2/aqara/news.js @@ -0,0 +1,53 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 35; + + const rootUrl = 'https://www.aqara.com'; + const currentUrl = `${rootUrl}/news`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + let items = response.data + .match(/parm.contentHerf = '(\d+)'/g) + .slice(0, limit) + .map((item) => ({ + link: `${rootUrl}/${item.match(/'(\d+)'/)[1]}`, + })); + + 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.title = content('h4.fnt_56').last().text(); + item.description = content('.news_body').html(); + item.pubDate = parseDate( + content('.news_date') + .first() + .text() + .replace(/( {2}年 {2}| {2}月 {2})/g, '-') + .replace(/ {2}日/, '') + ); + + return item; + }) + ) + ); + + ctx.state.data = { + title: '新闻动态 - Aqara 全屋智能', + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/aqara/radar.js b/lib/v2/aqara/radar.js new file mode 100644 index 0000000000..c588ece8e6 --- /dev/null +++ b/lib/v2/aqara/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'aqara.com': { + _name: 'Aqara', + '.': [ + { + title: 'News', + docs: 'https://docs.rsshub.app/other.html#aqara-news', + source: ['/news', '/'], + target: '/aqara/news', + }, + ], + }, +}; diff --git a/lib/v2/aqara/router.js b/lib/v2/aqara/router.js new file mode 100644 index 0000000000..69a5972f6a --- /dev/null +++ b/lib/v2/aqara/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/news', require('./news')); +};