From 40b376b7218a23970a2b48c476b58389e63d00fa Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 8 Nov 2020 09:24:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E5=BE=97=E5=88=B0=E7=9F=A5?= =?UTF-8?q?=E8=AF=86=E5=9F=8E=E9=82=A6=20(#6118)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 6 +++ lib/router.js | 3 ++ lib/routes/dedao/knowledge.js | 78 +++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 lib/routes/dedao/knowledge.js diff --git a/docs/new-media.md b/docs/new-media.md index 43cbac6c1..1a6db7f44 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -631,6 +631,12 @@ Supported sub-sites: +## 得到 + +### 知识城邦 + + + ## 电商报 ### 分区 diff --git a/lib/router.js b/lib/router.js index 384c7d81c..b19d5586b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3468,6 +3468,9 @@ router.get('/grandchallenge/challenges', require('./routes/grandchallenge/challe // 西北工业大学 router.get('/nwpu/:column', require('./routes/nwpu/index')); +// 得到 +router.get('/dedao/knowledge/:topic?/:type?', require('./routes/dedao/knowledge')); + // 未名新闻 router.get('/mitbbs/:caty?', require('./routes/mitbbs/index')); diff --git a/lib/routes/dedao/knowledge.js b/lib/routes/dedao/knowledge.js new file mode 100644 index 000000000..06fdfb9f6 --- /dev/null +++ b/lib/routes/dedao/knowledge.js @@ -0,0 +1,78 @@ +const got = require('@/utils/got'); +const date = require('@/utils/date'); + +module.exports = async (ctx) => { + const topic = ctx.params.topic || ''; + const type = ctx.params.type || 'true'; + const rootUrl = 'https://www.dedao.cn'; + const detailUrl = `${rootUrl}/pc/ledgers/topic/detail`; + const currentUrl = `${rootUrl}/knowledge${topic === '' ? '' : '/topic/' + topic}`; + const apiUrl = `${rootUrl}/pc/ledgers/${topic === '' ? 'notes/friends_timeline' : 'topic/notes/list'}`; + + let title = '', + description = ''; + + if (topic !== '') { + const detailResponse = await got({ + method: 'post', + url: detailUrl, + json: { + incr_view_count: false, + topic_id_hazy: topic, + }, + }); + + title = detailResponse.data.c.name; + description = detailResponse.data.c.intro; + } + + const response = await got({ + method: 'post', + url: apiUrl, + json: { + count: 20, + load_chain: true, + is_elected: type === 'true', + page_id: 0, + topic_id_hazy: topic, + version: 2, + }, + }); + + const items = (topic === '' ? response.data.c.notes : response.data.c.note_detail_list).map((item) => { + let s_part = ''; + if (item.s_part) { + const s_author = `

引用 ${item.s_part.nick_name} (${item.s_part.v_info}):

`; + const s_content = `

${item.s_part.note.replace(/\n\n/g, '

')}

[查看原文]`; + let s_images = '
'; + if (item.s_part.images) { + for (const i of item.s_part.images) { + s_images += ``; + } + } + s_part = s_author + s_content + s_images; + } + + let f_images = '
'; + if (item.f_part.images) { + for (const i of item.f_part.images) { + f_images += ``; + } + } + + return { + title: item.f_part.note, + author: item.f_part.nick_name, + description: `

${item.f_part.note.replace(/\n\n/g, '

')}

${f_images}
${s_part}`, + link: `${rootUrl}/knowledge/note/${item.f_part.note_id_hazy}`, + pubDate: new Date(date(item.f_part.time_desc)).toUTCString(), + }; + }); + + ctx.state.data = { + title: `得到 - 知识城邦${title === '' ? '' : ' - ' + title}`, + link: currentUrl, + item: items, + description, + }; +};