From c454200876f90214c2237b87817c7f7ec64d86cf Mon Sep 17 00:00:00 2001 From: LyleLee Date: Fri, 5 Apr 2024 12:07:11 -0400 Subject: [PATCH] =?UTF-8?q?feat(route):=20Add=20=E5=9B=BD=E5=AE=B6?= =?UTF-8?q?=E5=93=B2=E5=AD=A6=E7=A4=BE=E4=BC=9A=E7=A7=91=E5=AD=A6=E6=96=87?= =?UTF-8?q?=E7=8C=AE=E4=B8=AD=E5=BF=83=20(#14737)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route) Add 国家哲学社会科学文献中心 * Adapt to new namespace * Update lib/routes/ncpssd/newlist.ts Co-authored-by: Tony * Update lib/routes/ncpssd/newlist.ts Co-authored-by: Tony * Update lib/routes/ncpssd/newlist.ts Co-authored-by: Tony * Update lib/routes/ncpssd/newlist.ts Co-authored-by: Tony * Update lib/routes/ncpssd/newlist.ts Co-authored-by: Tony * fix: cr --------- --- lib/routes/ncpssd/namespace.ts | 6 +++ lib/routes/ncpssd/newlist.ts | 98 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 lib/routes/ncpssd/namespace.ts create mode 100644 lib/routes/ncpssd/newlist.ts diff --git a/lib/routes/ncpssd/namespace.ts b/lib/routes/ncpssd/namespace.ts new file mode 100644 index 000000000..317113c18 --- /dev/null +++ b/lib/routes/ncpssd/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '国家哲学社会科学文献中心', + url: 'ncpssd.cn', +}; diff --git a/lib/routes/ncpssd/newlist.ts b/lib/routes/ncpssd/newlist.ts new file mode 100644 index 000000000..f90d80c18 --- /dev/null +++ b/lib/routes/ncpssd/newlist.ts @@ -0,0 +1,98 @@ +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import { Route } from '@/types'; + +export const route: Route = { + path: '/newlist', + categories: ['study'], + example: '/ncpssd/newlist', + radar: [ + { + source: ['ncpssd.cn/', 'ncpssd.cn/newlist'], + }, + ], + name: '最新文献', + maintainers: ['LyleLee'], + handler, + url: 'ncpssd.cn/', +}; + +async function handler() { + const baseUrl = 'https://www.ncpssd.cn'; + const argument = '/newlist?type=0'; + + const response = await got({ + method: 'get', + url: baseUrl + argument, + }); + + const data = response.data; + const $ = load(data); + const items = $('.news-list > li'); + + const list = items.toArray().map((p) => { + const title = $(p) + .find('a') + .text() + .replaceAll(/(\r\n|\n|\r)/gm, '') + .trim(); + const articleUrl = + baseUrl + + $(p) + .find('a') + .attr('onclick') + ?.match(/\('(.*?)'\)/)?.[1]; + const parseUrl = new URL(articleUrl); + + return { + title, + link: articleUrl, + lngid: parseUrl.searchParams.get('id'), + type: parseUrl.searchParams.get('typename'), + pageType: parseUrl.searchParams.get('nav'), + }; + }); + + const paper = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const url = 'https://www.ncpssd.cn/articleinfoHandler/getjournalarticletable'; // Adjust the URL accordingly + const headers = { + Accept: 'application/json, text/javascript, */*; q=0.01', + 'Content-Type': 'application/json; charset=UTF-8', + }; + + const requestBody = { + lngid: item.lngid, + type: item.type, + pageType: item.pageType, + }; + + const response = await got.post(url, { + headers, + json: requestBody, + responseType: 'json', // Set the expected response type + }); + + return { + title: item.title, + link: item.link, + author: response.data.data.showwriter, + description: response.data.data.remarkc, + pubDate: parseDate(response.data.data.publishDateTime), + }; + }) + ) + ); + + return { + // 源标题 + title: '国家哲学社会科学文献中心', + // 源链接 + link: baseUrl + argument, + // 源文章 + item: paper, + }; +}