Last active
June 4, 2016 13:00
-
-
Save caarlos0/88ec017ad687c883581a to your computer and use it in GitHub Desktop.
My Ruby script to list the top-3 president candidates in Brazilian 2014 elections. Updated with second round data as well. http://carlosbecker.com/posts/elections/
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
require 'net/http' | |
require 'uri' | |
require 'json' | |
uri = URI('http://divulga.tse.jus.br/2014/divulgacao/oficial/143/dadosdivweb/br/br-0001-e001431-w.js') | |
class String; def percent_of(n) "#{(self.to_f / n.to_f * 100.0).round(2)}%"; end; end | |
begin | |
data = JSON(Net::HTTP.get_response(uri).body) | |
system('clear') | |
puts "\n\n----\n#{data['ht']} - #{data['ea'].percent_of(data['e'])} dos votos apurados\n----" | |
data['cand'].take(3).each do |candidate| | |
puts "[#{candidate['n']}] #{candidate['nm']} - #{candidate['v'].percent_of(data['vv'])}" | |
end | |
end while sleep(60) |
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
require 'net/http' | |
require 'uri' | |
require 'json' | |
uri = URI('http://divulga.tse.jus.br/2014/divulgacao/oficial/144/dadosdivweb/br/br-0001-e001441-w.js') | |
class String; def percent_of(n) "#{(self.to_f / n.to_f * 100.0).round(2)}%"; end; end | |
begin | |
data = JSON(Net::HTTP.get_response(uri).body) | |
system('clear') | |
puts "\n\n----\n#{data['ht']} - #{data['ea'].percent_of(data['e'])} dos votos apurados\n----" | |
data['cand'].each do |candidate| | |
puts "[#{candidate['n']}] #{candidate['nm']} - #{candidate['v'].percent_of(data['vv'])}" | |
end | |
end while sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cara, haja empenho pra fazer algo assim kkkk
Ou foi fácil fazer?