feat: add 晚点LatePost & fix: cnbeta去除新增广告 (#6287)

This commit is contained in:
Haitian 2020-12-07 01:59:03 +08:00 committed by GitHub
parent 67dfa2977a
commit 4b425cfbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 1 deletions

View File

@ -116,7 +116,7 @@ pageClass: routes
### 最新
<Route author="kt286" example="/cnbeta" path="/cnbeta"/>
<Route author="kt286 HaitianLiu" example="/cnbeta" path="/cnbeta"/>
## DeepL
@ -1585,6 +1585,16 @@ column 为 third 时可选的 category:
</Route>
## 晚点 LatePost
<Route author="HaitianLiu nczitzk" example="/latepost" path="/latepost/:proma?" :paramsDesc="['栏目 id见下表默认为最新报道']">
| 最新报道 | 晚点独家 | 人物访谈 | 晚点早知道 | 长报道 |
| -------- | -------- | -------- | ---------- | ------ |
| | 1 | 2 | 3 | 4 |
</Route>
## 万联网
### 资讯

View File

@ -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'));

View File

@ -10,6 +10,7 @@ module.exports = async (ctx) => {
// 移除6.18广告
$('.article-global').remove();
$('.article-topic').remove();
// 提取内容
return $('.article-summary p').html() + '<br>' + $('.article-content').html();

View File

@ -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 class="ql-align-justify"><br><\/p>/g, '')
.replace(/<p class="ql-align-center"><br><\/p>/g, '')
.replace(/<p><br><\/p>/g, '')
);
item.description = $('.article .abstract').html() + $('.article .article-body').html();
item.pubDate = formatPubDate(dateAdapter($('.article-header-date').text()));
return item;
})
)
),
};
};