From 43673a5147153a5ea13181702bcd119be613e11d Mon Sep 17 00:00:00 2001 From: Fan Shen <1370275510@qq.com> Date: Sun, 19 May 2019 17:15:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=95=B0=E8=8B=B1=E7=BD=91=20(#2?= =?UTF-8?q?166)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/other.md | 6 +++++ lib/router.js | 3 +++ lib/routes/digitaling/index.js | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 lib/routes/digitaling/index.js diff --git a/docs/other.md b/docs/other.md index fe91ddaa33..915596702a 100644 --- a/docs/other.md +++ b/docs/other.md @@ -416,6 +416,12 @@ type 为 all 时,category 参数不支持 cost 和 free +## 数英网 + +### 数英网最新文章 + + + ## 果壳网 ### 科学人 diff --git a/lib/router.js b/lib/router.js index 415812c412..296fc79ee9 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1309,4 +1309,7 @@ router.get('/enclavebooks/category/:id?', require('./routes/enclavebooks/categor // 色花堂 router.get('/dsndsht23', require('./routes/dsndsht23/index')); +// 数英网最新文章 +router.get('/digitaling/index', require('./routes/digitaling/index')); + module.exports = router; diff --git a/lib/routes/digitaling/index.js b/lib/routes/digitaling/index.js new file mode 100644 index 0000000000..6f83f99fb0 --- /dev/null +++ b/lib/routes/digitaling/index.js @@ -0,0 +1,44 @@ +const cheerio = require('cheerio'); +const axios = require('@/utils/axios'); + +module.exports = async (ctx) => { + const link = 'https://www.digitaling.com'; + const res = await axios.get(link); + const $ = cheerio.load(res.data); + // 文章列表 + const list = $('#home_latest li h3') + .find('a') + .map((i, e) => $(e).attr('href')) + .get(); + + const out = await Promise.all( + list.map(async (itemUrl) => { + const cache = await ctx.cache.get(itemUrl); + // 判断缓存 + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const res = await axios.get(itemUrl); + const $ = cheerio.load(res.data); + + const item = { + title: $('.project_title h2').text(), + author: $('#avatar_by') + .find('a') + .text(), + link: itemUrl, + description: $('#article_con').html(), + pubDate: new Date().toUTCString(), + }; + ctx.cache.set(itemUrl, JSON.stringify(item)); + + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: '数英网-最新文章', + link: link, + item: out, + }; +};