From 4cd8ec6e1fd0514fe0d400d99cd8fb18058629c9 Mon Sep 17 00:00:00 2001
From: cerebrater <49020242+cerebrater@users.noreply.github.com>
Date: Wed, 17 Apr 2019 14:54:39 +0800
Subject: [PATCH] =?UTF-8?q?Matters=20=E6=9C=80=E6=96=B0=E6=96=87=E7=AB=A0?=
=?UTF-8?q?=E7=9A=84=E5=85=A8=E6=96=87=E8=BC=B8=E5=87=BA=20(#1945)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* converts matters-latest into full text
* code cleanup
* hotfix: cache
---
docs/social-media.md | 2 +-
lib/routes/matters/latest.js | 29 ++++++++++++++++++++---------
2 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/docs/social-media.md b/docs/social-media.md
index 0ad5d725bf..3e38c505bf 100644
--- a/docs/social-media.md
+++ b/docs/social-media.md
@@ -474,7 +474,7 @@
## Matters
-
+
diff --git a/lib/routes/matters/latest.js b/lib/routes/matters/latest.js
index 15302006d5..6606bddad8 100644
--- a/lib/routes/matters/latest.js
+++ b/lib/routes/matters/latest.js
@@ -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 | 最新文章',