From 607a9455e2d58f69d3cc8b65955cc713b77f7c14 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 26 Mar 2024 02:36:27 +0800 Subject: [PATCH] fix(route): github user starred repos use reset api (#14960) --- lib/routes/github/star.ts | 12 ++--- lib/routes/github/starred-repos.ts | 76 ++++++++++-------------------- 2 files changed, 31 insertions(+), 57 deletions(-) diff --git a/lib/routes/github/star.ts b/lib/routes/github/star.ts index 9e7b892c66..309f513906 100644 --- a/lib/routes/github/star.ts +++ b/lib/routes/github/star.ts @@ -8,12 +8,12 @@ export const route: Route = { example: '/github/stars/DIYGod/RSSHub', parameters: { user: 'GitHub username', repo: 'GitHub repo name' }, features: { - requireConfig: false, - requirePuppeteer: false, - antiCrawler: false, - supportBT: false, - supportPodcast: false, - supportScihub: false, + requireConfig: [ + { + name: 'GITHUB_ACCESS_TOKEN', + description: 'GitHub Access Token', + }, + ], }, radar: [ { diff --git a/lib/routes/github/starred-repos.ts b/lib/routes/github/starred-repos.ts index 9324f1bbe3..ce56d6abe5 100644 --- a/lib/routes/github/starred-repos.ts +++ b/lib/routes/github/starred-repos.ts @@ -1,6 +1,8 @@ import { Route } from '@/types'; import got from '@/utils/got'; import { config } from '@/config'; +import md5 from '@/utils/md5'; +import { parseDate } from '@/utils/parse-date'; export const route: Route = { path: '/starred_repos/:user', @@ -8,12 +10,13 @@ export const route: Route = { example: '/github/starred_repos/DIYgod', parameters: { user: 'User name' }, features: { - requireConfig: false, - requirePuppeteer: false, - antiCrawler: false, - supportBT: false, - supportPodcast: false, - supportScihub: false, + requireConfig: [ + { + name: 'GITHUB_ACCESS_TOKEN', + optional: true, + description: 'To get more requests', + }, + ], }, radar: [ { @@ -26,62 +29,33 @@ export const route: Route = { }; async function handler(ctx) { - if (!config.github || !config.github.access_token) { - throw new Error('GitHub star RSS is disabled due to the lack of relevant config'); - } const user = ctx.req.param('user'); const host = `https://github.com/${user}?tab=stars`; - const url = 'https://api.github.com/graphql'; - const response = await got({ - method: 'post', - url, + const { data: response } = await got(`https://api.github.com/users/${user}/starred`, { headers: { - Authorization: `bearer ${config.github.access_token}`, - }, - json: { - query: ` - { - user(login: "${user}") { - starredRepositories(first: 10, orderBy: {direction: DESC, field: STARRED_AT}) { - edges { - starredAt - node { - name - description - url - openGraphImageUrl - primaryLanguage { - name - } - stargazers { - totalCount - } - } - } - } - } - } - `, + Accept: 'application/vnd.github.star+json', + Authorization: config.github?.access_token ? `Bearer ${config.github.access_token}` : undefined, }, }); - const data = response.data.data.user.starredRepositories.edges; + const data = response.map((repo) => ({ + title: `${user} starred ${repo.name}`, + author: user, + description: `${repo.description ?? 'No Description'}
+ Primary Language: ${repo.language ?? 'Primary Language'}
+ Stargazers: ${repo.stargazers_count}
+ `, + pubDate: parseDate(repo.starred_at), + link: repo.html_url, + })); return { allowEmpty: true, - title: `${user}’s starred repositories`, + title: `${user}'s starred repositories`, link: host, - description: `${user}’s starred repositories`, - item: data.map((repo) => ({ - title: `${user} starred ${repo.node.name}`, - author: user, - description: `${repo.node.description === null ? 'no description' : repo.node.description}
primary language: ${ - repo.node.primaryLanguage === null ? 'no primary language' : repo.node.primaryLanguage.name - }
stargazers: ${repo.node.stargazers.totalCount}
`, - pubDate: new Date(repo.starredAt), - link: repo.node.url, - })), + description: `${user}'s starred repositories`, + item: data, }; }