diff --git a/lib/routes/sspai/matrix.js b/lib/routes/sspai/matrix.js index 6cc53da08..15e1f464e 100644 --- a/lib/routes/sspai/matrix.js +++ b/lib/routes/sspai/matrix.js @@ -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, }; };