feat: 增加人民日报环保频道 (#2299)

* 增加人民日报环保频道

* feat: 增加人民网环保频道


Co-authored-by: gewj <gewj@sinoyd.com>
This commit is contained in:
xishui
2019-06-04 11:03:38 +08:00
committed by DIYgod
parent a04a88d2cd
commit 434e1afa42
3 changed files with 88 additions and 0 deletions

View File

@@ -241,6 +241,11 @@ category 对应的关键词有
### 观点
<Route author="LogicJake" example="/people/opinion/223228" path="/people/opinion/:id" :paramsDesc="['板块id可在 URL 中找到']"/>
### 环保频道
<Route author="zsimple" example="/people/env/74877" path="/people/env/:id" :paramsDesc="['板块id可在 URL 中找到']"/>
### 习近平系列重要讲话
<Route author="LogicJake" example="/people/xjpjh" path="/people/xjpjh/:keyword?/:year?" :paramsDesc="['关键词,默认不填','年份默认all']"/>

View File

@@ -1167,6 +1167,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'));
// gamersky

82
lib/routes/people/env.js Normal file
View File

@@ -0,0 +1,82 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const iconv = require('iconv-lite');
const host = 'http://env.people.com.cn';
module.exports = async (ctx) => {
const id = ctx.params.id;
const link = `http://env.people.com.cn/GB/${id}/index.html`;
const response = await got.get(link, {
responseType: 'buffer',
});
response.data = iconv.decode(response.data, 'gbk');
const $ = cheerio.load(response.data);
let title = $('div.clearfix.w1000_320.d2nav').text();
title = title.replace(/ >> /g, '—');
const list = $('div.headingNews div.hdNews.clearfix')
.slice(0, 10)
.map(function() {
const info = {
title: $(this)
.find('strong > a')
.text(),
link: $(this)
.find('strong > a')
.attr('href'),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const itemUrl = url.resolve(host, info.link);
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl, {
responseType: 'buffer',
});
response.data = iconv.decode(response.data, 'gbk');
const $ = cheerio.load(response.data);
let date = $('.clearfix.w1000_320.text_title .box01 .fl')
.text()
.split(/\s+/);
if (date.length > 0) {
date = date[0].replace(/(年|月)/g, '/').replace('日', ' ') + ':00';
console.log(date);
} else {
date = new Date();
}
const description = $('div#rwb_zw')
.html()
.trim();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: new Date(date).toUTCString(),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: title,
link: link,
item: out,
};
};