feat: add数英网文章及项目专题 (#2204)

* add 数英网

* fix: 部分标题无法显示

* feat: add数英网文章及项目专题

* remove duplicate route
This commit is contained in:
Fan Shen
2019-05-22 23:03:12 +08:00
committed by DIYgod
parent 09e331988a
commit 40511d1ba5
4 changed files with 161 additions and 3 deletions

View File

@@ -696,6 +696,28 @@ type 为 all 时category 参数不支持 cost 和 free
<Route author="occupy5" example="/digitaling/index" path="/digitaling/index" :paramsDesc="['首页最新文章, 数英网']" /> <Route author="occupy5" example="/digitaling/index" path="/digitaling/index" :paramsDesc="['首页最新文章, 数英网']" />
### 数英网文章专题
<Route author="occupy5" example="/digitaling/articles/latest" path="/digitaling/articles/:category/:subcate?" :paramsDesc="['文章专题分类 ','hot分类下的子类']" />
| 最新文章 | 头条 | 热文 | 精选 |
| -------- | -------- | ---- | ------ |
| latest | headline | hot | choice |
分类`hot`下的子类
| 近期热门文章 | 近期最多收藏 | 近期最多赞 |
| ------------ | ------------ | ---------- |
| views | collects | zan |
### 数英网项目专题
<Route author="occupy5" example="/digitaling/projects/all" path="/digitaling/projects/:category" :paramsDesc="['项目专题分类 ']" />
| 全部 | 每周项目精选 | 每月项目精选 | 海外项目精选 | 近期热门项目 | 近期最多收藏 |
| ---- | ------------ | ------------ | ------------- | ------------ | ------------ |
| all | weekly | monthly | international | hot | favorite |
## 刷屏 ## 刷屏
### 最新 ### 最新

View File

@@ -1317,6 +1317,15 @@ router.get('/enclavebooks/category/:id?', require('./routes/enclavebooks/categor
router.get('/dsndsht23/:subforumid', require('./routes/dsndsht23/index')); router.get('/dsndsht23/:subforumid', require('./routes/dsndsht23/index'));
router.get('/dsndsht23', 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壁纸 // Bing壁纸
router.get('/bing', require('./routes/bing/index')); 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('/ningmeng/song', require('./routes/ningmeng/song'));
// 数英网最新文章
router.get('/digitaling/index', require('./routes/digitaling/index'));
// Steamgifts // Steamgifts
router.get('/steamgifts/discussions/:category?', require('./routes/steamgifts/discussions')); router.get('/steamgifts/discussions/:category?', require('./routes/steamgifts/discussions'));

View File

@@ -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,
};
};

View File

@@ -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,
};
};