feat: 增加Discuz通用路由 (#3721)

This commit is contained in:
junfengP
2020-01-16 14:20:10 +08:00
committed by DIYgod
parent 8a4e83aa2f
commit 252f314a19
4 changed files with 41 additions and 2 deletions

View File

@@ -14,6 +14,12 @@ pageClass: routes
<Route author="HenryQW" example="/dcard/funny/popular" path="/dcard/:section/:type?" :paramsDesc="['板塊名稱URL 中獲得', '排序popular 熱門latest 最新,默認為 latest']" radar="1"/>
## Discuz
### 通用子版块
<Route author="junfengP" example="/discuz/http%3a%2f%2fwww.u-share.cn%2fforum.php%3fmod%3dforumdisplay%26fid%3d56" path="/discuz/:link" :paramsDesc="['子版块链接, 需要手动Url编码']"/>
## MCBBS
### 版块

View File

@@ -2138,4 +2138,7 @@ router.get('/gbcc/trust', require('./routes/gbcc/trust'));
// Associated Press
router.get('/apnews/topics/:topic', require('./routes/apnews/topics'));
// discuz
router.get('/discuz/:link', require('./routes/discuz/discuz'));
module.exports = router;

View File

@@ -0,0 +1,20 @@
const buildData = require('@/utils/common-config');
module.exports = async (ctx) => {
const link = ctx.params.link;
ctx.state.data = await buildData({
link,
url: link,
title: `%title%`,
params: {
title: `$('head > title').text()`,
},
item: {
item: 'tbody[id^="normalthread"] tr',
title: `$('a.xst').text()`,
link: `$('a.xst').attr('href')`,
description: `$('a.xst').text()`,
pubDate: `require('@/utils/date')($('td.by:nth-child(3) em span').last().text())`,
guid: `$('a.xst').attr('href')`,
},
});
};

View File

@@ -1,5 +1,6 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const iconv = require('iconv-lite');
// eslint-disable-next-line no-unused-vars
const date = require('@/utils/date');
@@ -38,8 +39,17 @@ function getProp(data, prop, $) {
}
async function buildData(data) {
const response = (await got.get(data.url)).data;
const $ = cheerio.load(response);
const response = await got.get(data.url);
const contentType = response.headers['content-type'] || '';
// 若没有指定编码则默认utf-8
let charset = 'utf-8';
for (const attr of contentType.split(';')) {
if (attr.indexOf('charset=') >= 0) {
charset = attr.split('=').pop();
}
}
const responseData = charset === 'utf-8' ? response.data : iconv.decode((await got.get({ url: data.url, responseType: 'buffer' })).data, charset);
const $ = cheerio.load(responseData);
const $item = $(data.item.item);
// 这里应该是可以通过参数注入一些代码的,不过应该无伤大雅
return {