From 63e78432be53dc375cdb39206d77aea7510d96c4 Mon Sep 17 00:00:00 2001 From: Fatpandac <1779196284@qq.com> Date: Sat, 22 Jan 2022 23:39:01 +0800 Subject: [PATCH] Add(route): add cnki (#8877) --- docs/journal.md | 10 +++++ lib/v2/cnki/debut.js | 62 ++++++++++++++++++++++++++++ lib/v2/cnki/journals.js | 74 ++++++++++++++++++++++++++++++++++ lib/v2/cnki/maintainer.js | 4 ++ lib/v2/cnki/radar.js | 19 +++++++++ lib/v2/cnki/router.js | 4 ++ lib/v2/cnki/templates/desc.art | 5 +++ 7 files changed, 178 insertions(+) create mode 100644 lib/v2/cnki/debut.js create mode 100644 lib/v2/cnki/journals.js create mode 100644 lib/v2/cnki/maintainer.js create mode 100644 lib/v2/cnki/radar.js create mode 100644 lib/v2/cnki/router.js create mode 100644 lib/v2/cnki/templates/desc.art diff --git a/docs/journal.md b/docs/journal.md index c66e22a5ba..6da4013203 100644 --- a/docs/journal.md +++ b/docs/journal.md @@ -306,3 +306,13 @@ _仅支持 Science 主刊_ | bqym | xshd | tzgg | + +## 中国知网 + +### 期刊 + + + +### 网络首发 + + diff --git a/lib/v2/cnki/debut.js b/lib/v2/cnki/debut.js new file mode 100644 index 0000000000..2ac3863a75 --- /dev/null +++ b/lib/v2/cnki/debut.js @@ -0,0 +1,62 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +const rootUrl = 'https://chn.oversea.cnki.net'; + +module.exports = async (ctx) => { + const { name } = ctx.params; + + const journalUrl = `${rootUrl}/knavi/JournalDetail?pcode=CjFD&pykm=${name}`; + const title = await got.get(journalUrl).then((res) => cheerio.load(res.data)('head > title').text()); + + const outlineUrl = `${rootUrl}/knavi/JournalDetail/GetnfAllOutline`; + const response = await got({ + method: 'post', + url: outlineUrl, + form: { + pageIdx: '0', + type: '2', + pcode: 'CJFD', + pykm: name, + }, + }); + const $ = cheerio.load(response.data); + const list = $('dd') + .map((_, item) => ({ + title: $(item).find('span.name > a').text().trim(), + link: `${rootUrl}/kcms/detail/${new URLSearchParams(new URL(`${rootUrl}/${$(item).find('span.name > a').attr('href')}`).search).get('url')}.html`, + pubDate: parseDate($(item).find('span.company').text(), 'YYYY-MM-DD HH:mm:ss'), + })) + .get(); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got.get(item.link); + const $ = cheerio.load(detailResponse.data); + item.description = art(path.join(__dirname, 'templates/desc.art'), { + author: $('h3.author > span') + .map((_, item) => $(item).text()) + .get() + .join(' '), + company: $('a.author') + .map((_, item) => $(item).text()) + .get() + .join(' '), + content: $('div.row > span.abstract-text').parent().text(), + }); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${title} - 全网首发`, + link: `https://navi.cnki.net/knavi/journals/${name}/detail`, + item: items, + }; +}; diff --git a/lib/v2/cnki/journals.js b/lib/v2/cnki/journals.js new file mode 100644 index 0000000000..eca97d2d60 --- /dev/null +++ b/lib/v2/cnki/journals.js @@ -0,0 +1,74 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +const rootUrl = 'https://chn.oversea.cnki.net'; + +module.exports = async (ctx) => { + const { name } = ctx.params; + const journalUrl = `${rootUrl}/knavi/JournalDetail?pcode=CjFD&pykm=${name}`; + const title = await got.get(journalUrl).then((res) => cheerio.load(res.data)('head > title').text()); + + const yaerListUrl = `${rootUrl}/knavi/JournalDetail/GetJournalYearList?pcode=CJFD&pykm=${name}&pIdx=0`; + const journalsInfo = await got.get(yaerListUrl).then((res) => { + const $ = cheerio.load(res.data); + const date = $('dd').find('a').first().attr('id').replace('yq', ''); + const year = date.substring(0, 4); + const issue = date.substring(4); + + return { + year, + issue, + date, + }; + }); + + const papersUrl = `${rootUrl}/knavi/JournalDetail/GetArticleList?year=${journalsInfo.year}&issue=${journalsInfo.issue}&pykm=${name}&pageIdx=0&pcode=CjFD`; + const list = await got.get(papersUrl).then((res) => { + const $ = cheerio.load(res.data); + const item = $('span.name') + .map((_, item) => { + const url = new URL(`${rootUrl}/${$(item).find('a').attr('href')}`); + const urlParams = new URLSearchParams(url.search); + + return { + title: $(item).find('a').text().trim(), + link: `${rootUrl}/kcms/detail/detail.aspx?dbcode=${urlParams.get('dbCode')}&filename=${urlParams.get('filename')}`, + pubDate: parseDate(journalsInfo.date, 'YYYYMM'), + }; + }) + .get(); + + return item; + }); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got.get(item.link); + const $ = cheerio.load(detailResponse.data); + item.description = art(path.join(__dirname, 'templates/desc.art'), { + author: $('h3.author > span') + .map((_, item) => $(item).text()) + .get() + .join(' '), + company: $('a.author') + .map((_, item) => $(item).text()) + .get() + .join(' '), + content: $('div.row > span.abstract-text').parent().text(), + }); + + return item; + }) + ) + ); + + ctx.state.data = { + title: String(title), + link: `https://navi.cnki.net/knavi/journals/${name}/detail`, + item: items, + }; +}; diff --git a/lib/v2/cnki/maintainer.js b/lib/v2/cnki/maintainer.js new file mode 100644 index 0000000000..952d141e47 --- /dev/null +++ b/lib/v2/cnki/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/journals/:name': ['Fatpandac'], + '/journals/debut/:name': ['Fatpandac'], +}; diff --git a/lib/v2/cnki/radar.js b/lib/v2/cnki/radar.js new file mode 100644 index 0000000000..ed759578bb --- /dev/null +++ b/lib/v2/cnki/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'cnki.net': { + _name: '中国知网', + navi: [ + { + title: '期刊', + docs: 'https://docs.rsshub.app/journal.html#zhong-guo-zhi-wang-qi-kan', + source: ['/knavi/journals/:name/detail'], + target: '/cnki/journals/:name', + }, + { + title: '网络首发', + docs: 'https://docs.rsshub.app/journal.html#zhong-guo-zhi-wang-wang-luo-shou-fa', + source: ['/knavi/journals/:name/detail'], + target: '/cnki/journals/debut/:name', + }, + ], + }, +}; diff --git a/lib/v2/cnki/router.js b/lib/v2/cnki/router.js new file mode 100644 index 0000000000..8248af6217 --- /dev/null +++ b/lib/v2/cnki/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/journals/:name', require('./journals')); + router.get('/journals/debut/:name', require('./debut')); +}; diff --git a/lib/v2/cnki/templates/desc.art b/lib/v2/cnki/templates/desc.art new file mode 100644 index 0000000000..5a539b1739 --- /dev/null +++ b/lib/v2/cnki/templates/desc.art @@ -0,0 +1,5 @@ +作者:{{ author }} +
+单位:{{ company }} +
+{{ content }}