Skip to content

Instantly share code, notes, and snippets.

@mxgxw
Forked from anonymous/extract.js
Created November 19, 2015 22:52
Show Gist options
  • Save mxgxw/e3898b286a05450667b1 to your computer and use it in GitHub Desktop.
Save mxgxw/e3898b286a05450667b1 to your computer and use it in GitHub Desktop.
Extractor de Resultados Elecciones 2015 - El Salvador
/*
Extractor de Resultados Elecciones 2015 - El Salvador
Copyright (C) 2015 Mario Gómez (mario.gomez*_at_*teubi.co)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* Extractor de resultados de elecciones 2015 de Concejos Municipales
* por Municipio en El Salvador
* Este código, ejecutado desde la página:
* http://www.tse.gob.sv/resultados2015/concejos/
* Extrae la tabla final de ganadores por municipalidad y la adiciona
* al final de la página y puede ser copiada a Excel u otro formato.
*/
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
$("body").append('<table id="textOut"><tr><th>Departamento</th><th>Municipio</th><th>Ganador</th></tr></table>');
$('#accordion > div').each(function (idx,obj) {
pattern = /(.+) \(/i;
var dpto = pattern.exec($(obj).find('div a').html())[1];
municipalities = $(obj).find('li a').each(function (idx,obj) {
$.ajax({
type: 'GET',
url: "http://www.tse.gob.sv/resultados2015/concejos/"+$(obj).attr("href"),
data: {
key: "value"
},
dataType: "text",
success: function (data){
pattern = /([A-Z ]+)<\/a><\/li> <\/ul>/i;
var municipio = pattern.exec(data)[1];
pattern = /"dataProvider": \[(.*), \],/i;
partidos = JSON.parse('['+pattern.exec(data)[1]+']');
var maxVotes = 0;
var winner = "";
for(i=0;i<partidos.length;i++) {
if(partidos[i].votos>maxVotes) {
winner = partidos[i].partido
maxVotes = partidos[i].votos
}
}
$('#textOut').append("<tr><td>"+dpto+"</td><td>"+municipio+"</td>,<td>"+winner+"</td></tr>");
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment