feat: add 央视网新闻专题 (#5700)

This commit is contained in:
Ethan Shen
2020-09-25 11:37:11 +01:00
committed by GitHub
parent 25fb166ee4
commit 8b25d3aec8
3 changed files with 124 additions and 0 deletions
+14
View File
@@ -625,6 +625,20 @@ category 对应的关键词有
</Route>
### 新闻专题
<Route author="nczitzk" example="/cctv-special/315" path="/cctv-special/:id?" :paramsDesc="['专题 id,可在对应专题页 URL 中找到,默认为 `315` 即 3·15晚会']">
::: tip 提示
如 [2020 年国家网络安全宣传周](https://news.cctv.com/special/2020gjwlaqxcz/index.shtml) 的专题页 URL 为 <https://news.cctv.com/special/2020gjwlaqxcz/index.shtml。其专题> id 即为 `2020gjwlaqxcz`。
:::
此处查看 [所有新闻专题](http://news.cctv.com/special/index.shtml)
</Route>
### 专题
<Route author="idealclover xyqfer" example="/cctv/world" path="/cctv/:category" :paramsDesc="['分类名']">
+1
View File
@@ -363,6 +363,7 @@ router.get('/mihoyo/bh2/:type', require('./routes/mihoyo/bh2'));
router.get('/cctv/xwlb', require('./routes/cctv/xwlb'));
// 央视新闻
router.get('/cctv/:category', require('./routes/cctv/category'));
router.get('/cctv-special/:id?', require('./routes/cctv/special'));
// 财新博客
router.get('/caixin/blog/:column', require('./routes/caixin/blog'));
+109
View File
@@ -0,0 +1,109 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
ctx.params.id = ctx.params.id || '315';
let rootUrl;
if (ctx.params.id === '315') {
rootUrl = `https://315.cctv.com`;
} else {
rootUrl = `https://news.cctv.com/special/${ctx.params.id}/index.shtml`;
}
const response = await got({
method: 'get',
url: rootUrl,
});
const $ = cheerio.load(response.data);
const list = $('h3>a, h4>a')
.slice(0, 10)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
let date;
if (item.link.indexOf('photo.cctv.com') > 0) {
const imageLink = item.link.split('.shtml')[0] + '.xml?randomNum=26';
const imageResponse = await got({
method: 'get',
url: imageLink,
});
const images = cheerio.load(imageResponse.data);
let description;
item.description = '';
images('li')
.get()
.forEach((i) => {
i = images(i);
item.description += `<img src="${i.attr('photourl')}">`;
date = i.attr('time').replace(/年/g, '-').replace(/月/g, '-').replace(/日/g, '');
description = i.html();
});
item.description += `<p>${description}</p>`;
} else {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
const match = detailResponse.data.match(/publishDate ="(.*)";/);
if (match === null) {
let dateSpan;
if (content('div.info span').html() !== null) {
dateSpan = content('div.info span').text();
} else if (content('div.info1').html() !== null) {
dateSpan = content('div.info1').text().split(' | ')[1];
} else {
dateSpan = content('span.time').text();
}
date = dateSpan.replace(/年/g, '-').replace(/月/g, '-').replace(/日/g, '');
} else {
const dateString = match[1];
date = `${dateString.slice(0, 4)}-${dateString.slice(4, 6)}-${dateString.slice(6, 8)} ${dateString.slice(8, 10)}:${dateString.slice(10, 12)}:${dateString.slice(12, 14)}`;
}
if (content('#content_area').html() !== null) {
item.description = content('#content_area').html();
} else if (content('div.cnt_bd').html() !== null) {
content('div.function,h1,h2').remove();
item.description = content('div.cnt_bd').html();
} else if (content('#text_area').html() !== null) {
item.description = content('#text_area').html();
} else {
item.description = content('div.con').html();
}
}
item.pubDate = new Date(date + ' GMT+8').toUTCString();
return item;
})
)
);
ctx.state.data = {
title: `${$('title').text().split('_')[0]} - 新闻专题 - 央视网`,
link: rootUrl,
item: items,
};
};