Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/routes/opengithub-trending/namespace.ts
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use namespace itc instead.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'opengithub-trending',
url: 'github.com/OpenGithubs/github-weekly-rank/',
description: `OpenGithubs Weekly 是一个开源项目,
旨在每周分享 GitHub 上最受欢迎的开源项目。
`,
lang: 'zh-CN',
zh: {
name: 'github每周趋势',
},
};
87 changes: 87 additions & 0 deletions lib/routes/opengithub-trending/weekly-rank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { Route } from '@/types';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/weekly',
categories: ['programming'],
example: '/opengithub-trending/weekly',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['github.com/OpenGithubs/github-weekly-rank'],
target: '/opengithub-trending/weekly',
},
],
name: 'Weekly Rank',
maintainers: ['Nemocccc'],
description: '追踪 OpenGithubs 每周发布的开源项目排行榜',
handler,
};

async function handler() {
const repoUrl = 'https://api.github.com/repos/OpenGithubs/github-weekly-rank/contents';

const yearResponse = await got(repoUrl);
const years = yearResponse.data
.filter((item: any) => item.type === 'dir' && /^\d{4}$/.test(item.name))
.sort((a: any, b: any) => b.name.localeCompare(a.name))

Check warning

Code scanning / ESLint

Prefer `Array#toSorted()` over `Array#sort()`.

Use `Array#toSorted()` instead of `Array#sort()`.
Comment thread Fixed
.slice(0, 2);

const monthPromises = years.map((yearItem: any) =>
got(repoUrl + '/' + yearItem.name).then((monthResponse) => ({
Comment thread Fixed
year: yearItem.name,
months: monthResponse.data
.filter((item: any) => item.type === 'dir')
.sort((a: any, b: any) => b.name.localeCompare(a.name))

Check warning

Code scanning / ESLint

Prefer `Array#toSorted()` over `Array#sort()`.

Use `Array#toSorted()` instead of `Array#sort()`.
Comment thread Fixed
.slice(0, 3),
}))
);

const monthData = await Promise.all(monthPromises);

const dayPromises = monthData.flatMap(({ year, months }: any) =>
months.map((monthItem: any) =>
got(repoUrl + '/' + year + '/' + monthItem.name).then((dayResponse) => ({
Comment thread Fixed
year,
month: monthItem.name,
days: dayResponse.data
.filter((item: any) => item.type === 'file' && item.name.endsWith('.md'))
.sort((a: any, b: any) => b.name.localeCompare(a.name))

Check warning

Code scanning / ESLint

Prefer `Array#toSorted()` over `Array#sort()`.

Use `Array#toSorted()` instead of `Array#sort()`.
Comment thread Fixed
.slice(0, 5),
}))
)
);

const allDayData = await Promise.all(dayPromises);

const filePromises = allDayData.flatMap(({ year, month, days }: any) =>
days.map((dayItem: any) => {
const fileName = dayItem.name.replace('.md', '');
const fileUrl = 'https://raw.githubusercontent.com/OpenGithubs/github-weekly-rank/main/' + year + '/' + month + '/' + dayItem.name;
return got(fileUrl).then((fileContent) => ({
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an option to accept GitHub token for a higher API limit.

Comment thread Fixed
title: 'GitHub Weekly Rank - ' + fileName,
description: fileContent.data,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use markdown-it to render the markdown content.

link: 'https://github.com/OpenGithubs/github-weekly-rank/blob/main/' + year + '/' + month + '/' + fileName + '.md',
pubDate: parseDate(year + '-' + month + '-' + fileName.slice(6)),
}));
})
);

const items = (await Promise.all(filePromises)).slice(0, 20);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are more than 20 promises, they will still be resolved, but they will not be returned, which simply wastes time and resources.


return {
title: 'OpenGithubs Weekly Rank',
link: 'https://github.com/OpenGithubs/github-weekly-rank',
description: 'OpenGithubs 每周开源项目排行榜',
item: items,
};
}
Loading