From 560a173956a63f608301fd5ca5d9f3393494e0e5 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 27 Jun 2021 08:08:19 +0800 Subject: [PATCH] feat(route): add Topbook overview (#7440) --- docs/new-media.md | 4 +++ lib/router.js | 1 + lib/routes/topbook/overview.js | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 lib/routes/topbook/overview.js diff --git a/docs/new-media.md b/docs/new-media.md index 3b704a715e..b07c3fb13a 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -613,6 +613,10 @@ Provides all of the Thrillist articles with the specified tag. ## Topbook +### Overview + + + ### 今天看什么 diff --git a/lib/router.js b/lib/router.js index 8254645f67..625cd8cc50 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4075,6 +4075,7 @@ router.get('/jisilu/topic/:user', require('./routes/jisilu/topic')); router.get('/verfghbw/press/:keyword?', require('./routes/verfghbw/press')); // Topbook +router.get('/topbook/overview/:id?', require('./routes/topbook/overview')); router.get('/topbook/today', require('./routes/topbook/today')); // Melon diff --git a/lib/routes/topbook/overview.js b/lib/routes/topbook/overview.js new file mode 100644 index 0000000000..77aa7d42ae --- /dev/null +++ b/lib/routes/topbook/overview.js @@ -0,0 +1,48 @@ +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const id = ctx.params.id || '24'; + + const rootUrl = 'https://topbook.cc'; + const categoryUrl = `${rootUrl}/webapi/content/category/${id}/query`; + const currentUrl = `${rootUrl}/webapi/content/article/${id}/page?start=0&limit=24`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const categoryResponse = await got({ + method: 'get', + url: categoryUrl, + }); + + const list = response.data.data.items.map((item) => ({ + title: item.title, + pubDate: Date.parse(item.createTime), + link: `${rootUrl}/webapi/content/article/query/${item.articleId}`, + })); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + item.author = detailResponse.data.data.nickname; + item.description = detailResponse.data.data.content; + item.link = `${rootUrl}/overview?selectedArticle=${item.link.split('/query/')[1]}`; + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${categoryResponse.data.data.name} - Topbook`, + link: `${rootUrl}/overview/${id}`, + item: items, + }; +};