增加: 澎湃新闻-频道 (#1322)

Closes #1315
This commit is contained in:
凉凉
2018-12-31 00:59:30 +08:00
committed by DIYgod
parent 0e9a5de669
commit 668161fe5d
5 changed files with 79 additions and 57 deletions

View File

@@ -1870,6 +1870,8 @@ Category 列表:
<route name="首页头条" author="HenryQW" example="/thepaper/featured" path="/thepaper/featured"/>
<route name="频道" author="xyqfer" example="/thepaper/channel/27224" path="/thepaper/channel/:id" :paramsDesc="['频道 id']"/>
### 联合早报
<route name="即时新闻" author="lengthmin" example="/zaobao/realtime/china" path="/zaobao/realtime/:type?" :paramsDesc="['分类, 缺省为中港台']">

View File

@@ -704,6 +704,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('/dysfz', require('./routes/dysfz/index'));

View File

@@ -0,0 +1,13 @@
const utils = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
const link = `https://m.thepaper.cn/list_${id}`;
const items = await utils.ProcessFeed(link, ctx);
ctx.state.data = {
title: `澎湃新闻频道 - ${id}`,
link,
item: items,
};
};

View File

@@ -1,66 +1,12 @@
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const url = require('url');
const axios = require('../../utils/axios');
const utils = require('./utils');
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')
.slice(0, 10)
.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: $(item)
.find('a')
.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: content('#v3cont_id > div.news_content > p:nth-child(3)').html()
? dayjs(
content('#v3cont_id > div.news_content > p:nth-child(3)')
.html()
.split('&#xA0;')[0]
)
.add(-8 - serverOffset, 'hour')
.toISOString()
: null,
author: content('#v3cont_id > div.news_content > p:nth-child(2)').text(),
};
return Promise.resolve(single);
})
);
const items = await utils.ProcessFeed(link, ctx);
ctx.state.data = {
title: '澎湃新闻 - 首页头条',
link,
item: out,
item: items,
};
};

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 = {
ProcessFeed: async (link, 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 res = await axios_ins.get(link);
const data = res.data;
const $ = cheerio.load(data);
const list = $('p.news_tit01, div.news_tit02')
.slice(0, 10)
.get();
return 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: $(item)
.find('a')
.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: content('#v3cont_id > div.news_content > p:nth-child(3)').html()
? dayjs(
content('#v3cont_id > div.news_content > p:nth-child(3)')
.html()
.split('&#xA0;')[0]
)
.add(-8 - serverOffset, 'hour')
.toISOString()
: null,
author: content('#v3cont_id > div.news_content > p:nth-child(2)').text(),
};
return Promise.resolve(single);
})
);
},
};