feat: 获取中山网文章全文预览 (#6029)

This commit is contained in:
Laam Pui 2020-10-28 22:57:13 +08:00 committed by GitHub
parent 0e002343ce
commit 81877beca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 6 deletions

View File

@ -1,18 +1,32 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
ctx.params.cateid = ctx.params.cateid || '35';
const response = await got.get(`http://www.zsnews.cn/api/mobile/getlist.html?cateid=${ctx.params.cateid}`);
const out = await Promise.all(
response.data.map(
async (item) =>
await ctx.cache.tryGet(item.url, async () => {
try {
const res = await got.get(item.url);
const $ = cheerio.load(res.data);
item.description = $('.article-content').html();
item.pubDate = new Date($('.article-meta span').first().text().slice(5)).toUTCString();
item.link = item.url;
return item;
} catch (err) {
return Promise.resolve('');
}
})
)
);
ctx.state.data = {
title: '中山新闻',
link: 'https://www.zsnews.cn/',
item: response.data.map((item) => ({
guid: item.id,
title: item.title,
description: item.title,
link: item.url,
})),
item: out,
};
};