feat: add zreading fulltext (#2522)

* feat: add zreding fulltext

* feat: add zreading fulltext

* feat: add zreading fulltext

* feat: add zreading fulltext
This commit is contained in:
Cloud 2019-07-01 11:11:48 +08:00 committed by DIYgod
parent 8a6af56529
commit 0281592839
3 changed files with 65 additions and 0 deletions

View File

@ -123,3 +123,9 @@ pageClass: routes
### 章节
<Route author="georeth" example="/zongheng/chapter/672340" path="/zongheng/chapter/:id" :paramsDesc="['小说 id, 可在对应小说页 URL 中找到']"/>
## 左岸读书
### 主页
<Route author="kt286" example="/zreading" path="/zreading" />

View File

@ -1452,4 +1452,7 @@ router.get('/changba/:userid', require('./routes/changba/user'));
// 掌上英雄联盟
router.get('/lolapp/recommend', require('./routes/lolapp/recommend'));
// 左岸读书
router.get('/zreading', require('./routes/zreading/home'));
module.exports = router;

View File

@ -0,0 +1,56 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
const feed = await parser.parseURL('http://www.zreading.cn/feed');
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
$('script').remove();
$('.adsbygoogle').remove();
// 还原图片地址
$('img').each((index, elem) => {
const $elem = $(elem);
$elem.attr('referrerpolicy', 'no-referrer');
});
// 提取内容
return $('.grap').html();
};
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 description = ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.link,
author: item.author,
};
ctx.cache.set(item.link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
};
};