feat: add 企鹅号全文输出 (#3072)

This commit is contained in:
Hendricks Zheng
2019-09-15 12:51:08 +08:00
committed by DIYgod
parent 32ea59134c
commit 2a164517aa

View File

@@ -1,4 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date = require('@/utils/date');
const iconv = require('iconv-lite');
module.exports = async (ctx) => {
const mid = ctx.params.mid;
@@ -12,24 +15,39 @@ module.exports = async (ctx) => {
const reponse = response.data;
const title = reponse.mediainfo.name;
const description = reponse.mediainfo.intro;
const list = reponse.data;
const list = reponse.data.splice(0, 10);
const items = list.map((item) => {
const title = item.title;
const date = Date(item.timestamp);
const itemUrl = item.vurl;
const author = item.source;
const description = item.abstract;
const items = await Promise.all(
list.map(async (item) => {
const title = item.title;
const unixTimestamp = new Date(item.timestamp * 1000);
const pubDate = date(unixTimestamp.toLocaleString(), 8);
const itemUrl = item.vurl;
const author = item.source;
const abstract = item.abstract;
const response = await ctx.cache.tryGet(
itemUrl,
async () =>
(await got.get(itemUrl, {
responseType: 'buffer',
})).data
);
const html = iconv.decode(response, 'gbk');
const $ = cheerio.load(html, { decodeEntities: false });
const article = $('div.content-article');
const single = {
title,
description: article.html() || abstract,
link: itemUrl,
author,
pubDate,
};
return Promise.resolve(single);
})
);
const single = {
title,
description,
link: itemUrl,
author,
pubDate: date,
};
return single;
});
ctx.state.data = {
title: title,
description: description,