feat: add Chiphell support (#4215)

This commit is contained in:
tylinux 2020-03-13 00:30:15 +08:00 committed by GitHub
parent 0eec1d0f39
commit 2a2ebb29e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 0 deletions

View File

@ -4,6 +4,12 @@ pageClass: routes
# 论坛
## Chiphell
### 子版块
<Route author="tylinux" example="/chiphell/forum/80" path="/chiphell/forum/:forumId" :paramsDesc="['子版块 id可在子版块 URL 找到']"/>
## Dcard
### 首頁帖子

View File

@ -2348,4 +2348,7 @@ router.get('/csc/notice/:type?', require('./routes/csc/notice'));
// LearnKu
router.get('/learnku/:community/:category?', require('./routes/learnku/topic'));
// Chiphell
router.get('/chiphell/forum/:forumId?', require('./routes/chiphell/forum'));
module.exports = router;

View File

@ -0,0 +1,82 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
const forumId = ctx.params.forumId;
const feed = await parser.parseURL(`https://www.chiphell.com/forum.php?mod=rss&fid=${forumId}`);
const items = await Promise.all(
feed.items.map(async (item) => {
const cache = await ctx.cache.get(item.link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: item.link,
});
const html = response.data;
const $ = cheerio.load(html);
const description = $('div[class="pcb"]').first();
const imgNode = $(description).find('img');
// replace placeholer image url
imgNode.each((index, element) => {
if (
$(element).attr('src') &&
$(element)
.attr('src')
.indexOf('none.gif') !== -1
) {
$(element).attr('src', $(element).attr('zoomfile'));
}
// remove image size limit
$(element).attr('width', null);
$(element).attr('height', null);
});
// remove image tips
$(description)
.find('div[class*="aimg_tip"]')
.remove();
$(description)
.find('div[class*="tip"]')
.remove();
$(description)
.find('p[class="mbn"]')
.remove();
// remove rate infomation
$(description)
.find('dl[class="rate"]')
.remove();
$(description)
.find('h3[class*="psth"]')
.remove();
const single = {
title: item.title,
description: description.html(),
pubDate: item.pubDate,
link: item.link,
author: item.author,
};
ctx.cache.set(item.link, JSON.stringify(single));
return Promise.resolve(single);
})
);
let title = feed.title.split('-');
title = `${title[title.length - 1]} - Chiphell`;
ctx.state.data = {
title: title,
link: feed.link,
description: feed.description,
item: items,
};
};