Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.