From d4ff0ff20d81f2c2f82a721f58b23af1ec15a955 Mon Sep 17 00:00:00 2001 From: Theresa <63280168+sd0ric4@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:08:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(github):=20=E6=B7=BB=E5=8A=A0=20GitHub=20A?= =?UTF-8?q?dvisory=20Database=20RSS=20=E8=B7=AF=E7=94=B1=20(#16745)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(github-advisor): add GitHub Advisory Database RSS route and namespace * fix(xiaohongshu): handle optional chaining for cover image URL * refactor(github-advisor): remove unused code and files * feat(github): 添加 GitHub Advisory Database RSS 路由 * refactor(route): fix category in GitHub Advisor route * refactor(route): 更新 GitHub Advisor 路由中的类别参数名 * refactor(route): Updated to scrape HTML directly instead of relying on third-party API --- lib/routes/github/advisor.ts | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 lib/routes/github/advisor.ts diff --git a/lib/routes/github/advisor.ts b/lib/routes/github/advisor.ts new file mode 100644 index 000000000..8a0edd8dc --- /dev/null +++ b/lib/routes/github/advisor.ts @@ -0,0 +1,97 @@ +import { Route } from '@/types'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import ofetch from '@/utils/ofetch'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/advisor/data/:type?/:category?', + categories: ['programming'], + example: '/github/advisor/data/reviewed/composer', + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['github.com/advisories', 'github.com'], + }, + ], + name: 'Github Advisory Database RSS', + maintainers: ['sd0ric4'], + handler, + description: ` +| Type | Description | Explanation | +| --- | --- | --- | +| reviewed | Reviewed | 已审核 | +| unreviewed | Unreviewed | 未审核 | + +| Category | Description | Explanation | +| --- | --- | --- | +| composer | Composer | PHP 依赖管理工具 | +| go | Go | Go 语言包管理工具 | +| maven | Maven | Java 项目管理工具 | +| npm | NPM | Node.js 包管理工具 | +| nuget | NuGet | .NET 包管理工具 | +| pip | Pip | Python 包管理工具 | +| pub | Pub | Dart 包管理工具 | +| rubygems | RubyGems | Ruby 包管理工具 | +| rust | Rust | Rust 包管理工具 | +| erlang | Erlang | Erlang 包管理工具 | +| actions | Actions | GitHub Actions | +| swift | Swift | Swift 包管理工具 |`, +}; + +async function handler(ctx) { + const { category, type } = ctx.req.param(); + + const apiRootUrl = 'https://github.com/advisories'; + const apiUrl = `${apiRootUrl}?query=type%3A${type}+ecosystem%3A${category}`; + const currentUrl = `https://github.com/advisories`; + + const response = await got({ + method: 'get', + url: apiUrl, + }); + const $ = load(response.data); + + const list = $('div.Box-row') + .toArray() + .map((item) => { + item = $(item); + const a = item.find('a.Link--primary'); + const b = item.find('relative-time').attr('datetime'); + const title = a.text() || 'No title'; + const link = a.attr('href') || '#'; + const pubDate = parseDate(b || ''); + + return { + title, + link: `https://github.com${link}`, + pubDate, + description: '', + }; + }); + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const response = await ofetch(item.link); + const $ = load(response); + + item.description = $('.comment-body').first().html() || ''; + + return item; + }) + ) + ); + return { + title: `GitHub Advisory Database RSS - ${category} - ${type}`, + link: currentUrl, + item: items, + }; +}