Skip to content

Commit

Permalink
feat(route): jingzhengu (#15709)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored May 25, 2024
1 parent 690b22f commit 4cea8ac
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/routes/jingzhengu/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '精真估',
url: 'www.jingzhengu.com',
};
77 changes: 77 additions & 0 deletions lib/routes/jingzhengu/news.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> = new Map([
['pageNo', 1],
['middleware', String(Date.now())],
]);
const response = await ofetch<NewsInfo>(`${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<string, any> = new Map([
['id', item.id],
['middleware', String(Date.now())],
]);

const response = await ofetch<NewsDetail>(`${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,
};
}
39 changes: 39 additions & 0 deletions lib/routes/jingzhengu/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions lib/routes/jingzhengu/utils.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>) {
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);
}

0 comments on commit 4cea8ac

Please sign in to comment.