feat(route): add 51cto (#14958)

* feat(route): add 51cto

增加了51cto的推荐列表

* Update rss.ts

* Update rss.ts

* fix(route): fix 51cto

* Update recommend.ts

调整 51cto,使用API来生成RSS。

* Update recommend.ts

* fix: request signing

---------

Co-authored-by: pull[bot] <39814207+pull[bot]@users.noreply.github.com>
This commit is contained in:
cnkmmk 2024-04-07 06:23:13 +08:00 committed by GitHub
parent 4296911775
commit cb69f5032c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '51CTO',
url: '51cto.com',
};

View File

@ -0,0 +1,52 @@
import { Route } from '@/types';
import { parseDate } from '@/utils/parse-date';
import got from '@/utils/got';
import { getToken, sign } from './utils';
export const route: Route = {
path: '/index/recommend',
categories: ['programming'],
example: '/51cto/index/recommend',
radar: [
{
source: ['51cto.com/'],
},
],
name: '推荐',
maintainers: ['cnkmmk'],
handler,
url: '51cto.com/',
};
async function handler(ctx) {
const url = 'https://api-media.51cto.com';
const requestPath = 'index/index/recommend';
const token = (await getToken()) as string;
const timestamp = Date.now();
const params = {
page: 1,
page_size: ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 50,
limit_time: 0,
name_en: '',
};
const response = await got(`${url}/${requestPath}`, {
searchParams: {
...params,
timestamp,
token,
sign: sign(requestPath, params, timestamp, token),
},
});
const list = response.data.data.data.list;
return {
title: '51CTO',
link: 'https://www.51cto.com/',
description: '51cto - 推荐',
item: list.map((item) => ({
title: item.title,
link: item.url,
pubDate: parseDate(item.pubdate, +8),
description: item.abstract,
})),
};
}

21
lib/routes/51cto/utils.ts Normal file
View File

@ -0,0 +1,21 @@
import ofetch from '@/utils/ofetch';
import cache from '@/utils/cache';
import md5 from '@/utils/md5';
export const getToken = () =>
cache.tryGet(
'51cto:token',
async () => {
const response = await ofetch('https://api-media.51cto.com/api/token-get');
return response.data.data.token;
},
3600,
false
);
export const sign = (requestPath: string, payload: Record<string, any> = {}, timestamp: number, token: string) => {
payload.timestamp = timestamp;
payload.token = token;
const sortedParams = Object.keys(payload).sort();
return md5(md5(requestPath) + md5(sortedParams + md5(token) + timestamp));
};