From 7be049226904c2ec5b578c2ae2e0387ea7ca2dd2 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Mon, 10 Sep 2018 08:03:09 +0100 Subject: [PATCH] =?UTF-8?q?Add=20=E6=BE=8E=E6=B9=83=E6=96=B0=E9=97=BB=20-?= =?UTF-8?q?=20=E9=A6=96=E9=A1=B5=E5=A4=B4=E6=9D=A1=20(#672)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #615 --- docs/README.md | 10 +++++++ router.js | 3 ++ routes/thepaper/featured.js | 60 +++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 routes/thepaper/featured.js diff --git a/docs/README.md b/docs/README.md index 65224795f..c9501dca8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2197,6 +2197,16 @@ category 列表: - category, 新京报的栏目名, 点击对应栏目后在地址栏找到 +### 澎湃新闻 + +#### 首页头条 + +举例: + +路由: `/thepaper/featured` + +参数: 无 + ### 联合早报 #### 即时新闻 diff --git a/router.js b/router.js index 6525e1b6b..3a993f17d 100644 --- a/router.js +++ b/router.js @@ -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; diff --git a/routes/thepaper/featured.js b/routes/thepaper/featured.js new file mode 100644 index 000000000..016aab3cb --- /dev/null +++ b/routes/thepaper/featured.js @@ -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(' ')[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, + }; +};