diff --git a/docs/traditional-media.md b/docs/traditional-media.md index f352820de..fc91b291f 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -625,6 +625,20 @@ category 对应的关键词有 +### 新闻专题 + + + +::: tip 提示 + +如 [2020 年国家网络安全宣传周](https://news.cctv.com/special/2020gjwlaqxcz/index.shtml) 的专题页 URL 为 id 即为 `2020gjwlaqxcz`。 + +::: + +此处查看 [所有新闻专题](http://news.cctv.com/special/index.shtml) + + + ### 专题 diff --git a/lib/router.js b/lib/router.js index df766a053..71ebedf68 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/cctv/special.js b/lib/routes/cctv/special.js new file mode 100644 index 000000000..1abde6003 --- /dev/null +++ b/lib/routes/cctv/special.js @@ -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 += ``; + date = i.attr('time').replace(/年/g, '-').replace(/月/g, '-').replace(/日/g, ''); + description = i.html(); + }); + + item.description += `

${description}

`; + } 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, + }; +};