-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |