From fdbb6fa7098b31ce3beccf93d999d217ae122de6 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 13 Aug 2022 08:31:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20The=20News=20Lens=20?= =?UTF-8?q?=E9=97=9C=E9=8D=B5=E8=A9=95=E8=AB=96=20(#10454)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add The News Lens 關鍵評論 * fix typo --- docs/new-media.md | 50 ++++++++++++- lib/v2/thenewslens/index.js | 79 ++++++++++++++++++++ lib/v2/thenewslens/maintainer.js | 10 +++ lib/v2/thenewslens/radar.js | 55 ++++++++++++++ lib/v2/thenewslens/router.js | 3 + lib/v2/thenewslens/templates/description.art | 6 ++ 6 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 lib/v2/thenewslens/index.js create mode 100644 lib/v2/thenewslens/maintainer.js create mode 100644 lib/v2/thenewslens/radar.js create mode 100644 lib/v2/thenewslens/router.js create mode 100644 lib/v2/thenewslens/templates/description.art diff --git a/docs/new-media.md b/docs/new-media.md index b159798b6..47ae29c56 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -12,7 +12,7 @@ pageClass: routes ### 分类 - + 分类为单一路径,如 `http://www.199it.com/archives/category/199itdata` 则路由为 `/199it/category/199itdata`. @@ -22,7 +22,7 @@ pageClass: routes ### 标签 - + ## 36kr @@ -1272,6 +1272,52 @@ IPFS 网关有可能失效,那时候换成其他网关。 +## The News Lens 關鍵評論 + +### 最新 + + + +| 最新文章 | 最多觀看 | 最多分享 | 本日 | 本週 | 本月 | 今年 | 去年 | 有史以來 | +| ---- | ---- | ------ | --------- | -------- | --------- | -------- | ------------ | ----------- | +| | hot | social | hot/today | hot/week | hot/month | hot/year | hot/lastYear | hot/history | + + + +### 新闻 + + + +| 最新文章 | 最多觀看 | 最多分享 | +| ---- | ---- | ------ | +| | hot | social | + + + +### 作者 + + + +### 分类 + + + +### 标签 + + + +### 频道 + + + +### 评论 + + + +### 影音 + + + ## The Partnership on AI ### Resources diff --git a/lib/v2/thenewslens/index.js b/lib/v2/thenewslens/index.js new file mode 100644 index 000000000..b07567e57 --- /dev/null +++ b/lib/v2/thenewslens/index.js @@ -0,0 +1,79 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const rootUrl = 'https://www.thenewslens.com'; + const currentUrl = `${rootUrl}${ctx.path === '/' ? '/latest-article' : ctx.path}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + $('h1.title').remove(); + + let items = $('.regular-article-list, #list-container, .list-container') + .find('.article-title, .title') + .find('a') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 25) + .toArray() + .map((item) => { + item = $(item); + + const link = item.attr('href'); + + return { + title: item.text(), + link: /\/article\//.test(link) ? `${link}/fullpage` : link, + }; + }); + + 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('#salon_comment_widget-anchor').remove(); + content('ad2-recommender, footer, noscript').remove(); + content('a[data-sk="tooltip_parent"]').parent().remove(); + content('.ad-section, .recommender-title, .navigation-content').remove(); + + content('.article-img-container').each(function () { + content(this).replaceWith( + art(path.join(__dirname, 'templates/description.art'), { + image: content(this).find('img')?.attr('data-srcset').split('?')[0] ?? undefined, + }) + ); + }); + + item.author = content('meta[property="article:author"]').attr('content'); + item.pubDate = parseDate(content('meta[property="article:published_time"]').attr('content')); + item.category = content('meta[property="article:tag"]') + .toArray() + .map((t) => content(t).attr('content')); + item.description = art(path.join(__dirname, 'templates/description.art'), { + image: content('meta[property="og:image"]')?.attr('content').split('?')[0] ?? undefined, + description: content('.article-main-box, article[itemprop="articleBody"]').html(), + }); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/thenewslens/maintainer.js b/lib/v2/thenewslens/maintainer.js new file mode 100644 index 000000000..423c13535 --- /dev/null +++ b/lib/v2/thenewslens/maintainer.js @@ -0,0 +1,10 @@ +module.exports = { + '/author/:id/:sort?': ['nczitzk'], + '/category/:id/:sort?': ['nczitzk'], + '/channel/:id/:sort?': ['nczitzk'], + '/latest-article/:sort?': ['nczitzk'], + '/news/:sort?': ['nczitzk'], + '/review/:sort?': ['nczitzk'], + '/tag/:id/:sort?': ['nczitzk'], + '/videos/Projects/:sort?': ['nczitzk'], +}; diff --git a/lib/v2/thenewslens/radar.js b/lib/v2/thenewslens/radar.js new file mode 100644 index 000000000..4c9752122 --- /dev/null +++ b/lib/v2/thenewslens/radar.js @@ -0,0 +1,55 @@ +module.exports = { + 'thenewslens.com': { + _name: 'The News Lens 關鍵評論', + '.': [ + { + title: '最新', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/latest-article/:sort?', '/'], + target: '/thenewslens/latest-article/:sort?', + }, + { + title: '新闻', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/news/:sort?', '/'], + target: '/thenewslens/news/:sort?', + }, + { + title: '作者', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/author/:id/:sort?', '/'], + target: '/thenewslens/author/:id/:sort?', + }, + { + title: '分类', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/category/:id/:sort?', '/'], + target: '/thenewslens/category/:id/:sort?', + }, + { + title: '标签', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/tag/:id/:sort?', '/'], + target: '/thenewslens/tag/:id/:sort?', + }, + { + title: '频道', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/channel/:id/:sort?', '/'], + target: '/thenewslens/channel/:id/:sort?', + }, + { + title: '评论', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/review/:sort?', '/'], + target: '/thenewslens/review/:sort?', + }, + { + title: '影音', + docs: 'https://docs.rsshub.app/new-media.html#the-news-lens-guan-jian-ping-lun', + source: ['/videos/Projects/:sort?', '/'], + target: '/thenewslens/videos/Projects/:sort?', + }, + ], + }, +}; diff --git a/lib/v2/thenewslens/router.js b/lib/v2/thenewslens/router.js new file mode 100644 index 000000000..406315685 --- /dev/null +++ b/lib/v2/thenewslens/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get(/([\w-/]+)?/, require('./index')); +}; diff --git a/lib/v2/thenewslens/templates/description.art b/lib/v2/thenewslens/templates/description.art new file mode 100644 index 000000000..69e93d20c --- /dev/null +++ b/lib/v2/thenewslens/templates/description.art @@ -0,0 +1,6 @@ +{{ if image }} + +{{ /if }} +{{ if description }} +{{@ description }} +{{ /if }} \ No newline at end of file