Files
RSSHub/lib/routes/huggingface/models.ts
T
dependabot[bot]andTony 417ab04f06 chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 (#22310)
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 68.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix no-useless-coercion

* style: fix prefer-boolean-return

* style: fix no-useless-logical-operand

* style: fix prefer-single-replace

* style: fix consistent-conditional-object-spread

* style: fix no-useless-else

* style: fix prefer-unicode-code-point-escapes

* style: fix operator-assignment

* style: fix prefer-continue

* style: fix prefer-math-constants

* style: fix prefer-hoisting-branch-code

* style: fix prefer-array-slice

* style: fix consistent-class-member-order

* style: fix no-return-array-push

* style: fix prefer-promise-with-resolvers

* style: fix no-array-concat-in-loop

* style: fix no-loop-iterable-mutation

* style: fix no-invalid-argument-count

* style: fix no-duplicate-if-branches

* style: fix prefer-else-if

* style: fix no-unreadable-for-of-expression

* style: fix logical-assignment-operators

* style: fix default-export-style

* style: revert false positve in prefer-type-literal-last in #22274

* style: fix arrow-body-style

* style: add v68 recommended preset

* style: refactor GraphQL queries to use template literals for better readability

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-20 23:11:27 +08:00

138 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { load } from 'cheerio';
import MarkdownIt from 'markdown-it';
import { FetchError } from 'ofetch';
import type { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
const md = MarkdownIt({
html: true,
});
export const route: Route = {
path: '/models/:group',
categories: ['programming'],
example: '/huggingface/models/deepseek-ai',
parameters: {
group: 'The organization or user group name',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['huggingface.co/:group/models'],
target: '/models/:group',
},
],
name: 'Group Models',
maintainers: ['WuNein'],
handler,
url: 'huggingface.co',
};
async function handler(ctx) {
const { group, cycle = 'date' } = ctx.req.param();
// Validate cycle parameter
if (!['date', 'week', 'month'].includes(cycle)) {
throw new Error(`Invalid cycle: ${cycle}`);
}
const url = `https://huggingface.co/${group}/models?sort=created`;
const { body: response } = await got(url);
const $ = load(response);
let items = $('article')
.toArray()
.map((article) => {
const $article = $(article);
const title = $article.find('a > div > header > h4').text().trim();
const link = `https://huggingface.co/${title}`;
const timeElement = $article.find('a > div > div > span.truncate > time');
const datetime = timeElement.attr('datetime');
const description = $article.text().replaceAll(/\s+/g, ' ').trim();
return {
title,
link,
description,
pubDate: datetime ? parseDate(datetime) : undefined,
};
})
.filter((item) => item.title);
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
try {
// 一般有readme, 且没设置权限的模型
const { body: detailResp } = await got(item.link + '/raw/main/README.md');
item.description += md.render(detailResp);
// Qwen一般是外链,主要针对DeepSeek
const $Out = load(item.description);
$Out('img').each((_, e) => {
const $e = $Out(e);
const src = $e.attr('src');
if (!src) {
return;
}
// 如果不是绝对 URL(没有 http:// 或 https://),添加 base URL
if (/^https?:\/\//i.test(src)) {
// 已经是完整 URL
$e.attr('src', src);
} else if (src.startsWith('/')) {
// 处理以 / 开头的绝对路径
$e.attr('src', `${item.link}/resolve/main/` + src);
} else {
// 处理相对路径(如 ./images/pic.png 或 images/pic.png)
const baseUrl = item.link + '/resolve/main/';
$e.attr('src', baseUrl + src.replace(/^\.\//, ''));
}
});
item.description = $Out.html();
return item;
} catch (error) {
if (error instanceof FetchError && (error.statusCode === 403 || error.statusCode === 401)) {
// 要权限的情况
// Example: https://huggingface.co/facebook/sam-3d-objects/raw/main/README.md
try {
// 以免再次出错
// 这里可以不管image相对绝对路径,一般要认证的模型都是外链、或者索性图也是403
const { body: respHtml } = await got(item.link + '/blob/main/README.md?code=true');
const $ = load(respHtml);
const detailHtml = $('body').find('div > main > div > section > div > div > div > div > div > table > tbody').text().trim();
item.description += md.render(detailHtml);
return item;
} catch {
return item;
}
}
// 没有介绍页面的情况 error.statusCode === 404
// Example: https://huggingface.co/ianyang02/aita_qwen3-30b/raw/main/README.md
// return item;
// 其他错误的情况
return item;
}
})
)
);
return {
title: `Huggingface ${group} Models`,
link: url,
item: items,
};
}