Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions lib/routes/opengithub-trending/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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每周趋势',
},
};
105 changes: 105 additions & 0 deletions lib/routes/opengithub-trending/weekly-rank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import MarkdownIt from 'markdown-it';

import { config } from '@/config';
import type { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/weekly-rank',
categories: ['programming'],
example: '/opengithub-trending/weekly-rank',
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-rank',
},
],
name: 'Weekly Rank',
maintainers: ['Nemocccc'],
description: '追踪 OpenGithubs 每周发布的开源项目排行榜',
handler,
};

async function handler() {
const repoUrl = 'https://api.github.com/repos/OpenGithubs/github-weekly-rank/contents';
const headers: Record<string, string> = {};

if (config.github?.accessToken) {
headers.Authorization = `token ${config.github.accessToken}`;
}

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

const getMonthData = async (yearItem: any) => {
const monthResponse = await got(`${repoUrl}/${yearItem.name}`, { headers });
return {
year: yearItem.name,
months: monthResponse.data
.filter((item: any) => item.type === 'dir')
.toSorted((a: any, b: any) => b.name.localeCompare(a.name))
.slice(0, 3),
};
};

const monthData = await Promise.all(years.map((year) => getMonthData(year)));

const getDayData = async (year: string, monthItem: any) => {
const dayResponse = await got(`${repoUrl}/${year}/${monthItem.name}`, { headers });
return {
year,
month: monthItem.name,
days: dayResponse.data
.filter((item: any) => item.type === 'file' && item.name.endsWith('.md'))
.toSorted((a: any, b: any) => b.name.localeCompare(a.name))
.slice(0, 5),
};
};

const dayPromises = monthData.flatMap(({ year, months }: any) => months.map((monthItem: any) => getDayData(year, monthItem)));

const allDayData = await Promise.all(dayPromises);

const md = new MarkdownIt();

const getFileItem = async (year: string, month: string, dayItem: any) => {
const fileName = dayItem.name.replace('.md', '');
const key = `opengithub-trending:${year}/${month}/${dayItem.name}`;
const fileContent = await cache.tryGet(key, async () => {
const response = await got(`https://raw.githubusercontent.com/OpenGithubs/github-weekly-rank/main/${year}/${month}/${dayItem.name}`);
return response.data;
});

return {
title: `GitHub Weekly Rank - ${fileName}`,
description: md.render(fileContent),
link: `https://github.com/OpenGithubs/github-weekly-rank/blob/main/${year}/${month}/${fileName}.md`,
pubDate: parseDate(`${year}-${month}-${fileName.slice(6)}`),
};
};

const filePromises = allDayData.flatMap(({ year, month, days }: any) => days.map((dayItem: any) => getFileItem(year, month, dayItem))).slice(0, 20);

const items = await Promise.all(filePromises);

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