refactor: extract juejin utils (#1598)

This commit is contained in:
Maecenas Li 2019-02-22 14:57:42 +08:00 committed by DIYgod
parent 9298ad40f2
commit d605c7f17c
1 changed files with 2 additions and 34 deletions

View File

@ -1,5 +1,5 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const util = require('./utils');
module.exports = async (ctx) => {
const tag = ctx.params.tag;
@ -31,39 +31,7 @@ module.exports = async (ctx) => {
if (response.data.d && response.data.d.entrylist) {
originalData = response.data.d && response.data.d.entrylist.slice(0, 10);
}
const resultItems = await Promise.all(
originalData.map(async (item) => {
const resultItem = {
title: item.title,
description: `${(item.content || item.summaryInfo || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`,
pubDate: new Date(item.createdAt).toUTCString(),
link: item.originalUrl,
};
if (item.type === 'post') {
const key = 'juejin' + resultItem.link;
const value = await ctx.cache.get(key);
if (value) {
resultItem.description = value;
} else {
const detail = await axios({
method: 'get',
url: item.originalUrl,
headers: {
Referer: item.originalUrl,
},
});
const content = cheerio.load(detail.data);
resultItem.description = content('.article-content')
.html()
.replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')
.replace(/(<img.*?)(data-src)(.*?>)/g, '$1src$3');
ctx.cache.set(key, resultItem.description, 24 * 60 * 60);
}
}
return Promise.resolve(resultItem);
})
);
const resultItems = await util.ProcessFeed(originalData, ctx.cache);
ctx.state.data = {
title: `掘金${cat.title}`,