Created
April 27, 2012 15:32
-
-
Save harlow/2510198 to your computer and use it in GitHub Desktop.
Gitsucker
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
#!/usr/bin/env ruby | |
require 'open-uri' | |
require 'nokogiri' | |
require 'json' | |
class Repo | |
def initialize(name) | |
@name = name | |
end | |
def forking_authors | |
forking_author_names. | |
map {|author_name| Author.new(author_name) }. | |
sort_by {|author| author.score }. | |
reverse | |
end | |
private | |
def forking_author_names | |
JSON.parse(open(author_forks_url).read).map {|fork| fork['owner']['login'] } | |
end | |
def author_forks_url | |
"https://api.github.com/repos/#{author_name}/#{@name}/forks" | |
end | |
def author_name | |
JSON.parse(open(repo_url).read)['repositories'].first['username'] | |
end | |
def repo_url | |
"https://api.github.com/legacy/repos/search/#{@name}" | |
end | |
end | |
class Author | |
def initialize(name) | |
@name = name | |
end | |
def stats | |
[ | |
@name, | |
repository_count('public'), | |
repository_count('source'), | |
repository_count('fork'), | |
language_count('Ruby'), | |
language_count('Javascript'), | |
score | |
] | |
end | |
def score | |
score = repository_count('source') * 3 | |
score += language_count('Ruby') * 2 | |
score += language_count('Javascript') * 2 | |
score += repository_count('public') * 1 | |
end | |
private | |
def repository_count(repository_type) | |
github_profile.css(".#{repository_type}").count | |
end | |
def language_count(language) | |
github_profile.css('ul.repo-stats').select do |li| | |
li.text.include? language | |
end.count | |
end | |
def github_profile | |
@github_profile ||= Nokogiri::HTML(open("https://github.com/#{@name}")) | |
end | |
end | |
class Table | |
def self.header_row | |
self.col_widths % %w(name public original forked ruby javascript score) | |
end | |
def self.body_row(data) | |
self.col_widths % data | |
end | |
def self.horizontal_line_break | |
'=' * 80 | |
end | |
private | |
def self.col_widths | |
'%-20s %-10s %-10s %-10s %-10s %-10s %-10s' | |
end | |
end | |
ARGV.each do |query| | |
puts Table.header_row | |
puts Table.horizontal_line_break | |
Repo.new(query).forking_authors.each do |author| | |
puts Table.body_row author.stats | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment