Skip to content

Commit

Permalink
feat(route): add 51cto (#14958)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
cnkmmk and pull[bot] authored Apr 6, 2024
1 parent 4296911 commit cb69f50
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/routes/51cto/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: '51CTO',
url: '51cto.com',
};
52 changes: 52 additions & 0 deletions lib/routes/51cto/recommend.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions lib/routes/51cto/utils.ts
Original file line number Diff line number Diff line change
@@ -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));
};

0 comments on commit cb69f50

Please sign in to comment.