From b9cdc5eac22b0147d5eed690c39607e4e0b065eb Mon Sep 17 00:00:00 2001 From: siva Date: Fri, 15 Mar 2019 14:29:31 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E5=A5=BD=E5=A5=87=E5=BF=83=E6=97=A5?= =?UTF-8?q?=E6=8A=A5=E6=94=AF=E6=8C=81=20tag=20=E8=B7=AF=E7=94=B1=20(#1759?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 查看如 https://www.qdaily.com/tags/7294.html 等tag类别的文章 --- docs/README.md | 2 ++ lib/router.js | 1 + lib/routes/qdaily/tag.js | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 lib/routes/qdaily/tag.js diff --git a/docs/README.md b/docs/README.md index f2117c27e3..a232c6ed4a 100755 --- a/docs/README.md +++ b/docs/README.md @@ -662,6 +662,8 @@ RSSHub 提供下列 API 接口: + + ## 编程 ### 掘金 diff --git a/lib/router.js b/lib/router.js index f437af2a3a..16236aee6c 100755 --- a/lib/router.js +++ b/lib/router.js @@ -283,6 +283,7 @@ router.get('/jiemian/list/:cid', require('./routes/jiemian/list.js')); // 好奇心日报 router.get('/qdaily/column/:id', require('./routes/qdaily/column')); router.get('/qdaily/category/:id', require('./routes/qdaily/category')); +router.get('/qdaily/tag/:id', require('./routes/qdaily/tag')); // 爱奇艺 router.get('/iqiyi/dongman/:id', require('./routes/iqiyi/dongman')); diff --git a/lib/routes/qdaily/tag.js b/lib/routes/qdaily/tag.js new file mode 100644 index 0000000000..04fbf5c9ec --- /dev/null +++ b/lib/routes/qdaily/tag.js @@ -0,0 +1,59 @@ +const cheerio = require('cheerio'); +const axios = require('../../utils/axios'); + +module.exports = async (ctx) => { + const url = `https://www.qdaily.com/tags/${ctx.params.id}.html`; + + const res = await axios.get(url); + const $ = cheerio.load(res.data); + const list = $('.article').get(); + + const proList = []; + const indexList = []; + + const out = await Promise.all( + list.map(async (item, i) => { + const $ = cheerio.load(item); + const time = $('.smart-date').attr('data-origindate'); + let title = $('.title').text(); + if (!title) { + title = $('.smart-dotdotdot').text(); + } + const itemUrl = $('a').attr('href'); + const allUrl = `https://www.qdaily.com${itemUrl}`; + const cache = await ctx.cache.get(allUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const single = { + title, + pubDate: new Date(time).toUTCString(), + link: allUrl, + guid: allUrl, + }; + proList.push(axios.get(allUrl)); + indexList.push(i); + return Promise.resolve(single); + }) + ); + const responses = await axios.all(proList); + for (let i = 0; i < responses.length; i++) { + const res = responses[i]; + const $ = cheerio.load(res.data); + $('img').each((index, item) => { + item = $(item); + item.attr('src', item.attr('data-src')); + item.attr('referrerpolicy', 'no-referrer'); + }); + $('.article-detail-bd .author-share').remove(); + $('.article-detail-ft').remove(); + + out[indexList[i]].description = $('.main .com-article-detail').html(); + ctx.cache.set(out[indexList[i]].link, JSON.stringify(out[i]), 24 * 60 * 60); + } + ctx.state.data = { + title: $('title').text(), + link: url, + item: out, + }; +};