diff --git a/docs/new-media.md b/docs/new-media.md index e43896dce2..166b8e6060 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -603,6 +603,10 @@ pageClass: routes +### 标签订阅 + + + ## 世界卫生组织 ### 媒体中心 diff --git a/lib/router.js b/lib/router.js index 18812f588e..f2d18264b7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -716,6 +716,7 @@ router.get('/sspai/column/:id', require('./routes/sspai/column')); router.get('/sspai/author/:id', require('./routes/sspai/author')); router.get('/sspai/topics', require('./routes/sspai/topics')); router.get('/sspai/topic/:id', require('./routes/sspai/topic')); +router.get('/sspai/tag/:keyword', require('./routes/sspai/tag')); // 异次元软件世界 router.get('/iplay/home', require('./routes/iplay/home')); diff --git a/lib/routes/sspai/tag.js b/lib/routes/sspai/tag.js new file mode 100644 index 0000000000..bdc45a9117 --- /dev/null +++ b/lib/routes/sspai/tag.js @@ -0,0 +1,45 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const keyword = ctx.params.keyword; + const api_url = `https://sspai.com/api/v1/articles?offset=0&limit=50&has_tag=1&tag=${keyword}&include_total=false`; + const host = `https://beta.sspai.com/tag/${keyword}`; + const resp = await got({ + method: 'get', + url: api_url, + headers: { + Referer: host, + }, + }); + 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 = item.id; + const value = await ctx.cache.get(key); + if (value) { + description = value; + } else { + const response = await got({ method: 'get', url: link, headers: { Referer: host } }); + const $ = cheerio.load(response.data); + description = $('div.content.wangEditor-txt.clock').html(); + ctx.cache.set(key, description); + } + 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: `#${keyword} - 少数派`, + link: host, + description: `${keyword} 更新推送 `, + item: items, + }; +};