Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): pubscholar #15788

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lib/routes/pubscholar/explore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { baseUrl, uuidv4, getArticleLink, getSignedHeaders } from './utils';
import md5 from '@/utils/md5';
import { Resource } from './types';
import sanitizeHtml from 'sanitize-html';

export const route: Route = {
path: '/explore/:category?/:keyword?',
name: 'Explore',
maintainers: ['TonyRL'],
example: '/pubscholar/explore',
parameters: {
category: 'Category, see the table below, `articles` by default',
keyword: 'Search Keyword',
},
handler,
description: `| Articles / 论文 | Patents / 专利 | Reports / 领域快报 | Information / 动态快讯 | Datasets / 科学数据 | Books / 图书 |
| --------------- | -------------- | ------------------ | ---------------------- | ------------------- | ------------ |
| articles | patents | bulletins | reports | sciencedata | books |`,
};

async function handler(ctx) {
const { category = 'articles', keyword } = ctx.req.param();
const uuid = uuidv4();

const response = await ofetch<Resource>(`${baseUrl}/hky/open/resources/api/v1/${category}`, {
method: 'POST',
headers: {
...getSignedHeaders(),
Cookie: `XSRF-TOKEN=${uuid}`,
'X-XSRF-TOKEN': uuid,
},
body: {
page: 1,
size: 10,
order_field: 'date',
order_direction: 'desc',
user_id: md5(Date.now().toString()),
lang: 'zh',
query: keyword,
strategy: null,
orderField: 'default',
},
});

const list = response.content.map((item) => ({
title: (item.is_free || item.links.some((l) => l.is_open_access) ? '「Open Access」' : '') + sanitizeHtml(item.title, { allowedTags: [], allowedAttributes: {} }),
description: item.abstracts + `<br>${item.links.map((link) => `<a href="${link.url}">${link.is_open_access ? '「Open Access」' : ''}${link.name}</a>`).join('<br>')}`,
author: item.author.join('; '),
pubDate: parseDate(item.date),
category: item.keywords.map((keyword) => sanitizeHtml(keyword, { allowedTags: [], allowedAttributes: {} })),
link: `${baseUrl}/${category}/${getArticleLink(item.id)}`,
}));

return {
title: 'PubScholar 公益学术平台',
link: `${baseUrl}/explore`,
item: list,
};
}
7 changes: 7 additions & 0 deletions lib/routes/pubscholar/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'PubScholar 公益学术平台',
url: 'pubscholar.cn',
categories: ['journal'],
};
41 changes: 41 additions & 0 deletions lib/routes/pubscholar/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
interface Link {
is_open_access: boolean;
name: string;
url: string;
}

interface Content {
date: string;
attachments: any[];
keywords: string[];
year: number;
source: string;
title: string;
type: string;
abstracts_abbreviation: string;
major: string;
school: any[];
first_page: string;
local_links: any[];
links: Link[];
id: string;
graduation_institution: any[];
cn_type: string;
article_type: string;
issue: string;
abstracts: string;
author: string[];
last_page: string;
degree: string;
tutor: any[];
semantic_entities: object;
volume: string;
source_list: string[];
is_free: boolean;
}

export interface Resource {
total: number;
is_last: boolean;
content: Content[];
}
44 changes: 44 additions & 0 deletions lib/routes/pubscholar/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import crypto from 'node:crypto';
import CryptoJS from 'crypto-js';

const salt = '6m6pingbinwaktg227gngifoocrfbo95';
const key = CryptoJS.enc.Utf8.parse('eRtYuIoPaSdFgHqW');
const iv = CryptoJS.enc.Utf8.parse('Nmc09JkLzX8765Vb');

export const baseUrl = 'https://pubscholar.cn';
export const sha1 = (str: string) => crypto.createHash('sha1').update(str).digest('hex');
export const uuidv4 = () => crypto.randomUUID();

const generateNonce = (length: number): string => {
if (!length) {
return null;
}

let nonce = '';
while (nonce.length < length) {
const randomString = Math.random().toString(36).slice(2).toUpperCase();
nonce += randomString;
}

return nonce.slice(0, length);
};

export const getSignedHeaders = () => {
const nonce = generateNonce(6);
const timestamp = Date.now().toString();
const signature = sha1([salt, timestamp, nonce].sort().join(''));
return {
nonce,
timestamp,
signature,
};
};

export const getArticleLink = (id: string) => {
const ciphertext = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(id), key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
}).ciphertext.toString();
return ciphertext;
};