Skip to content

Instantly share code, notes, and snippets.

@JaciBrunning
Last active March 21, 2018 07:20
Show Gist options
  • Select an option

  • Save JaciBrunning/e77b477eb79ba4b487c7b592eb203673 to your computer and use it in GitHub Desktop.

Select an option

Save JaciBrunning/e77b477eb79ba4b487c7b592eb203673 to your computer and use it in GitHub Desktop.
require 'json'
require 'open-uri'
require 'digest'
require 'fileutils'
KEY = "g3t_uR-0wN-K3y"
CACHEFILE = '_cache'
def query path
JSON.parse open("http://www.thebluealliance.com/api/v3/#{path}?X-TBA-Auth-Key=#{KEY}").read
end
def query_cached path
cc = Digest::MD5.hexdigest(path)
cf = "#{CACHEFILE}/#{cc}"
if File.exist?(cf)
eval File.read(cf)
else
r = query(path)
File.write(cf, r)
r
end
end
FileUtils.mkdir_p CACHEFILE
LEVELS = {
qm: 'Quals',
of: 'Octos',
qf: 'Quarters',
sf: 'Semis',
f: 'Finals'
}
perspectives = {}
outdata = [['event', 'match_level', 'match_key', 'week', 'red_plates', 'blue_plates']]
query("events/2018").each do |event|
key = event['key']
query_cached("event/#{key}/matches").each do |x|
v = x['score_breakdown']['red']
pers = "#{LEVELS[x['comp_level'].to_sym]}"
perspectives[pers] ||= { variations: {} }
gd = v['tba_gameData']
perspectives[pers][:variations][gd] ||= 0
perspectives[pers][:variations][gd] += 1
outdata << [key, LEVELS[x['comp_level'].to_sym], x['key'], event['week'], gd, gd]
end
end
puts "\t PLATE REPORT"
puts "============================="
puts
perspectives.each do |perspective, v|
puts "Perspective: #{perspective}"
total = v[:variations].map { |k, v| v }.inject(:+)
v[:variations].sort.each do |variation, count|
puts "\t #{variation}: #{count} (#{(count.to_f / total * 100).round(1)}%)"
end
puts "\t ---------------"
puts "\t Total: #{total}"
puts
end
File.write('plate_dump.csv', outdata.map { |x| x.join(',') }.join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment