Matters 最新文章的全文輸出 (#1945)

* converts matters-latest into full text

* code cleanup

* hotfix: cache
This commit is contained in:
cerebrater
2019-04-17 14:54:39 +08:00
committed by DIYgod
parent f5dd564ecd
commit 4cd8ec6e1f
2 changed files with 21 additions and 10 deletions
+1 -1
View File
@@ -474,7 +474,7 @@
## Matters
<Route name="最新排序" author="xyqfer" example="/matters/latest" path="/matters/latest" />
<Route name="最新排序" author="xyqfer Cerebrater" example="/matters/latest" path="/matters/latest" />
<Route name="熱門文章" author="Cerebrater" example="/matters/hot" path="/matters/hot" />
+20 -9
View File
@@ -1,3 +1,4 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
module.exports = async (ctx) => {
@@ -16,16 +17,26 @@ module.exports = async (ctx) => {
},
});
console.log(response.data);
const items = response.data.data.viewer.recommendation.feed.edges.map(({ node }) => {
const link = `https://matters.news/@${node.author.userName}/${encodeURIComponent(node.slug)}-${node.mediaHash}`;
const items = await Promise.all(
response.data.data.viewer.recommendation.feed.edges.map(async ({ node }) => {
const link = `https://matters.news/@${node.author.userName}/${encodeURIComponent(node.slug)}-${node.mediaHash}`;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await axios.get(link);
const $ = cheerio.load(res.data);
const article = $('.u-content').html();
const single = {
title: node.title,
link,
description: article,
};
return {
title: node.title,
link,
description: node.summary,
};
});
ctx.cache.set(link, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'Matters | 最新文章',