From 40511d1ba5e280ab5e9b38ef6ac185250e1b7ec8 Mon Sep 17 00:00:00 2001 From: Fan Shen <1370275510@qq.com> Date: Wed, 22 May 2019 23:03:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=E6=95=B0=E8=8B=B1=E7=BD=91=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=8F=8A=E9=A1=B9=E7=9B=AE=E4=B8=93=E9=A2=98=20(#2204?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add 数英网 * fix: 部分标题无法显示 * feat: add数英网文章及项目专题 * remove duplicate route --- docs/other.md | 22 ++++++++++ lib/router.js | 12 ++++-- lib/routes/digitaling/article.js | 73 ++++++++++++++++++++++++++++++++ lib/routes/digitaling/project.js | 57 +++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 lib/routes/digitaling/article.js create mode 100644 lib/routes/digitaling/project.js diff --git a/docs/other.md b/docs/other.md index 2af9c4dd75..ab43a1a86f 100644 --- a/docs/other.md +++ b/docs/other.md @@ -696,6 +696,28 @@ type 为 all 时,category 参数不支持 cost 和 free +### 数英网文章专题 + + + +| 最新文章 | 头条 | 热文 | 精选 | +| -------- | -------- | ---- | ------ | +| latest | headline | hot | choice | + +分类`hot`下的子类 + +| 近期热门文章 | 近期最多收藏 | 近期最多赞 | +| ------------ | ------------ | ---------- | +| views | collects | zan | + +### 数英网项目专题 + + + +| 全部 | 每周项目精选 | 每月项目精选 | 海外项目精选 | 近期热门项目 | 近期最多收藏 | +| ---- | ------------ | ------------ | ------------- | ------------ | ------------ | +| all | weekly | monthly | international | hot | favorite | + ## 刷屏 ### 最新 diff --git a/lib/router.js b/lib/router.js index 94cd769ea7..2fa15ef5db 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1317,6 +1317,15 @@ router.get('/enclavebooks/category/:id?', require('./routes/enclavebooks/categor router.get('/dsndsht23/:subforumid', require('./routes/dsndsht23/index')); router.get('/dsndsht23', require('./routes/dsndsht23/index')); +// 数英网最新文章 +router.get('/digitaling/index', require('./routes/digitaling/index')); + +// 数英网文章专题 +router.get('/digitaling/articles/:category/:subcate', require('./routes/digitaling/article')); + +// 数英网项目专题 +router.get('/digitaling/projects/:category', require('./routes/digitaling/project')); + // Bing壁纸 router.get('/bing', require('./routes/bing/index')); @@ -1326,9 +1335,6 @@ router.get('/maxnews/dota2', require('./routes/maxnews/dota2')); // 柠檬 - 私房歌 router.get('/ningmeng/song', require('./routes/ningmeng/song')); -// 数英网最新文章 -router.get('/digitaling/index', require('./routes/digitaling/index')); - // Steamgifts router.get('/steamgifts/discussions/:category?', require('./routes/steamgifts/discussions')); diff --git a/lib/routes/digitaling/article.js b/lib/routes/digitaling/article.js new file mode 100644 index 0000000000..205f910609 --- /dev/null +++ b/lib/routes/digitaling/article.js @@ -0,0 +1,73 @@ +const cheerio = require('cheerio'); +const axios = require('@/utils/axios'); + +// 分类标题 +const categoryTitle = new Map([ + ['latest', { name: '最新文章', type: {} }], + ['headline', { name: '头条', type: {} }], + [ + 'hot', + { + name: '热文', + type: { + views: '近期热门文章', + collects: '近期最多收藏', + zan: '近期最多赞', + }, + }, + ], + ['choice', { name: '精选', type: {} }], +]); +module.exports = async (ctx) => { + let category = ctx.params.category || 'latest'; + const subcate = ctx.params.subcate; + // 获取分类标题 + let title = categoryTitle.get(category).name; + if (category === 'latest') { + // 获取最新文章 + category = ''; + } + let link = `https://www.digitaling.com/articles/${category}/`; + // 二级子分类 + if (subcate !== undefined) { + link = link + `${subcate}`; + title = title + '/' + categoryTitle.get(category).type[subcate]; + } + const res = await axios.get(link); + const $ = cheerio.load(res.data); + // 文章列表 + const list = $('.con_list 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: $('.clearfix 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: `数英网-文章专题-${title}`, + link: link, + item: out, + }; +}; diff --git a/lib/routes/digitaling/project.js b/lib/routes/digitaling/project.js new file mode 100644 index 0000000000..05749ceb7f --- /dev/null +++ b/lib/routes/digitaling/project.js @@ -0,0 +1,57 @@ +const cheerio = require('cheerio'); +const axios = require('@/utils/axios'); + +// 分类标题 +const categoryTitle = new Map([ + ['all', { name: '全部' }], + ['weekly', { name: '每周项目精选' }], + ['monthly', { name: '每月项目精选' }], + ['international', { name: '海外项目精选' }], + ['hot', { name: '近期热门项目' }], + ['favorite', { name: '近期最多收藏' }], +]); + +module.exports = async (ctx) => { + const category = ctx.params.category; + const link = `https://www.digitaling.com/projects/${category}`; + const res = await axios.get(link); + const $ = cheerio.load(res.data); + // 标题 + const title = categoryTitle.get(category).name; + // 文章列表 + const list = $('#pro_list .works_title 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: $('.clearfix 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: `数英网-项目专题-${title}`, + link: link, + item: out, + }; +};