feat: 添加CNU视觉联盟 (#3152)

* add cnu

* fix duplicate
This commit is contained in:
hoilc
2019-09-27 16:12:20 +08:00
committed by DIYgod
parent f8400d4f5b
commit b4850f8560
5 changed files with 160 additions and 0 deletions
+14
View File
@@ -16,6 +16,20 @@ pageClass: routes
<Route author="FHYunCai" example="/bing" path="/bing"/>
## CNU 视觉联盟
### 每日精选
<Route author="hoilc" example="/cnu/selected" path="/cnu/selected" />
### 发现
<Route author="hoilc" example="/cnu/discovery/hot/自然" path="/cnu/discovery/:type?/:category?" :paramsDesc="['板块类型, 默认为`热门`, 具体参见下表', '图片类别, 默认为`0`代表全部, 可参见[这里](http://www.cnu.cc/discoveryPage/hot-0)']"/>
| 热门 | 推荐 | 最新 |
| ---- | --------- | ------ |
| hot | recommend | recent |
## Dilbert Comic Strip
<Route name="Daily Strip" author="Maecenas" example="/dilbert/strip" path="/dilbert/strip">
+4
View File
@@ -1763,4 +1763,8 @@ router.get('/taptap/review/:id/:order?', require('./routes/taptap/review'));
// lofter
router.get('/lofter/tag/:name/:type?', require('./routes/lofter/tag'));
// CNU视觉联盟
router.get('/cnu/selected', require('./routes/cnu/selected'));
router.get('/cnu/discovery/:type?/:category?', require('./routes/cnu/discovery'));
module.exports = router;
+62
View File
@@ -0,0 +1,62 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
const types = {
hot: '热门',
recommend: '推荐',
recent: '最新',
};
module.exports = async (ctx) => {
const type = ctx.params.type ? ctx.params.type : 'hot';
const category = ctx.params.category ? ctx.params.category : '0';
const category_name = category === '0' ? '全部' : category;
const url = `http://www.cnu.cc/discoveryPage/${type}-${encodeURIComponent(category)}`;
const list_response = await got.get(url);
const $ = cheerio.load(list_response.data);
const list = $('.grid-item.work-thumbnail').toArray();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('.title');
const link = $('a.thumbnail').attr('href');
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const rssitem = {
title: title.text().trim(),
link: link,
};
try {
const response = await got.get(link);
const result = utils.parseContent(response.data);
if (!result) {
return Promise.resolve('');
}
rssitem.author = result.author;
rssitem.description = result.description;
rssitem.pubDate = result.pubDate;
} catch (err) {
return Promise.resolve('');
}
ctx.cache.set(link, JSON.stringify(rssitem));
return Promise.resolve(rssitem);
})
);
ctx.state.data = {
title: `CNU视觉联盟 - ${types[type]} - ${category_name}`,
link: url,
item: out.filter((item) => item !== ''),
};
};
+52
View File
@@ -0,0 +1,52 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
module.exports = async (ctx) => {
const url = 'http://www.cnu.cc/selectedPage';
const list_response = await got.get(url);
const $ = cheerio.load(list_response.data);
const list = $('.selectedUL > .bannerLi').toArray();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('.workTitle');
const link = title.parent().attr('href');
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const rssitem = {
title: title.text().trim(),
link: link,
};
try {
const response = await got.get(link);
const result = utils.parseContent(response.data);
if (!result) {
return Promise.resolve('');
}
rssitem.author = result.author;
rssitem.description = result.description;
rssitem.pubDate = result.pubDate;
} catch (err) {
return Promise.resolve('');
}
ctx.cache.set(link, JSON.stringify(rssitem));
return Promise.resolve(rssitem);
})
);
ctx.state.data = {
title: `CNU视觉联盟 - 每日精选`,
link: url,
item: out.filter((item) => item !== ''),
};
};
+28
View File
@@ -0,0 +1,28 @@
const cheerio = require('cheerio');
const pic_base_url = 'http://img.cnu.cc/uploads/images/920/';
const parseContent = (htmlString) => {
const $ = cheerio.load(htmlString);
const author = $('.author-info > a');
const time = $('.author-info > .timeago');
const content = $('#work_body');
const imgs_json = JSON.parse($('#imgs_json').text());
for (let k = 0; k < imgs_json.length; k++) {
content.append(`<img src="${pic_base_url}${imgs_json[k].img}" />`);
content.append(`<div class='img_description'>${imgs_json[k].content}</div>`);
content.append(`<p>${imgs_json[k].text}</p>`);
}
return {
author: author.text().trim(),
description: content.html(),
pubDate: new Date(time.text().trim()),
};
};
module.exports = {
parseContent,
};