Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Popular Uruguayan githubbers

View gist:2222145
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#!/usr/bin/env ruby
 
require 'rubygems'
require 'json'
require 'rankable_graph'
require 'net/http'
require 'mechanize'
require 'nokogiri'
 
def follower_usernames_of(user)
@follower_usernames_of ||= {}
return @follower_usernames_of[user] if @follower_usernames_of.has_key?(user)
 
@time_of_last_req ||= Time.now
json_result = Net::HTTP.get(URI("http://github.com/api/v2/json/user/show/#{user}/followers"))
time_since_last_req = Time.now - @time_of_last_req
@time_of_last_req = Time.now
time_to_sleep = 1 - time_since_last_req
if time_to_sleep > 0
puts "Sleeping #{time_to_sleep} secs"
sleep(time_to_sleep)
end
@follower_usernames_of[user] = [JSON.parse(json_result)["users"]].flatten.compact
end
 
 
def get_uruguayan_usernames
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
 
html = ""
(1..10).each do |i|
html += a.get("https://github.com/search?langOverride=&language=&q=location%3A+uruguay&repo=&start_value=#{i}&type=Users").body
end
 
doc = Nokogiri(html)
doc.css("h2.title a").map{|a| a.text}.uniq
end
 
 
def grow_list_with_followers(initial_username_list)
followers_of_initial_list = initial_username_list.map do |username|
follower_usernames_of(username)
end
 
followers_of_initial_list.flatten!.uniq!
 
(initial_username_list + followers_of_initial_list).uniq
end
 
def get_uruguayans_and_their_followers(levels)
if !File.exists?("usernames")
uruguayan_usernames = get_uruguayan_usernames
 
usernames = uruguayan_usernames
 
#The biggest the number of leves, the more precise the calculation is but slower
levels.times do
usernames = grow_list_with_followers(usernames)
end
 
File.open("usernames", 'wb') do |f|
f.write Marshal.dump([usernames, uruguayan_usernames])
end
 
[usernames, uruguayan_usernames]
else
Marshal.load(File.binread("usernames"))
end
end
 
usernames, uruguayan_usernames = get_uruguayans_and_their_followers(levels = 2)
 
puts "#{usernames.size} usernames"
unused_id = usernames.size
 
id2username_hash = {}
username2id_hash = {}
usernames.each_with_index do |username, user_id|
id2username_hash[user_id] = username
username2id_hash[username] = user_id
end
 
puts
rankable_graph = RankableGraph.new
id2username_hash.each do |id, user|
follower_usernames = follower_usernames_of(user)
follower_usernames.each do |follower_name|
follower_id = username2id_hash[follower_name]
if follower_id.nil?
follower_id = unused_id
unused_id += 1
end
 
puts "#{follower_name}(#{follower_id}) follows #{user}(#{id})"
rankable_graph.link(follower_id.to_i, id.to_i)
end
end
 
ranked_uruguayans = []
rankable_graph.rank(0.85, 0.0001) do |id, rank|
ranked_uruguayans << [id, rank] if uruguayan_usernames.include?(id2username_hash[id])
end
 
ranked_uruguayans.sort! do |a, b|
b.last <=> a.last
end
 
ranked_uruguayans.each do |id, rank|
puts "#{id2username_hash[id]}: #{rank}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.