Skip to content

Instantly share code, notes, and snippets.

@caarlos0
Last active June 4, 2016 13:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save caarlos0/88ec017ad687c883581a to your computer and use it in GitHub Desktop.
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/
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)
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)
@roberto-filho
Copy link

Cara, haja empenho pra fazer algo assim kkkk
Ou foi fácil fazer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment