Skip to content

Commit

Permalink
fix: some bugs in discuz (DIYgod#3951)
Browse files Browse the repository at this point in the history
  • Loading branch information
junfengP authored Feb 15, 2020
1 parent c2bd5ea commit 2e22386
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
12 changes: 11 additions & 1 deletion docs/bbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ pageClass: routes

## Discuz

### 通用子版块
### 通用子版块-自动检测

<Route author="junfengP" example="/discuz/http%3a%2f%2fwww.u-share.cn%2fforum.php%3fmod%3dforumdisplay%26fid%3d56" path="/discuz/:link" :paramsDesc="['子版块链接, 需要手动Url编码']"/>

### 通用子版块-指定版本

<Route author="junfengP" example="/discuz/x/https%3a%2f%2fwww.52pojie.cn%2fforum-16-1.html" path="/discuz/:ver/:link" :paramsDesc="['discuz版本类型,见下表','子版块链接, 需要手动Url编码']" >

| Discuz X 系列 | Discuz 7.x 系列 |
| ------------- | --------------- |
| x | 7 |

</Route>

## MCBBS

### 版块
Expand Down
16 changes: 16 additions & 0 deletions docs/en/bbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@ pageClass: routes
---

# BBS

## Discuz

### General Subforum - Auto detection

<Route author="junfengP" example="/discuz/http%3a%2f%2fwww.u-share.cn%2fforum.php%3fmod%3dforumdisplay%26fid%3d56" path="/discuz/:link" :paramsDesc="['link of subforum, require url encoded ']"/>

### General Subforum - Manual version

<Route author="junfengP" example="/discuz/x/https%3a%2f%2fwww.52pojie.cn%2fforum-16-1.html" path="/discuz/:ver/:link" :paramsDesc="['discuz version,see below table','link of subforum, require url encoded']" >

| Discuz X Series | Discuz 7.x Series |
| --------------- | ----------------- |
| x | 7 |

</Route>
1 change: 1 addition & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,7 @@ router.get('/gbcc/trust', require('./routes/gbcc/trust'));
router.get('/apnews/topics/:topic', require('./routes/apnews/topics'));

// discuz
router.get('/discuz/:ver([7|x])/:link(.*)', require('./routes/discuz/discuz'));
router.get('/discuz/:link(.*)', require('./routes/discuz/discuz'));

// China Dialogue 中外对话
Expand Down
20 changes: 8 additions & 12 deletions lib/routes/discuz/discuz.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function load(baseUrl, itemLink, ctx, charset) {

module.exports = async (ctx) => {
let link = ctx.params.link;
const ver = ctx.params.ver ? ctx.params.ver.toUpperCase() : undefined;
link = link.replace(/:\/\//, ':/').replace(/:\//, '://');

const response = await got.get(link);
Expand All @@ -52,18 +53,18 @@ module.exports = async (ctx) => {
let charset = 'utf-8';
for (const attr of contentType.split(';')) {
if (attr.indexOf('charset=') >= 0) {
charset = attr.split('=').pop();
charset = attr
.split('=')
.pop()
.toLowerCase();
}
}
const responseData = charset === 'utf-8' ? response.data : iconv.decode((await got.get({ url: link, responseType: 'buffer' })).data, charset);
const $ = cheerio.load(responseData);
const title = $('head > title').text();
const version = ver ? 'DISCUZ! ' + ver : $('head > meta[name=generator]').attr('content');
let process;
if (
$('div#footer p em')
.text()
.startsWith('7')
) {
if (version.toUpperCase().startsWith('DISCUZ! 7')) {
// discuz 7.x 系列
// 支持全文抓取,限制抓取页面5个
const list = $('tbody[id^="normalthread"] tr')
Expand All @@ -82,12 +83,7 @@ module.exports = async (ctx) => {
return Promise.resolve(Object.assign({}, single, detail));
})
);
} else if (
$('div#frt p em')
.text()
.toUpperCase()
.startsWith('X')
) {
} else if (version.toUpperCase().startsWith('DISCUZ! X')) {
// discuz X 系列
// 支持全文抓取,限制抓取页面5个
const list = $('tbody[id^="normalthread"] tr')
Expand Down

0 comments on commit 2e22386

Please sign in to comment.