Skip to content

Commit

Permalink
fix: github search result (#16301)
Browse files Browse the repository at this point in the history
  • Loading branch information
KarasuShin authored Jul 30, 2024
1 parent 376c6d6 commit 66bcaa0
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/routes/github/search.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 66bcaa0

Please sign in to comment.