feat: add 新闻联播 (#3004)

* feat: add 新闻联播

* fix: newline

* use dayjs to replace luxon
This commit is contained in:
Tseng Hsiang Sung
2019-09-08 12:47:15 +08:00
committed by DIYgod
parent 67380a6067
commit 6842c76de1
3 changed files with 63 additions and 0 deletions

View File

@@ -315,6 +315,12 @@ category 对应的关键词有
## 央视新闻
### 新闻联播
<Route author="zengxs" example="/cctv/xwlb" path="/cctv/xwlb">
新闻联播内容摘要。
</Route>
### 专题
<Route author="idealclover xyqfer" example="/cctv/world" path="/cctv/:category" :paramsDesc="['分类名']">

View File

@@ -290,6 +290,8 @@ router.get('/tingshuitz/wuhan', require('./routes/tingshuitz/wuhan'));
router.get('/mihoyo/bh3/:type', require('./routes/mihoyo/bh3'));
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'));

55
lib/routes/cctv/xwlb.js Normal file
View File

@@ -0,0 +1,55 @@
const got = require('@/utils/got');
const date = require('@/utils/date');
const cheerio = require('cheerio');
const dayjs = require('dayjs');
require('dayjs/locale/zh-cn');
module.exports = async (ctx) => {
const res = await got({ method: 'get', url: 'http://tv.cctv.com/lm/xwlb/' });
const $ = cheerio.load(res.data);
// 解析最新一期新闻联播的日期
const latestDate = dayjs(
date(
$('.md .mh_title > a')
.text()
.replace(/\s/g, '')
),
{ locale: 'zh-cn' }
);
const count = [];
for (let i = 0; i < 20; i++) {
count.push(i);
}
const resultItems = await Promise.all(
count.map(async (i) => {
const newsDate = latestDate.subtract(i, 'days').set('hour', 19);
const url = `http://tv.cctv.com/lm/xwlb/day/${newsDate.format('YYYYMMDD')}.shtml`;
const item = {
title: `新闻联播 ${newsDate.format('YYYY/MM/DD')}`,
link: url,
pubDate: newsDate.toISOString(),
description: await ctx.cache.tryGet(url, async () => {
const res = await got.get(url);
const content = cheerio.load(res.data);
const alist = new Array();
content('a').map((i, e) => {
const a = content(e);
const href = a.attr('href');
const title = a.find('.text .title').text();
const dur = a.find('.text .bottom').text();
alist.push(`<a href="${href}">${title}${dur}</a>`);
return i;
});
return alist.join('<br/>\n');
}),
};
return Promise.resolve(item);
})
);
ctx.state.data = {
title: 'CCTV 新闻联播',
link: 'http://tv.cctv.com/lm/xwlb/',
item: resultItems,
};
};