diff --git a/docs/new-media.md b/docs/new-media.md index 71dc92f1a..19d860d64 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -10,6 +10,14 @@ pageClass: routes +### 用户文章 + + + +### 主题文章 + + + ### 搜索文章 diff --git a/lib/router.js b/lib/router.js index 1ac64996a..a420359a7 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1107,6 +1107,8 @@ router.get('/tingdiantz/nanjing', require('./routes/tingdiantz/nanjing')); // 36kr router.get('/36kr/search/article/:keyword', require('./routes/36kr/search/article')); router.get('/36kr/newsflashes', require('./routes/36kr/newsflashes')); +router.get('/36kr/user/:uid', require('./routes/36kr/user')); +router.get('/36kr/motif/:mid', require('./routes/36kr/motif')); // PMCAFF router.get('/pmcaff/list/:typeid', require('./routes/pmcaff/list')); diff --git a/lib/routes/36kr/motif.js b/lib/routes/36kr/motif.js new file mode 100644 index 000000000..e9ebcab5e --- /dev/null +++ b/lib/routes/36kr/motif.js @@ -0,0 +1,36 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const motifUrl = `https://36kr.com/motif/${ctx.params.mid}`; + const response = await got({ + method: 'get', + url: motifUrl, + }); + const data = JSON.parse(response.data.match(/"motifDetailData":{"code":0,"data":(.*?)},"channel":/)[1]); + + const motifInfo = data.motifInfo.data; + const motifArticleList = data.motifArticleList.data.itemList; + const articleList = motifArticleList.map((item) => ({ + title: item.templateMaterial.widgetTitle, + link: `https://36kr.com/p/${item.itemId}`, + pubDate: new Date(item.templateMaterial.publishTime).toUTCString(), + })); + + ctx.state.data = { + title: `36氪专题 - ${motifInfo.categoryTitle}`, + link: motifUrl, + item: await Promise.all( + articleList.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const contentResponse = await got({ method: 'get', url: item.link }); + const content = cheerio.load(contentResponse.data); + item.description = content('div.common-width.content.articleDetailContent.kr-rich-text-wrapper').html(); + return item; + }) + ) + ), + description: `${motifInfo.categoryTitle}`, + }; +}; diff --git a/lib/routes/36kr/user.js b/lib/routes/36kr/user.js new file mode 100644 index 000000000..dce7a2e0d --- /dev/null +++ b/lib/routes/36kr/user.js @@ -0,0 +1,36 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const userUrl = `https://36kr.com/user/${ctx.params.uid}`; + const response = await got({ + method: 'get', + url: userUrl, + }); + const data = JSON.parse(response.data.match(/"authorDetailData":{"code":0,"data":(.*?)},"channel":/)[1]); + + const authorInfo = data.authorInfo.data; + const authorArticleList = data.authorArticleList.data.itemList; + const articleList = authorArticleList.map((item) => ({ + title: item.templateMaterial.widgetTitle, + link: `https://36kr.com/p/${item.itemId}`, + pubDate: new Date(item.templateMaterial.publishTime).toUTCString(), + })); + + ctx.state.data = { + title: `36氪用户 - ${authorInfo.userNick}`, + link: userUrl, + item: await Promise.all( + articleList.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const contentResponse = await got({ method: 'get', url: item.link }); + const content = cheerio.load(contentResponse.data); + item.description = content('div.common-width.content.articleDetailContent.kr-rich-text-wrapper').html(); + return item; + }) + ) + ), + description: `${authorInfo.label} | ${authorInfo.summary}`, + }; +};