Skip to content

Instantly share code, notes, and snippets.

@mohitmun
Created February 19, 2018 11:40
Show Gist options
  • Select an option

  • Save mohitmun/014786afd8666dd54d384819501bd495 to your computer and use it in GitHub Desktop.

Select an option

Save mohitmun/014786afd8666dd54d384819501bd495 to your computer and use it in GitHub Desktop.
require 'json'
require 'csv'
def run_command(command)
puts "Running command #{command}"
res = `#{command}`
return res
end
args = {}
ARGV.each do |arg|
match = /--(?<key>.*?)=(?<value>.*)/.match(arg)
args[match[:key].to_sym] = match[:value] # e.g. args['first_name'] = 'donald'
end
# add
query = ["sort=stars", "per_page=100"]
query += ["q=#{args[:q]} in:readme"] if !args[:q].nil?
query += ["topic=#{args[:topic]}"] if !args[:topic].nil?
command = "curl 'https://api.github.com/search/repositories?#{query.join("&").gsub(" ", "+")}'"
command += " -u #{args[:username]}:#{args[:password]}" if !args[:username].nil?
response = run_command(command)
response = JSON.parse(response)
# puts response
puts "Total repos found: #{response['total_count']}"
filename = args[:filename] || "result.csv"
CSV.open(filename, "wb") do |csv|
csv << ["Name", "Stars", "Url"]
response["items"].each do |item|
csv << [item['full_name'], item['stargazers_count'], item['clone_url']]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment