From 9e655d004bd5f8fb298e18ea336b70128fd5df5d Mon Sep 17 00:00:00 2001 From: junfengP <840282629@qq.com> Date: Tue, 7 May 2019 01:17:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20=E7=A6=8F=E5=88=A9?= =?UTF-8?q?=E8=B5=84=E6=BA=90=20-=20met.red=20(#2059)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/other.md | 4 +++ lib/router.js | 3 +++ lib/routes/metred/fuli.js | 56 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 lib/routes/metred/fuli.js diff --git a/docs/other.md b/docs/other.md index b509bde5e4..556bebe718 100644 --- a/docs/other.md +++ b/docs/other.md @@ -505,6 +505,10 @@ type 为 all 时,category 参数不支持 cost 和 free +## 福利资源-met.red + + + ## 毕马威 diff --git a/lib/router.js b/lib/router.js index 1de2734b06..4e7670b1a5 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1297,6 +1297,9 @@ router.get('/mlhang', require('./routes/mlhang/latest')); // PlayStation Store router.get('/ps/list/:gridName', require('./routes/ps/list')); +// 福利资源-met.red +router.get('/metred/fuli', require('./routes/metred/fuli')); + // MIT router.get('/mit/graduateadmissions/:type/:name', require('./routes/universities/mit/graduateadmissions')); diff --git a/lib/routes/metred/fuli.js b/lib/routes/metred/fuli.js new file mode 100644 index 0000000000..0888da9e10 --- /dev/null +++ b/lib/routes/metred/fuli.js @@ -0,0 +1,56 @@ +const axios = require('../../utils/axios'); +const host = 'https://met.red'; +const mainPage = 'https://met.red/h/weal/list#'; +const cheerio = require('cheerio'); +const url = require('url'); + +async function load(link, ctx) { + const cache = await ctx.cache.get(link); + if (cache) { + return cache; + } + const response = await axios.get(link); + const $ = cheerio.load(response.data); + const images = $('img'); + for (let k = 0; k < images.length; k++) { + $(images[k]).replaceWith(``); + } + const couponUrl = $('.layui-btn.layui-btn.layui-btn-lg').attr('href'); + let eventHtml; + if (!couponUrl || couponUrl === undefined) { + eventHtml = '

活动链接:无

'; + } else { + eventHtml = `
点我前往活动
`; + } + const description = eventHtml + $('.p-detail-html').html(); + await ctx.cache.set(link, description); + return { description }; +} + +module.exports = async (ctx) => { + const response = await axios({ + method: 'get', + url: mainPage, + }); + const data = response.data; + const $ = cheerio.load(data); + const list = $('h4 > a').get(); + const process = await Promise.all( + list.map(async (item) => { + const itemUrl = host + $(item).attr('href'); + const single = { + title: $(item).text(), + link: itemUrl, + guid: itemUrl, + }; + const other = await load(itemUrl, ctx); + return Promise.resolve(Object.assign({}, single, other)); + }) + ); + ctx.state.data = { + title: '福利资源-met.red', + link: mainPage, + description: '福利资源更新提醒', + item: process, + }; +};