Skip to content

Instantly share code, notes, and snippets.

@nealrs
Created June 23, 2016 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealrs/5325244f00e5aa9ecad716a5c8c03c1e to your computer and use it in GitHub Desktop.
Save nealrs/5325244f00e5aa9ecad716a5c8c03c1e to your computer and use it in GitHub Desktop.
search for github users in a particular geo and find their email address.
require "octokit"
require "json"
auth = "your-github-auth-token-goes-here"
Octokit.auto_paginate = true
c = Octokit::Client.new(:access_token => auth, :auto_paginate => true, :per_page =>1000)
### GET USER IDS FOR GEOS DEFINED in `locations` ###
data = []
# locations = [
# 'brooklyn',
# 'manhattan, new york',
# 'manhattan, ny',
# 'new york, ny',
# 'new york, ny',
# 'new york',
# 'staten island',
# 'bronx',
# 'new york city',
# 'nyc',
# ]
locations = ["new york"]
locations.each do |l|
puts 'searching for users in: ' + l
s = c.search_users('location:'+l+'type:user')
puts "# of results from API: " + s.total_count.to_s
puts "# of results in `items`: " + s.items.length.to_s
s.items.each do |i|
puts "user: " + i[:login]
data.push(i[:login])
end
end
### FIND USER EMAILS AND WRITE TO CSV ###
### inspired / cribbed from https://github.com/paulirish/github-email
### FYI, you still need to read through and check emails!!! (lots of irrelevant ones will be pulled)
writefile = File.open "users.csv","a"
writefile.puts "login,emails"
data.each_with_index do |d, index|
#hai rate limiting
sleep 15 if index % 5 == 0
puts d
e = []
###### check github for email
s = c.user(d)
if s[:email].to_s
e.push(s['email'].to_s)
end
###### emails from commits
t = c.user_events(d)
t.each do |i|
js = i.map(&:to_a).to_s
em = js.scan(/email=>(\".*?\")/)
em.uniq.each do |j|
tmp = j.to_s.gsub! '["\"', ''
tmp = tmp.to_s.gsub! '\""]', ''
e.push(tmp)
end
end
puts "login: " + d.to_s + ", name: " + s['name'].to_s + ", email: " + e.join(",")
writefile.puts d + "," + s['name'].to_s + "," +e.join(",")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment