From 668161fe5dc66c32e07c96f93605133c4bd1bbbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=89=E5=87=89?= Date: Mon, 31 Dec 2018 00:59:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0:=20=E6=BE=8E=E6=B9=83?= =?UTF-8?q?=E6=96=B0=E9=97=BB-=E9=A2=91=E9=81=93=20(#1322)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1315 --- docs/README.md | 2 ++ lib/router.js | 1 + lib/routes/thepaper/channel.js | 13 +++++++ lib/routes/thepaper/featured.js | 60 ++------------------------------- lib/routes/thepaper/utils.js | 60 +++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 57 deletions(-) create mode 100644 lib/routes/thepaper/channel.js create mode 100644 lib/routes/thepaper/utils.js diff --git a/docs/README.md b/docs/README.md index edb14719b7..ab27e4d510 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1870,6 +1870,8 @@ Category 列表: + + ### 联合早报 diff --git a/lib/router.js b/lib/router.js index ea92b3578d..ea16999bb1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/thepaper/channel.js b/lib/routes/thepaper/channel.js new file mode 100644 index 0000000000..009ea21fd4 --- /dev/null +++ b/lib/routes/thepaper/channel.js @@ -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, + }; +}; diff --git a/lib/routes/thepaper/featured.js b/lib/routes/thepaper/featured.js index 8f167da0ff..f4b18ad237 100644 --- a/lib/routes/thepaper/featured.js +++ b/lib/routes/thepaper/featured.js @@ -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(' ')[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, }; }; diff --git a/lib/routes/thepaper/utils.js b/lib/routes/thepaper/utils.js new file mode 100644 index 0000000000..09059bc985 --- /dev/null +++ b/lib/routes/thepaper/utils.js @@ -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(' ')[0] + ) + .add(-8 - serverOffset, 'hour') + .toISOString() + : null, + author: content('#v3cont_id > div.news_content > p:nth-child(2)').text(), + }; + return Promise.resolve(single); + }) + ); + }, +};