Skip to content

Instantly share code, notes, and snippets.

@albertorestifo
Created November 28, 2017 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertorestifo/5a7665c9a1da05b2062cc7c60a3bb2c7 to your computer and use it in GitHub Desktop.
Save albertorestifo/5a7665c9a1da05b2062cc7c60a3bb2c7 to your computer and use it in GitHub Desktop.
A quick script to sort by stars
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