Skip to content

Instantly share code, notes, and snippets.

@j-hannes
Created March 23, 2016 11:04
Show Gist options
  • Save j-hannes/1f0b75bfe718d91f1141 to your computer and use it in GitHub Desktop.
Save j-hannes/1f0b75bfe718d91f1141 to your computer and use it in GitHub Desktop.
Request github in batches of 30
const fetch = require('isomorphic-fetch')
const R = require('ramda')
const url = 'https://api.github.com/search/repositories?q=+language:javascript'
const listOccurences = names => {
const occurences = {}
names.forEach(name => {
if (name in occurences) {
occurences[name] = occurences[name] + 1
} else {
occurences[name] = 1
}
})
return occurences
}
const fetchPage = page => (
new Promise((resolve, reject) => {
fetch(`${url}&page=${page}`)
.then(result => result.json())
.then(result => {
if (result.items) {
return result.items.map(item => item.owner.login)
} else {
reject(result)
}
})
.then(resolve)
.catch(reject)
})
)
var interval
const getResults = (requests) =>
new Promise((resolve, reject) => {
var cachedResults = []
var requestBatches = R.splitEvery(30, requests)
const requestGithub = () => {
if (requestBatches.length > 1) {
console.log('found more than 30 requests, waiting 1min to cache results')
Promise.all(requestBatches.pop())
.then(results => {
cachedResults = cachedResults.concat(results)
})
.catch(reject)
} else {
Promise.all(requestBatches.pop())
.then(results => {
resolve(cachedResults.concat(results))
})
.catch(reject)
}
}
requestGithub()
interval = setInterval(requestGithub, 60000)
})
const pages = R.range(1, 35)
const requestPromises = pages.map(fetchPage)
getResults(requestPromises)
.then(results => {
clearInterval(interval)
const merged = [].concat.apply([], results)
const occurences = listOccurences(merged)
console.log(JSON.stringify(occurences, null, 2))
})
.catch(error => {
clearInterval(interval)
console.log(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment