From 2c8aa2dec77c2311748a361791bbac269006468d Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 14 Mar 2021 14:10:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=B8=AD=E5=9B=BD?= =?UTF-8?q?=E5=85=B1=E4=BA=A7=E5=85=9A=E6=96=B0=E9=97=BB=E7=BD=91=2024=20?= =?UTF-8?q?=E5=B0=8F=E6=97=B6=E6=BB=9A=E5=8A=A8=E6=96=B0=E9=97=BB=20(#6931?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add 中国共产党新闻网 24 小时滚动新闻 * fix typo * fix typo * fix router.js * fix router.js --- docs/traditional-media.md | 4 +++ lib/router.js | 1 + lib/routes/people/cpc/24h.js | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/routes/people/cpc/24h.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 5955668956..acd2283f28 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -716,6 +716,10 @@ category 对应的关键词有 +### 中国共产党新闻网 24 小时滚动新闻 + + + ## 人民日报社 国际金融报 ### 栏目 diff --git a/lib/router.js b/lib/router.js index bba91dd169..acc1379c36 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1557,6 +1557,7 @@ router.get('/banyuetan/:name', require('./routes/banyuetan')); router.get('/people/opinion/:id', require('./routes/people/opinion')); router.get('/people/env/:id', require('./routes/people/env')); router.get('/people/xjpjh/:keyword?/:year?', require('./routes/people/xjpjh')); +router.get('/people/cpc/24h', require('./routes/people/cpc/24h')); // 北极星电力网 router.get('/bjx/huanbao', require('./routes/bjx/huanbao')); diff --git a/lib/routes/people/cpc/24h.js b/lib/routes/people/cpc/24h.js new file mode 100644 index 0000000000..7053c83dc3 --- /dev/null +++ b/lib/routes/people/cpc/24h.js @@ -0,0 +1,53 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const rootUrl = 'http://cpc.people.com.cn'; + const currentUrl = `${rootUrl}/GB/87228/index.html`; + const response = await got({ + method: 'get', + url: currentUrl, + responseType: 'buffer', + }); + + const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + + const list = $('.fl ul li a') + .slice(0, 10) + .map((_, item) => { + item = $(item); + const link = item.attr('href'); + + return { + title: item.text(), + link: `${link.indexOf('http') < 0 ? rootUrl : ''}${link}`, + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + responseType: 'buffer', + }); + const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk')); + + item.description = content('.show_text').html(); + item.pubDate = new Date(content('meta[name="publishdate"]').attr('content')).toUTCString(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};