Created
November 28, 2017 15:21
-
-
Save albertorestifo/5a7665c9a1da05b2062cc7c60a3bb2c7 to your computer and use it in GitHub Desktop.
A quick script to sort by stars
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
const fs = require('fs'); | |
const rp = require('request-promise'); | |
const Promise = require('bluebird'); | |
const list = fs.readFileSync('./list', { encoding: 'utf8' }); | |
const urls = list.split('\n'); | |
const TOKEN = '<YOUR API TOKEN>'; | |
const repos = urls.map((url) => { | |
const path = url.replace('https://github.com/', ''); | |
const [owner, name] = path.split('/'); | |
return { owner, name }; | |
}); | |
const query = ` | |
query getStars($owner: String!, $name: String!) { | |
repository(owner: $owner, name: $name) { | |
stargazers { | |
totalCount | |
} | |
} | |
} | |
`; | |
Promise.map(repos, (variables) => Promise.resolve(rp({ | |
uri: 'https://api.github.com/graphql', | |
headers: { | |
Authorization: `bearer ${TOKEN}', | |
'User-Agent': 'Request-Promise', | |
}, | |
method: 'POST', | |
json: true, | |
body: { | |
query, | |
variables, | |
}, | |
})) | |
.then(res => ({ | |
url: `https://github.com/${variables.owner}/${variables.name}`, | |
stars: (res.data && res.data.repository) ? res.data.repository.stargazers.totalCount : 0, | |
})), { concurrency: 3 }) | |
.then((data) => { | |
const sorted = data.sort((a, b) => b.stars - a.stars) | |
.map(row => `${row.stars}\t${row.url}`) | |
.join('\n'); | |
console.log(sorted); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment