From e21f09174ce1b4dbc2890ca27e39d391c9104e34 Mon Sep 17 00:00:00 2001 From: yuxinliu-alex Date: Sun, 1 May 2022 21:58:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E7=88=B1=E6=9E=9C=E6=9E=9C-?= =?UTF-8?q?=E6=9C=80=E6=96=B0H5=20(#9648)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 爱果果-最新H5 * Update lib/v2/iguoguo/radar.js * Update lib/v2/iguoguo/radar.js Co-authored-by: yuxinliu --- docs/design.md | 6 +++ lib/v2/iguoguo/index.js | 75 ++++++++++++++++++++++++++++++++++++ lib/v2/iguoguo/maintainer.js | 3 ++ lib/v2/iguoguo/radar.js | 11 ++++++ lib/v2/iguoguo/router.js | 3 ++ 5 files changed, 98 insertions(+) create mode 100644 lib/v2/iguoguo/index.js create mode 100644 lib/v2/iguoguo/maintainer.js create mode 100644 lib/v2/iguoguo/radar.js create mode 100644 lib/v2/iguoguo/router.js diff --git a/docs/design.md b/docs/design.md index 7478284a4..2a6e89a3c 100644 --- a/docs/design.md +++ b/docs/design.md @@ -194,6 +194,12 @@ Behance 用户主页 URL 获取用户名,如 +## 爱果果 + +### 最新H5 + + + ## 优设网 ### 设计专题 diff --git a/lib/v2/iguoguo/index.js b/lib/v2/iguoguo/index.js new file mode 100644 index 000000000..92001fab9 --- /dev/null +++ b/lib/v2/iguoguo/index.js @@ -0,0 +1,75 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); +const rootUrl = 'https://www.iguoguo.net'; + +const getChildrenFromCheerioRoot = (cheerioInstance) => { + const root = cheerioInstance.root(); + const rootKeys = Object.keys(root).filter((n) => n % 1 === 0); + const rootChildren = rootKeys.map((key) => root[key].children); + return rootChildren[0]; +}; + +const getComments = (a, node) => { + if (node.type === 'comment') { + return a.concat([node.data]); + } else if (node.type === 'tag' && node.children && node.children.length > 0) { + return a.concat(node.children.reduce(getComments, [])); + } else { + return a; + } +}; + +module.exports = async (ctx) => { + const currentUrl = rootUrl.concat('/html5'); + const response = await got({ + method: 'get', + url: currentUrl, + }); + const $ = cheerio.load(response.data); + const list = $('a', '.post') + .filter((_, x) => $(x).attr('href').endsWith('.html')) + .filter((_, x) => $(x).children().eq(0).attr('src')) + .map((_, item) => ({ + link: $(item).attr('href'), + cover: $(item).children().eq(0).attr('src'), + })) + .get(); + + const mime = { + jpg: 'jpeg', + png: 'png', + }; + + const items = await Promise.all( + list.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('h1', '.post-info').text(); + item.description = content('div.clearfix', '.post_content').html(); + const comments = getChildrenFromCheerioRoot(content).reduce(getComments, []); + const date = comments[comments.length - 1].trim().split(' ').slice(9, 11); + item.pubDate = timezone(parseDate(date[0] + date[1], 'YYYY-MM-DDHH:mm:ss'), +8); + item.media = { + content: { + url: item.cover, + type: `image/${mime[item.cover.split('.').pop()]}`, + }, + }; + return item; + }) + ) + ); + ctx.state.data = { + title: '爱果果', + link: currentUrl, + description: '爱果果iguoguo是一个优秀酷站、h5、UI素材资源的发布分享平台,是设计师的灵感聚合地和素材下载源。', + language: 'zh-cn', + item: items, + }; +}; diff --git a/lib/v2/iguoguo/maintainer.js b/lib/v2/iguoguo/maintainer.js new file mode 100644 index 000000000..32a7e97c4 --- /dev/null +++ b/lib/v2/iguoguo/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/html5': ['yuxinliu-alex'], +}; diff --git a/lib/v2/iguoguo/radar.js b/lib/v2/iguoguo/radar.js new file mode 100644 index 000000000..b945e994d --- /dev/null +++ b/lib/v2/iguoguo/radar.js @@ -0,0 +1,11 @@ +module.exports = { + 'iguoguo.net': { + _name: '爱果果', + '.': [ + { + title: '最新H5', + docs: 'https://docs.rsshub.app/design.html#ai-guo-guo-h5', + }, + ], + }, +}; diff --git a/lib/v2/iguoguo/router.js b/lib/v2/iguoguo/router.js new file mode 100644 index 000000000..d193460e9 --- /dev/null +++ b/lib/v2/iguoguo/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/html5', require('./index')); +};