Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
- add languages
- add contributors
- update sorting (license & language)
  • Loading branch information
tunny17 committed Dec 13, 2024
1 parent 583ab14 commit 309dd00
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
4 changes: 4 additions & 0 deletions routes/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ app.get('/repositories', async (c) => {
CASE WHEN ${sortBy} = 'forks' AND ${order} = 'desc' THEN forks END DESC,
CASE WHEN ${sortBy} = 'author' AND ${order} = 'asc' THEN author END ASC,
CASE WHEN ${sortBy} = 'author' AND ${order} = 'desc' THEN author END DESC
CASE WHEN ${sortBy} = 'license' AND ${order} = 'asc' THEN license END ASC,
CASE WHEN ${sortBy} = 'license' AND ${order} = 'desc' THEN license END DESC
CASE WHEN ${sortBy} = 'language' AND ${order} = 'asc' THEN language END ASC,
CASE WHEN ${sortBy} = 'language' AND ${order} = 'desc' THEN language END DESC
LIMIT ${limit}
OFFSET ${offset};
`;
Expand Down
68 changes: 67 additions & 1 deletion utils/scrape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ScrapedRepository,
DBRepository,
GitHubRepository,
Contributor
} from '../types/repository.ts';

const databaseUrl = Deno.env.get('DATABASE_URL')!;
Expand Down Expand Up @@ -40,6 +41,8 @@ const fetchDataFromGithub = async (
});

const apiData = (await apiResponse.json()) as GitHubRepository;
const languages = await fetchLanguages(apiData.languages_url);
const contributors = await fetchContributors(`${GITHUB_API_BASE}/${user}/${repo}/contributors`);

repositories.push({
name: apiData.name,
Expand All @@ -60,6 +63,8 @@ const fetchDataFromGithub = async (
name: apiData.license?.name || '',
url: apiData.license?.url || '',
},
language: languages,
contributors,
forks: apiData.forks || 0,
open_issues_count: apiData.open_issues_count || 0,
archived: apiData.archived || false,
Expand Down Expand Up @@ -102,6 +107,63 @@ function convertToJSON(repositories: string[]): ScrapedRepository[] {
});
}

const fetchLanguages = async (languageUrl: string): Promise<string[]> => {
if (languageUrl === '') {
return [];
}

try {
const response = await fetch(languageUrl, {
headers: GITHUB_HEADERS,
});

if (!response.ok) {
throw new Error(`Failed to fetch languages from ${languageUrl}`);
}

const languagesData = await response.json();
return Object.keys(languagesData);
} catch (error) {
console.error(`Error fetching languages:`, error);
return [];
}
};

const fetchContributors = async (contributorsUrl: string): Promise<Contributor[]> => {
if (contributorsUrl === '') {
return [];
}

try {
const response = await fetch(contributorsUrl, { headers: GITHUB_HEADERS });

if (!response.ok) {
console.error(`Failed to fetch contributors from ${contributorsUrl}`);
return [];
}

const contributorsData = await response.json();

const sortedContributors = contributorsData
.map((contributor: Contributor) => ({
id: contributor.id,
node_id: contributor.node_id,
avatar_url: contributor.avatar_url,
username: contributor.login,
contributions: contributor.contributions,
profileUrl: contributor.html_url,
}))
.sort((a: { contributions: number; }, b: { contributions: number; }) => b.contributions - a.contributions) // Sort by contributions descending
.slice(0, 5);

return sortedContributors;
} catch (error) {
console.error(`Error fetching contributors:`, error);
return [];
}
};


export const scrape = async () => {
console.log('Fetching Repositories From GitHub...');

Expand Down Expand Up @@ -143,6 +205,8 @@ export const scrape = async () => {
stars,
topics,
license,
language,
contributor,
forks,
open_issues_count,
archived,
Expand All @@ -161,6 +225,8 @@ export const scrape = async () => {
${repo.stars || 0},
${repo.topics},
${repo.license},
${repo.language},
${repo.contributors},
${repo.forks || 0},
${repo.open_issues_count || 0},
${repo.archived},
Expand All @@ -186,4 +252,4 @@ export const scrape = async () => {
}

console.log('Data saved to Neon.');
};
};
11 changes: 11 additions & 0 deletions utils/types/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export interface ScrapedRepository {
authorLink: string;
}

export interface Contributor {
login: string;
id: number;
node_id: string;
avatar_url: string;
html_url: string;
contributions: number
}

export interface DBRepository {
name: string;
link: string;
Expand All @@ -24,6 +33,8 @@ export interface DBRepository {
name: string;
url: string;
};
language: string[];
contributors: Contributor[];
forks: number;
open_issues_count: number;
archived: boolean;
Expand Down

0 comments on commit 309dd00

Please sign in to comment.