Last active
August 29, 2015 13:57
-
-
Save jagbolanos/9478994 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
#!/usr/bin/env ruby | |
require "net/http" | |
require 'open-uri' | |
def download(url, acta) | |
File.open("presidente/#{acta}", "wb") do |saved_file| | |
# the following "open" is provided by open-uri | |
open(url, 'rb') do |read_file| | |
saved_file.write(read_file.read) | |
end | |
end | |
end | |
#http://elecciones2014.tse.gob.sv/actas/03/04/08/PRE03040804305.pdf | |
acta = 1 | |
departamento = 1 | |
municipio = 1 | |
instituto = 1 | |
available = 0 | |
u_instituto = false | |
u_municipio = false | |
u_departamento = false | |
while acta <= 10445 && departamento < 15 | |
actatext = sprintf( "%05d", acta ) | |
departamentotext = sprintf( "%02d", departamento ) | |
municipiotext = sprintf( "%02d", municipio ) | |
institutotext = sprintf( "%02d", instituto ) | |
actaurl = "http://elecciones2014.tse.gob.sv/actas/#{departamentotext}/#{municipiotext}/#{institutotext}/PRE#{departamentotext}#{municipiotext}#{institutotext}#{actatext}.pdf" | |
actafile = "#{departamentotext}#{municipiotext}#{institutotext}#{actatext}.pdf" | |
url = URI.parse(actaurl) | |
req = Net::HTTP.new(url.host, url.port) | |
res = req.request_head(url.path) | |
if res.code == "200" then | |
available = available + 1 | |
download(actaurl, actafile) | |
acta = acta + 1 | |
u_instituto = false | |
u_municipio = false | |
u_departamento = false | |
else | |
if !u_instituto then | |
u_instituto = true | |
instituto = instituto + 1 | |
else | |
if !u_municipio then | |
u_municipio = true | |
municipio = municipio + 1 | |
instituto = 1 | |
else | |
if !u_departamento then | |
u_departamento = true | |
departamento = departamento + 1 | |
instituto = 1 | |
municipio = 1 | |
else | |
puts "FAILED AT: #{actafile}" | |
departamento = 16 | |
end | |
end | |
end | |
end | |
end | |
puts "Actas disponibles #{available}" | |
puts "DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment