Last active
August 29, 2015 14:01
-
-
Save kejadlen/7b12e1e8cebf1d1cf78e to your computer and use it in GitHub Desktop.
This file contains 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
require 'base64' | |
require 'find' | |
require 'dotenv' | |
Dotenv.load | |
require 'faraday' | |
require 'faraday_middleware' | |
require 'pry' | |
module CodePunch | |
module Github | |
LANGS = %w[ ruby go ] | |
ACCESS_TOKEN = ENV['ACCESS_TOKEN'] | |
N = 5 | |
API = Faraday.new('https://api.github.com/v3') do |conn| | |
conn.request :oauth2, ACCESS_TOKEN | |
conn.request :json | |
conn.response :json, content_type: /\bjson/ | |
conn.adapter Faraday.default_adapter | |
end | |
def self.run | |
freqs = Hash.new(0) | |
LANGS.each do |lang| | |
lang = "language:#{lang}" | |
result = API.get('/search/repositories', q: lang) | |
repos = result.body['items'][0,N] | |
repos.each do |repo| | |
puts "Looking through #{repo['full_name']}" | |
result = API.get('/search/code', q: ['the', lang, "repo:#{repo['full_name']}"].join(' ')) | |
files = result.body['items'][0,N] | |
files.each do |file| | |
puts "Looking at #{file['path']}" | |
url = file['url'] | |
content = Base64.decode64(API.get(url).body['content']) | |
content.chars.each {|char| freqs[char] += 1 unless char =~ /[\sa-zA-Z0-9]/ } | |
end | |
end | |
end | |
freqs.sort_by {|_,v| v}.reverse.each do |char, freq| | |
puts "#{char} #{freq}" | |
end | |
end | |
end | |
module Source | |
EXTS = %w[ .go .rb ] | |
IGNORE = %w[ .bundle ] | |
def self.run(dir) | |
freqs = Hash.new(0) | |
Find.find(File.expand_path(dir)) do |path| | |
Find.prune if IGNORE.include?(File.basename(path)) | |
puts "Looking through #{path}" and next if File.directory?(path) | |
if EXTS.include?(File.extname(path)) | |
puts "Looking at #{path}" | |
content = File.read(path) | |
content.chars.each {|char| freqs[char] += 1 unless char =~ /[\sa-zA-Z0-9]/ } | |
end | |
end | |
freqs.sort_by {|_,v| v}.reverse.each do |char, freq| | |
puts "#{char} #{freq}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Github:
My src dir: