Last active
December 16, 2015 11:08
-
-
Save matiasinsaurralde/5424881 to your computer and use it in GitHub Desktop.
script para obtener los datos del padrón
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 'nokogiri' | |
require 'net/http' | |
require 'uri' | |
require 'cgi' | |
def data_for(id) | |
uri, nice_data = URI.parse('http://tsje.gov.py/dinamics/app/consulta-al-padron.php'), {} | |
http = Net::HTTP.new(uri.host, uri.port); req = Net::HTTP::Post.new(uri.path); req['Host'] = 'tsje.gov.py'; req['Referer'] = 'http://tsje.gov.py/'; req['X-Requested-With'] = 'XMLHttpRequest'; req.set_form_data( { 'cedula' => id }); res = http.request(req); raw_html = res.body; span6_count = 0 | |
Nokogiri::HTML(raw_html).css('div').each do |div| | |
if !div.to_s.include?('<strong>') | |
case div['class'] | |
when 'span7' | |
nice_data.store( :nombre, div.text ) | |
when 'span2' | |
nice_data.store( :sexo, div.text ) | |
when 'span3' | |
nice_data.store( :nacionalidad, div.text ) | |
when 'span6' | |
case span6_count | |
when 0 | |
nice_data.store( :departamento, div.text ) | |
when 1 | |
nice_data.store( :distrito, div.text ) | |
when 2 | |
nice_data.store( :zona, div.text ) | |
when 3 | |
nice_data.store( :local, div.text.strip ) | |
when 4 | |
nice_data.store( :mesa, div.text.to_i ) | |
when 5 | |
nice_data.store( :orden, div.text.to_i ) | |
end | |
span6_count += 1 | |
end | |
end | |
end | |
nice_data.store( :coordenadas, []); raw_html.match(/LatLng\((.*),(.*)\);/).to_a[1,2].each do |crd|; nice_data[:coordenadas] << crd.strip.to_f; end | |
return nice_data | |
end | |
## p data_for(12345678) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment