refactor: extract juejin utils (#1598)
This commit is contained in:
parent
9298ad40f2
commit
d605c7f17c
|
|
@ -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}`,
|
||||
|
|
|
|||
Loading…
Reference in New Issue