Show fulltext description for Sspai Matrix (#1450)

This commit is contained in:
Fei Gao
2019-01-23 16:04:12 +08:00
committed by DIYgod
parent 3b719e4111
commit d727e54a76

View File

@@ -1,4 +1,5 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const api_url = 'https://sspai.com/api/v1/articles?offset=0&limit=20&is_matrix=1&sort=matrix_at&include_total=false';
@@ -7,17 +8,37 @@ module.exports = async (ctx) => {
url: api_url,
});
const data = resp.data.list;
const items = await Promise.all(
data.map(async (item) => {
const link = `https://sspai.com/post/${item.id}`;
let description = '';
const key = `sspai: ${item.id}`;
const value = await ctx.cache.get(key);
if (value) {
description = value;
} else {
const response = await axios({ method: 'get', url: link });
const $ = cheerio.load(response.data);
description = $('#article-content > div').html();
ctx.cache.set(key, description, 24 * 60 * 60);
}
return {
title: item.title.trim(),
description: description,
link: link,
pubDate: new Date(item.released_at * 1000).toUTCString(),
author: item.author.nickname,
};
})
);
ctx.state.data = {
title: '少数派 -- Matrix',
link: 'https://sspai.com/matrix',
description: '少数派 -- Matrix',
item: data.map((item) => ({
title: item.title.trim(),
description: item.summary.trim(),
link: `https://sspai.com/post/${item.id}`,
pubDate: new Date(item.released_at * 1000).toUTCString(),
author: item.author.nickname,
})),
item: items,
};
};