Add 澎湃新闻 - 首页头条 (#672)

Close #615
This commit is contained in:
Henry Wang 2018-09-10 08:03:09 +01:00 committed by DIYgod
parent 8d1a071ab3
commit 7be0492269
3 changed files with 73 additions and 0 deletions

View File

@ -2197,6 +2197,16 @@ category 列表:
- category, 新京报的栏目名, 点击对应栏目后在地址栏找到
### 澎湃新闻
#### 首页头条 <Author uid="HenryQW"/>
举例: <https://rsshub.app/thepaper/featured>
路由: `/thepaper/featured`
参数: 无
### 联合早报
#### 即时新闻 <Author uid="lengthmin"/>

View File

@ -541,4 +541,7 @@ router.get('/xclient/app/:name', require('./routes/xclient/app'));
// 中国驻外使领事馆
router.get('/embassy/:country/:city?', require('./routes/embassy/index'));
// 澎湃新闻
router.get('/thepaper/featured', require('./routes/thepaper/featured'));
module.exports = router;

View File

@ -0,0 +1,60 @@
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const url = require('url');
const axios = require('../../utils/axios');
module.exports = async (ctx) => {
const axios_ins = axios.create({
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A356 Safari/604.1',
},
});
const link = 'https://m.thepaper.cn/';
const res = await axios_ins.get(link);
const data = res.data;
const $ = cheerio.load(data);
const list = $('p.news_tit01, div.news_tit02').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const itemUrl = url.resolve(
link,
$(item)
.find('a')
.attr('href')
);
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await axios_ins.get(itemUrl);
const content = cheerio.load(res.data);
const serverOffset = new Date().getTimezoneOffset() / 60;
const single = {
title: content('h1').text(),
guid: itemUrl,
link: itemUrl.replace('https://m.', 'https://'),
description: content('#v3cont_id > div.news_content > div.news_part_father > div > div:nth-child(1)').html(),
pubDate: dayjs(
content('#v3cont_id > div.news_content > p:nth-child(3)')
.html()
.split('&#xA0;')[0]
)
.add(-8 - serverOffset, 'hour')
.toISOString(),
author: content('#v3cont_id > div.news_content > p:nth-child(2)').text(),
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title: '澎湃新闻 - 首页头条',
link,
item: out,
};
};