feat: add 澎湃新闻列表 (#5768)

Co-authored-by: Henry Wang <hi@henry.wang>
This commit is contained in:
Ethan Shen 2020-10-03 07:32:15 +08:00 committed by GitHub
parent 2191eceda8
commit 6cfd57b335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 18 deletions

View File

@ -463,11 +463,21 @@ category 对应的关键词有
### 首页头条
<Route author="HenryQW" example="/thepaper/featured" path="/thepaper/featured"/>
<Route author="HenryQW nczitzk" example="/thepaper/featured" path="/thepaper/featured"/>
### 频道
<Route author="xyqfer" example="/thepaper/channel/27224" path="/thepaper/channel/:id" :paramsDesc="['频道 id']"/>
<Route author="xyqfer nczitzk" example="/thepaper/channel/27224" path="/thepaper/channel/:id" :paramsDesc="['频道 id可在频道页 URL 中找到']">
| 视频 | 时事 | 财经 | 思想 | 澎湃号 | 生活 |
| ----- | ----- | ----- | ----- | ------ | ----- |
| 26916 | 25950 | 25951 | 25952 | 36079 | 25953 |
</Route>
### 列表
<Route author="nczitzk" example="/thepaper/list/25457" path="/thepaper/list/:id" :paramsDesc="['列表 id可在列表页 URL 中找到']"/>
### 澎湃美数组作品集
@ -478,7 +488,6 @@ category 对应的关键词有
| 2 | 4 | 3 | 453 |
</Route>
## 齐鲁晚报
### 新闻

View File

@ -896,6 +896,7 @@ router.get('/embassy/:country/:city?', require('./routes/embassy/index'));
// 澎湃新闻
router.get('/thepaper/featured', require('./routes/thepaper/featured'));
router.get('/thepaper/channel/:id', require('./routes/thepaper/channel'));
router.get('/thepaper/list/:id', require('./routes/thepaper/list'));
// 澎湃美数课
router.get('/thepaper/839studio', require('./routes/thepaper/839studio/studio.js'));

View File

@ -2,12 +2,14 @@ const utils = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
const link = `https://m.thepaper.cn/channel_${id}`;
const items = await utils.ProcessFeed(link, ctx);
const rootUrl = 'https://m.thepaper.cn';
const link = id === '26916' ? `${rootUrl}/channel_26916` : `${rootUrl}/list_page.jsp?nodeid=${id}&isList=0&pageidx=1`;
const items = await utils.ProcessFeed(id === '26916' ? '.list_video_item' : '.list_item_infor', link, ctx);
ctx.state.data = {
title: `澎湃新闻频道 - ${id}`,
link,
link: `${rootUrl}/channel_${id}`,
item: items,
};
};

View File

@ -1,12 +1,14 @@
const utils = require('./utils');
module.exports = async (ctx) => {
const link = 'https://m.thepaper.cn/';
const items = await utils.ProcessFeed(link, ctx);
const rootUrl = 'https://m.thepaper.cn';
const link = `${rootUrl}/list_page.jsp?&nodeid=25949&isList=1&pageidx=1`;
const items = await utils.ProcessFeed('.list_item_infor', link, ctx);
ctx.state.data = {
title: '澎湃新闻 - 首页头条',
link,
link: `${rootUrl}/channel_25949`,
item: items,
};
};

View File

@ -0,0 +1,15 @@
const utils = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
const rootUrl = 'https://m.thepaper.cn';
const link = `${rootUrl}/list_page.jsp?nodeid=${id}&isList=1&pageidx=1`;
const items = await utils.ProcessFeed('.list_item_infor', link, ctx);
ctx.state.data = {
title: `澎湃新闻列表 - ${id}`,
link: `${rootUrl}/list_${id}`,
item: items,
};
};

View File

@ -3,17 +3,16 @@ const date = require('@/utils/date');
const got = require('@/utils/got');
module.exports = {
ProcessFeed: async (link, ctx) => {
ProcessFeed: async (query, link, ctx) => {
const res = await got.get(link);
const $ = cheerio.load(res.data);
const list = $('.list_item').slice(0, 10).get();
const list = $(query).slice(0, 10).get();
return await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const itemUrlID = $(item).find('.list_item_title a').attr('href');
const itemUrl = `https://m.thepaper.cn/${itemUrlID}`;
const itemUrl = `https://m.thepaper.cn/${$(item).find('a').eq(0).attr('href')}`;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
@ -22,14 +21,25 @@ module.exports = {
const res = await got.get(itemUrl);
const content = cheerio.load(res.data);
content('.contheight').replaceWith('<br>');
let description, pubDate;
if (content('div.news_video_msg').length > 0) {
description = content('#vdetail_sum').html();
pubDate = content('div.news_video_msg').html().split('&#xA0;&#xA0;')[0];
} else if (content('#slider_wrapper_ul').length > 0) {
description = '';
pubDate = new Date(date($(item).find('div.list_item_extra span').eq(1).text())).toUTCString();
} else {
description = content('div.newsdetail_content').html();
pubDate = new Date(content('div.date').text().trim().split('来源:')[0].trim() + ' GMT+8').toUTCString();
}
const single = {
title: content('.news_video_name').text() || content('.title').text(),
title: content('title').text(),
link: itemUrl,
description: content('#vdetail_sum_p').html() || content('.news_part_limit div').html(),
pubDate: date(content('.date').text().trim()),
author: content('.author').text(),
description,
pubDate,
author: content('div.author').text(),
};
return Promise.resolve(single);
})