feat(route): add Gitstar Ranking (#19850)

* feat(route): add Gitstar Ranking

* Apply suggestions from code review

---------
This commit is contained in:
Ethan Shen
2025-08-16 07:56:04 +08:00
committed by GitHub
parent 1295392fe7
commit f45039f70e
3 changed files with 197 additions and 0 deletions
+150
View File
@@ -0,0 +1,150 @@
import { type Data, type DataItem, type Route, ViewType } from '@/types';
import { art } from '@/utils/render';
import ofetch from '@/utils/ofetch';
import { type CheerioAPI, type Cheerio, load } from 'cheerio';
import type { Element } from 'domhandler';
import { type Context } from 'hono';
import path from 'node:path';
export const handler = async (ctx: Context): Promise<Data> => {
const { category = 'repositories' } = ctx.req.param();
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '100', 10);
const baseUrl: string = 'https://gitstar-ranking.com';
const targetUrl: string = new URL(category, baseUrl).href;
const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language = $('html').attr('lang') ?? 'en';
const items: DataItem[] = $('a.list-group-item')
.slice(0, limit)
.toArray()
.map((el): Element => {
const $el: Cheerio<Element> = $(el);
const stargazersCount: number = Number($el.find('span.stargazers_count').text()?.trim());
const title: string = $el.find('span.hidden-xs').text()?.trim();
const image: string | undefined = $el.find('img.avatar_image_big').attr('src');
const language: string | undefined = $el.find('div.repo-language').text()?.trim();
const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), {
images: image
? [
{
src: image,
alt: title,
},
]
: undefined,
stargazersCount,
language,
description: $el.find('div.repo-description').text()?.trim(),
});
const linkUrl: string | undefined = $el.attr('href');
const categories: string[] = language ? [language] : [];
const guid: string = `gitstar-ranking-${title}-${stargazersCount}`;
const processedItem: DataItem = {
title: `${title} ⭐${stargazersCount}`,
description,
link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined,
category: categories,
guid,
id: guid,
content: {
html: description,
text: description,
},
image,
banner: image,
language,
};
return processedItem;
});
const title: string = $('title').text();
return {
title,
description: title.split(/-/)[0],
link: targetUrl,
item: items,
allowEmpty: true,
author: title.split(/-/).pop()?.trim(),
language,
id: $('meta[property="og:url"]').attr('content'),
};
};
export const route: Route = {
path: '/:category?',
name: 'Ranking',
url: 'gitstar-ranking.com',
maintainers: ['nczitzk'],
handler,
example: '/gitstar-ranking/repositories',
parameters: {
category: {
description: 'Category, Repositories by default',
options: [
{
label: 'Users',
value: 'users',
},
{
label: 'Organizations',
value: 'organizations',
},
{
label: 'Repositories',
value: 'repositories',
},
],
},
},
description: `:::tip
To subscribe to [Repositories](https://gitstar-ranking.com/repositories), where the source URL is \`https://gitstar-ranking.com/repositories\`, extract the certain parts from this URL to be used as parameters, resulting in the route as [\`/gitstar-ranking/repositories\`](https://rsshub.app/gitstar-ranking/repositories).
:::
| Category | ID |
| ---------------------------------------------------------- | ----------------------------------------------------------------- |
| [Users](https://gitstar-ranking.com/users) | [users](https://rsshub.app/gitstar-ranking/users) |
| [Organizations](https://gitstar-ranking.com/organizations) | [organizations](https://rsshub.app/gitstar-ranking/organizations) |
| [Repositories](https://gitstar-ranking.com/repositories) | [repositories](https://rsshub.app/gitstar-ranking/repositories) |
`,
categories: ['programming'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['gitstar-ranking.com/:category'],
target: '/:category',
},
{
title: 'Users',
source: ['gitstar-ranking.com/users'],
target: '/users',
},
{
title: 'Organizations',
source: ['gitstar-ranking.com/organizations'],
target: '/organizations',
},
{
title: 'Repositories',
source: ['gitstar-ranking.com/repositories'],
target: '/repositories',
},
],
view: ViewType.Articles,
};
+9
View File
@@ -0,0 +1,9 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'Gitstar Ranking',
url: 'gitstar-ranking.com',
categories: ['programming'],
description: '',
lang: 'en',
};
@@ -0,0 +1,38 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}
{{ if stargazersCount }}
<table>
<tbody>
{{ if stargazersCount }}
<tr>
<th>Stars</th>
<td>{{ stargazersCount }}</td>
</tr>
{{ /if }}
{{ if language }}
<tr>
<th>Language</th>
<td>{{ language }}</td>
</tr>
{{ /if }}
{{ if description }}
<tr>
<th>Description</th>
<td>{{ description }}</td>
</tr>
{{ /if }}
</tbody>
</table>
{{ /if }}