Created
April 7, 2019 20:40
-
-
Save avoidwork/1d759caeae301feb600d0ecf9866f416 to your computer and use it in GitHub Desktop.
Adobe employees active on github
This file contains hidden or 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
(async function () { | |
const fetch = require('node-fetch'), | |
moment = require('moment'), | |
users = new Map(), | |
size = 100; | |
let i = -1, | |
nth = -1, | |
done = false; | |
async function sleep (ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
console.log('Retrieving users who have `Adobe` in org name & an event within the last year'); | |
do { | |
try { | |
const x = 100 * ++i, | |
res = await fetch(`https://api.github.com/search/users?q=adobe+in:org+type:user&per_page=${size}&page=${i + 1}`), | |
data = await res.json(); | |
if (nth === -1) { | |
nth = data.total_count; | |
} | |
if (res.ok) { | |
if (data.items.length > 0) { | |
console.log(`Retrieved ${data.items.length} users`); | |
data.items.forEach(v => users.set(v.id, v)); | |
if (x >= nth) { | |
done = true; | |
} else { | |
const remaining = Number(res.headers.get('x-ratelimit-remaining') || 0); | |
if (remaining === 0) { | |
const x = Number(res.headers.get('x-ratelimit-reset')) - Math.floor(new Date().getTime() / 1e3); | |
console.log(`Sleeping for ${x} seconds due to rate limiting`); | |
await sleep(x * 1e3 + 1); | |
} | |
} | |
} else { | |
done = true; | |
} | |
} else { | |
throw new Error(data.message); | |
} | |
} catch (err) { | |
console.error(err.message); | |
done = true; | |
} | |
} while (done === false); | |
console.log(`Retrieved ${users.size} of ${nth} users`); | |
for (const user of users.values()) { | |
console.log(`Retrieving events for ${user.login}`); | |
const res = await fetch(user.events_url.replace(/{.*/, '')), | |
data = res.ok ? await res.json() : [], | |
ev = data[0] || null; | |
if (ev !== null) { | |
const m = moment(ev.created_at); | |
if (m.diff(moment(), 'year') > 0) { | |
await users.delete(user.id); | |
} | |
} else { | |
await users.delete(user.id); | |
} | |
const remaining = Number(res.headers.get('x-ratelimit-remaining') || 0); | |
if (remaining === 0) { | |
const x = Number(res.headers.get('x-ratelimit-reset')) - Math.floor(new Date().getTime() / 1e3); | |
console.log(`Sleeping for ${x} seconds due to rate limiting`); | |
await sleep(x * 1e3 + 1); | |
} | |
} | |
console.log(`Adobe users with activity within the last year: ${users.size}`); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment