From 66bcaa0c392b422565d841f44db0e36629280283 Mon Sep 17 00:00:00 2001 From: karasu Date: Tue, 30 Jul 2024 21:04:09 +0800 Subject: [PATCH] fix: github search result (#16301) --- lib/routes/github/search.ts | 38 +++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/routes/github/search.ts b/lib/routes/github/search.ts index f8c5b344e..480a6a39d 100644 --- a/lib/routes/github/search.ts +++ b/lib/routes/github/search.ts @@ -1,6 +1,5 @@ -import { Route } from '@/types'; -import got from '@/utils/got'; -import { load } from 'cheerio'; +import { DataItem, Route } from '@/types'; +import ofetch from '@/utils/ofetch'; import * as url from 'node:url'; const host = 'https://github.com'; @@ -40,22 +39,25 @@ async function handler(ctx) { const suffix = 'search?o='.concat(order, '&q=', encodeURIComponent(query), '&s=', sort, '&type=Repositories'); const link = url.resolve(host, suffix); - const response = await got.get(link); - const $ = load(response.data); + const response = await ofetch(link, { + headers: { + accept: 'application/json', + }, + }); - const out = $('.repo-list li') - .slice(0, 10) - .map(function () { - const a = $(this).find('.f4.text-normal > a'); - const single = { - title: a.text(), - author: a.text().split('/')[0].trim(), - link: host.concat(a.attr('href')), - description: $(this).find('div p').text().trim(), - }; - return single; - }) - .get(); + const out = response.payload.results.map((item) => { + const { + repo: { repository }, + hl_trunc_description, + } = item; + + return { + title: repository.name, + author: repository.owner_login, + link: host.concat(`/${repository.owner_login}/${repository.name}`), + description: hl_trunc_description, + } as DataItem; + }); return { allowEmpty: true,