From 4b425cfbe7b94f5abcd3844b3f80903da8ced63a Mon Sep 17 00:00:00 2001 From: Haitian Date: Mon, 7 Dec 2020 01:59:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=99=9A=E7=82=B9LatePost=20&=20?= =?UTF-8?q?fix:=20cnbeta=E5=8E=BB=E9=99=A4=E6=96=B0=E5=A2=9E=E5=B9=BF?= =?UTF-8?q?=E5=91=8A=20(#6287)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 12 ++++- lib/router.js | 3 ++ lib/routes/cnbeta/home.js | 1 + lib/routes/latepost/index.js | 85 ++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 lib/routes/latepost/index.js diff --git a/docs/new-media.md b/docs/new-media.md index a0f02f87e..e59baa338 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -116,7 +116,7 @@ pageClass: routes ### 最新 - + ## DeepL @@ -1585,6 +1585,16 @@ column 为 third 时可选的 category: +## 晚点 LatePost + + + +| 最新报道 | 晚点独家 | 人物访谈 | 晚点早知道 | 长报道 | +| -------- | -------- | -------- | ---------- | ------ | +| | 1 | 2 | 3 | 4 | + + + ## 万联网 ### 资讯 diff --git a/lib/router.js b/lib/router.js index 47aab2f26..ec92e8662 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3623,6 +3623,9 @@ router.get('/youdao/latest', require('./routes/youdao/latest')); router.get('/yinxiang/note', require('./routes/yinxiang/note')); router.get('/yinxiang/card/:id?', require('./routes/yinxiang/card')); +// 晚点LatePost +router.get('/latepost/:proma?', require('./routes/latepost/index')); + // 西瓜视频 router.get('/ixigua/user/video/:uid/:disableEmbed?', require('./routes/ixigua/userVideo')); diff --git a/lib/routes/cnbeta/home.js b/lib/routes/cnbeta/home.js index 4b86ac4be..2fbabc280 100644 --- a/lib/routes/cnbeta/home.js +++ b/lib/routes/cnbeta/home.js @@ -10,6 +10,7 @@ module.exports = async (ctx) => { // 移除6.18广告 $('.article-global').remove(); + $('.article-topic').remove(); // 提取内容 return $('.article-summary p').html() + '
' + $('.article-content').html(); diff --git a/lib/routes/latepost/index.js b/lib/routes/latepost/index.js new file mode 100644 index 000000000..518d69bd9 --- /dev/null +++ b/lib/routes/latepost/index.js @@ -0,0 +1,85 @@ +const got = require('@/utils/got'); +const formatPubDate = require('@/utils/date'); +const cheerio = require('cheerio'); +const FormData = require('form-data'); + +const titles = { + '': '最新报道', + 1: '晚点独家', + 2: '人物访谈', + 3: '晚点早知道', + 4: '长报道', +}; + +const rootUrl = 'https://www.latepost.com'; + +const dateAdapter = (date) => { + const incompleteDate = ['今天', '昨天', '前天']; + if (incompleteDate.indexOf(date) !== -1) { + return `${date} 00:00`; + } else { + return date; + } +}; + +module.exports = async (ctx) => { + const proma = ctx.params.proma || ''; + const formData = new FormData(); + formData.append('page', 1); + let data; + if (proma === '') { + const apiUrl = `${rootUrl}/site/index`; + formData.append('limit', 9); + const [first, response] = await Promise.all([ + got({ method: 'get', url: rootUrl }), + got({ + method: 'post', + url: apiUrl, + body: formData, + }), + ]); + const $ = cheerio.load(first.data); + const firstItem = {}; + firstItem.title = $('.headlines-title a').text(); + firstItem.detail_url = `${$('.headlines-title a').attr('href')}`; + data = [firstItem].concat(response.data.data || []); + } else { + const apiUrl = `${rootUrl}/news/get-news-data`; + formData.append('limit', 10); + const response = await got({ + method: 'post', + url: apiUrl, + body: formData, + }); + data = response.data.data; + } + + const items = data.map((item) => ({ + title: item.title, + description: item.abstract, + link: `${rootUrl}${item.detail_url}`, + })); + + ctx.state.data = { + title: `晚点LatePost-${titles[proma]}`, + link: rootUrl, + item: await Promise.all( + items.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ method: 'get', url: item.link }); + // 优化排版 + const $ = cheerio.load( + res.data + .replace(/


<\/p>/g, '') + .replace(/


<\/p>/g, '') + .replace(/


<\/p>/g, '') + ); + item.description = $('.article .abstract').html() + $('.article .article-body').html(); + item.pubDate = formatPubDate(dateAdapter($('.article-header-date').text())); + return item; + }) + ) + ), + }; +};