Last active
January 16, 2023 18:18
-
-
Save ademidun/0d3c57666bcf782e7160181493b12026 to your computer and use it in GitHub Desktop.
a node script that gets the top 10 most starred repos on safe-global github by sending a request to github API and save the name, url, description and star count to a JSON and CSV file
This file contains 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
/** | |
A node script that gets the top 10 most starred repos on safe-global github | |
by sending a request to github API | |
and save the name, url, description and star count to a JSON and CSV file | |
courtesy of ChatGPT: https://i.imgur.com/bQuh1Sr.png **/ | |
const https = require('https'); | |
const fs = require('fs'); | |
const orgName = 'safe-global'; | |
const options = { | |
hostname: 'api.github.com', | |
path: `/orgs/${orgName}/repos`, | |
headers: { 'User-Agent': 'node.js' } | |
}; | |
const req = https.request(options, (res) => { | |
let data = ''; | |
res.on('data', (chunk) => { | |
data += chunk; | |
}); | |
res.on('end', () => { | |
const repos = JSON.parse(data); | |
const output = repos.map(repo => ({ | |
name: repo.name, | |
url: repo.html_url, | |
description: repo.description, | |
stars: repo.stargazers_count | |
})).sort(function(first, second) { | |
return second.stars - first.stars; | |
}); | |
fs.writeFileSync(`repos_${orgName}.json`, JSON.stringify(output, null, 2)); | |
console.log('Repo data saved to repos.json'); | |
const csv_output = convertToCSV(output); | |
fs.writeFileSync(`repos_${orgName}.csv`, csv_output); | |
console.log('Repo data saved to repos.csv'); | |
}); | |
}); | |
req.on('error', (e) => { | |
console.error(`Problem with request: ${e.message}`); | |
}); | |
req.end(); | |
function convertToCSV(arr) { | |
const array = [Object.keys(arr[0])].concat(arr) | |
return array.map(it => { | |
return Object.values(it).toString() | |
}).join('\n') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment