diff --git a/lib/routes/jingzhengu/namespace.ts b/lib/routes/jingzhengu/namespace.ts new file mode 100644 index 00000000000000..f44e2f59fd4756 --- /dev/null +++ b/lib/routes/jingzhengu/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '精真估', + url: 'www.jingzhengu.com', +}; diff --git a/lib/routes/jingzhengu/news.ts b/lib/routes/jingzhengu/news.ts new file mode 100644 index 00000000000000..6f434f9cecc8b4 --- /dev/null +++ b/lib/routes/jingzhengu/news.ts @@ -0,0 +1,77 @@ +import { Route } from '@/types'; +import { NewsInfo, NewsDetail } from './types'; + +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; +import { sign } from './utils'; + +export const route: Route = { + path: '/news', + categories: ['other'], + example: '/jingzhengu/news', + radar: [ + { + source: ['www.jingzhengu.com'], + }, + ], + name: '资讯', + maintainers: ['TonyRL'], + handler, + url: 'www.jingzhengu.com', +}; + +async function handler() { + const baseUrl = 'https://www.jingzhengu.com'; + + const payload: Map = new Map([ + ['pageNo', 1], + ['middleware', String(Date.now())], + ]); + const response = await ofetch(`${baseUrl}/news/makeNewsInfo`, { + method: 'POST', + body: { + ...Object.fromEntries(payload), + sign: sign(payload), + }, + }); + + const list = response.data.articles.map((item) => ({ + title: item.title, + description: item.summary, + link: `${baseUrl}/#/cn/Details_${item.addDate.split(' ')[0].replaceAll('-', '')}${item.id}.html`, + pubDate: timezone(parseDate(item.addDate, 'YYYY-MM-DD HH:mm:ss'), 8), + author: item.author, + id: item.id, + })); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const payload: Map = new Map([ + ['id', item.id], + ['middleware', String(Date.now())], + ]); + + const response = await ofetch(`${baseUrl}/news/makeNewsDetail`, { + method: 'POST', + body: { + ...Object.fromEntries(payload), + sign: sign(payload), + }, + }); + + item.description = response.data.content; + + return item; + }) + ) + ); + + return { + title: '精真估 > 资讯', + link: `${baseUrl}/#/index/boot`, + item: items, + }; +} diff --git a/lib/routes/jingzhengu/types.ts b/lib/routes/jingzhengu/types.ts new file mode 100644 index 00000000000000..1958a20a0a7e2b --- /dev/null +++ b/lib/routes/jingzhengu/types.ts @@ -0,0 +1,39 @@ +interface Article { + addDate: string; + author: string; + channelId: number; + content: string; + externalUrl: string; + firstPage: string; + fromArticleId: number; + fromChannelId: number; + id: number; + img2: string; + imgUrl: string; + isImg: number; + summary: string; + title: string; + viewNum: number; +} + +interface pageInf { + currentPage: number; + pageCount: number; + pageSize: number; + pageTotal: number; +} + +export interface NewsInfo { + code: number; + data: { + articles: Article[]; + pageInf: pageInf; + }; + msg: string; +} + +export interface NewsDetail { + code: number; + data: Article; + msg: string; +} diff --git a/lib/routes/jingzhengu/utils.ts b/lib/routes/jingzhengu/utils.ts new file mode 100644 index 00000000000000..242826a9cdd344 --- /dev/null +++ b/lib/routes/jingzhengu/utils.ts @@ -0,0 +1,35 @@ +import md5 from '@/utils/md5'; + +function link(str: string, ...args: string[]): string { + let result = args.map((arg) => arg + str).join(''); + if (result.search('-')) { + result = result.substring(0, result.length - 1); + } + return result; +} + +function replaceCharAt(str: string, index: number, replacement: string) { + return index < 0 || index >= str.length ? str : str.slice(0, index) + replacement + str.slice(index + 1); +} + +export function sign(payload: Map) { + const map = new Map(); + const lowerCaseKeys: string[] = []; + + for (const [key, value] of payload.entries()) { + const lowerCaseKey = key.toLowerCase(); + lowerCaseKeys.push(lowerCaseKey); + map.set(lowerCaseKey, typeof value === 'string' ? value.toLowerCase() : value); + } + + const sortedString = lowerCaseKeys + .sort() + .map((key) => key + '=' + map.get(key)) + .join(''); + const linkedString = link('--'.substring(0, 1), '#CEAIWER', '892F', 'KB97', 'JKB6', 'HJ7OC7C8', 'GJZG'); + const lastSeparatorIndex = linkedString.lastIndexOf('--'.substring(0, 1)); // 32 + const replacedString = replaceCharAt(linkedString, lastSeparatorIndex, ''); // #CEAIWER-892F-KB97-JKB6-HJ7OC7C8GJZG + const finalString = (sortedString + replacedString).toLowerCase(); + + return md5(finalString); +}