Created
January 27, 2025 15:59
-
-
Save emmaodia/42c22c780136bb4b0df2a5c89d91d065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const fs = require("fs"); | |
const path = require("path"); | |
// GitHub API base URL | |
const GITHUB_API_BASE_URL = "https://api.github.com"; | |
// Replace with your GitHub personal access token (optional but recommended for higher rate limits) | |
const GITHUB_PERSONAL_ACCESS_TOKEN = "your_personal_access_token"; | |
// Axios instance with authentication headers (if token is provided) | |
const axiosInstance = axios.create({ | |
baseURL: GITHUB_API_BASE_URL, | |
headers: GITHUB_PERSONAL_ACCESS_TOKEN ? { Authorization: `Bearer ${GITHUB_PERSONAL_ACCESS_TOKEN}` } : {}, | |
}); | |
// Function to verify if a GitHub profile meets the criteria | |
async function checkGitHubProfile(username) { | |
try { | |
// Step 1: Verify if the profile exists | |
const userResponse = await axiosInstance.get(`/users/${username}`); | |
// Step 2: Check the number of public repositories | |
const publicReposCount = userResponse.data.public_repos; | |
if (publicReposCount <= 3) { | |
console.log(`Profile ${username} does not have more than 3 repositories.`); | |
return { username, exists: true, meetsCriteria: false, isProspect: true, reason: "Less than 3 repositories" }; | |
} | |
// Step 3: Get the user's commits count from all repos | |
let totalCommits = 0; | |
let page = 1; | |
while (true) { | |
const reposResponse = await axiosInstance.get(`/users/${username}/repos`, { | |
params: { per_page: 100, page }, | |
}); | |
const repos = reposResponse.data; | |
if (repos.length === 0) break; | |
for (const repo of repos) { | |
const owner = repo.owner.login; | |
const repoName = repo.name; | |
const commitsResponse = await axiosInstance.get(`/repos/${owner}/${repoName}/commits`, { | |
params: { author: username, per_page: 1 }, | |
}); | |
totalCommits += commitsResponse.headers["x-total-count"] ? parseInt(commitsResponse.headers["x-total-count"], 10) : 0; | |
} | |
page++; | |
} | |
if (totalCommits <= 5) { | |
console.log(`Profile ${username} does not have more than 5 commits.`); | |
return { username, exists: true, meetsCriteria: false, isProspect: true, reason: "Less than 5 commits" }; | |
} | |
console.log(`Profile ${username} meets all criteria.`); | |
return { username, exists: true, meetsCriteria: true, isProspect: false, reason: "Meets all criteria" }; | |
} catch (error) { | |
if (error.response && error.response.status === 404) { | |
console.log(`Profile ${username} does not exist on GitHub.`); | |
return { username, exists: false, meetsCriteria: false, isProspect: false, reason: "Profile does not exist" }; | |
} else { | |
console.error(`Error checking profile ${username}:`, error.message); | |
return { username, exists: false, meetsCriteria: false, isProspect: false, reason: "Error occurred" }; | |
} | |
} | |
} | |
// Function to check multiple GitHub profiles | |
async function checkProfiles(profiles) { | |
const results = []; | |
for (const username of profiles) { | |
const result = await checkGitHubProfile(username); | |
results.push(result); | |
} | |
// Write results to a CSV file | |
const csvContent = [ | |
"Username,Exists,MeetsCriteria,IsProspect,Reason", | |
...results.map((r) => `${r.username},${r.exists},${r.meetsCriteria},${r.isProspect},"${r.reason}"`), | |
].join("\n"); | |
const outputPath = path.join(__dirname, "github_profiles_check.csv"); | |
fs.writeFileSync(outputPath, csvContent); | |
console.log(`Results exported to ${outputPath}`); | |
} | |
// Example usage | |
const githubProfiles = ['octocat', 'torvalds', 'nonexistentprofile']; | |
checkProfiles(githubProfiles); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment