Skip to content

Instantly share code, notes, and snippets.

@soulim
Created June 15, 2015 10:40
Show Gist options
  • Save soulim/d69e5dabc511c325f089 to your computer and use it in GitHub Desktop.
Save soulim/d69e5dabc511c325f089 to your computer and use it in GitHub Desktop.
GitHub repositories ranking algorithm (based on HN ranking algorithm)
# Inspired by Hacker News ranking algorithm
# http://www.righto.com/2013/11/how-hacker-news-ranking-really-works.html
require 'json'
require 'ostruct'
require 'time'
require 'open-uri'
FORKS_FACTOR = 1.7
STARGAZERS_FACTOR = 1.5
OPEN_ISSUES_FACTOR = 1.3
FORK_FACTOR = 0.5
GRAVITY = 1.8
pretty_print = -> (repo) {
puts "#{repo.name}: #{repo.rating} (forks: #{repo.forks_count}, stargazers: #{repo.stargazers_count}, issues: #{repo.open_issues_count}, fork: #{repo.fork})"
}
json = URI.parse('https://api.github.com/users/soulim/repos?per_page=100').read
# repos = JSON.parse(File.read('./repos.json')).map do |repo|
repos = JSON.parse(json).map do |repo|
OpenStruct.new(repo).tap do |repo|
repo.pushed_at = Time.parse(repo.pushed_at)
repo.created_at = Time.parse(repo.created_at)
repo.updated_at = Time.parse(repo.updated_at)
repo.rating = begin
rating = 0.0
rating = repo.forks_count * FORKS_FACTOR
rating += repo.stargazers_count * STARGAZERS_FACTOR
rating += repo.open_issues_count * OPEN_ISSUES_FACTOR
if repo.fork
rating *= FORK_FACTOR
end
rating /= ((Time.now.to_i - repo.pushed_at.to_i)/3600)**GRAVITY
rating
end
end
end
puts '# Unordered'
puts ''
repos.each { |repo| pretty_print.call(repo) }
puts ''
puts '# Ordered by rating'
puts ''
repos.sort_by(&:rating).reverse.each { |repo| pretty_print.call(repo) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment