Skip to content

Instantly share code, notes, and snippets.

@axd
Last active March 7, 2018 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axd/c017872982f91555445e71321d5c49b7 to your computer and use it in GitHub Desktop.
Save axd/c017872982f91555445e71321d5c49b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/**
* TSE Downloader
* Copyright (C) 2018
*
* Script para obtener todas las actas (imágenes) de las diferentes JRV
* para elección de diputados de la Asamblea Legislativa 2018.
* Utilización:
* tse.php departamentos -- Genera estructura base para la descarga.
* tse.php municipios -- Descarga y genera estructura interna de los diferentes municipios.
* tse.php actas -- Descarga las actas de cada JRV basado en la estructura de municipios y departamentos.
*
* 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.
*
* 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 <https://www.gnu.org/licenses/>.
**/
/* Declaración de algunas funciones necesarias */
function cloud_file_exists ($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
function save_file($url, $path){
if(file_exists($path)){
return;
}
if(cloud_file_exists($url)){
if(!file_exists($path)){
if(!file_put_contents($path, file_get_contents($url))){
echo "No se pudo guardar archivo. Saltando... \n ";
return;
}
}
return true;
}
else {
$filename = explode('/', $url);
echo "\nArchivo \"" . end($filename) ."\"no existe en el sitio del TSE... Saltando\n";
return;
}
}
function prgr($done, $total) {
$perc = floor(($done / $total) * 100);
$left = 100 - $perc;
$write = sprintf("\033[0G\033[2K[%'={$perc}s>%-{$left}s] - $perc%% - $done/$total", "", "");
fwrite(STDERR, $write);
}
/* global vars */
$folder = './json/';
$url_tse = 'http://elecciones2018.tse.gob.sv/';
if( !isset($argv[1]) || ($argv[1] != 'departamentos' && $argv[1] != 'municipios' && $argv[1] != 'actas' )) {
echo "\033[31m No existe esa opción, las opciones son: \n
" . basename(__FILE__) . " departamentos
" . basename(__FILE__) . " municipios
" . basename(__FILE__) . " actas\033[0m \n";
exit(1);
}
if ( $argv[1] === 'departamentos' ) {
$json = '{"customCode":"0000","categoryName":"Pais","url":"data/navigation/0/1/1.json","results":"data/results/r/0/1/","level":1,"name":"EL SALVADOR","subRegions":{"0100":{"customCode":"0100","name":"SAN SALVADOR","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/2/2.json","results":"data/results/r/0/2/","expected":{"1":2507,"2":2507,"3":2507,"4":2507,"5":2507,"6":2507}},"0200":{"customCode":"0200","name":"SANTA ANA","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/22/22.json","results":"data/results/r/0/22/","expected":{"1":851,"2":851,"3":851,"4":851,"5":851,"6":851}},"0300":{"customCode":"0300","name":"SAN MIGUEL","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/36/36.json","results":"data/results/r/0/36/","expected":{"1":748,"2":748,"3":748,"4":748,"5":748,"6":748}},"0400":{"customCode":"0400","name":"LA LIBERTAD","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/57/57.json","results":"data/results/r/0/57/","expected":{"1":1084,"2":1084,"3":1084,"4":1084,"5":1084,"6":1084}},"0500":{"customCode":"0500","name":"USULUTAN","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/80/80.json","results":"data/results/r/0/80/","expected":{"1":561,"2":561,"3":561,"4":561,"5":561,"6":561}},"0600":{"customCode":"0600","name":"SONSONATE","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/104/104.json","results":"data/results/r/0/104/","expected":{"1":680,"2":680,"3":680,"4":680,"5":680,"6":680}},"0700":{"customCode":"0700","name":"LA UNION","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/121/121.json","results":"data/results/r/0/121/","expected":{"1":450,"2":450,"3":450,"4":450,"5":450,"6":450}},"0800":{"customCode":"0800","name":"LA PAZ","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/140/140.json","results":"data/results/r/0/140/","expected":{"1":494,"2":494,"3":494,"4":494,"5":494,"6":494}},"0900":{"customCode":"0900","name":"CHALATENANGO","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/163/163.json","results":"data/results/r/0/163/","expected":{"1":352,"2":352,"3":352,"4":352,"5":352,"6":352}},"1000":{"customCode":"1000","name":"CUSCATLAN","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/197/197.json","results":"data/results/r/0/197/","expected":{"1":365,"2":365,"3":365,"4":365,"5":365,"6":365}},"1100":{"customCode":"1100","name":"AHUACHAPAN","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/214/214.json","results":"data/results/r/0/214/","expected":{"1":495,"2":495,"3":495,"4":495,"5":495,"6":495}},"1200":{"customCode":"1200","name":"MORAZAN","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/227/227.json","results":"data/results/r/0/227/","expected":{"1":311,"2":311,"3":311,"4":311,"5":311,"6":311}},"1300":{"customCode":"1300","name":"SAN VICENTE","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/254/254.json","results":"data/results/r/0/254/","expected":{"1":268,"2":268,"3":268,"4":268,"5":268,"6":268}},"1400":{"customCode":"1400","name":"CABA&Ntilde;AS","level":2,"parentName":"EL SALVADOR","parentCustomCode":"0000","categoryName":"Departamento","parentCategoryName":"Pais","url":"data/navigation/0/268/268.json","results":"data/results/r/0/268/","expected":{"1":256,"2":256,"3":256,"4":256,"5":256,"6":256}}},"pollingPlaces":{}}';
$json = (json_decode($json));
/* Obtener departamentos y sus respectivos archivos json */
echo "Creando estructura de departamentos... \n";
foreach($json->subRegions as $key => $subregion){
/* Let's save the JSON´s */
$folder_d_name = html_entity_decode($subregion->name);
if (!file_exists($folder . $folder_d_name)){
if(!@mkdir($folder . $folder_d_name, 0777, true)){
echo "No se pudo crear carpeta $folder_d_name\n"; exit(1);
}
echo "Creando carpeta: $folder_d_name\n";
}
echo "Guardando JSON $key en: $folder_d_name\n";
save_file($url_tse.$subregion->url, $folder . $folder_d_name . '/n-' . $key . '.json');
}
}
/* Obtener municipios y sus respectivos archivos json */
if ( $argv[1] === 'municipios' ){
$files = glob($folder . '*/*.json');
if(!$files || count($files) == 0){
echo "No existen archivos para leer. Revise si ha ejecutado 'tse.php departamentos' antes.\n";
exit(1);
}
foreach ($files as $file) {
echo "Leyendo archivo JSON $file para generar estructura de municipios \n";
$json = json_decode(file_get_contents($file));
foreach ($json->subRegions as $key => $value) {
$folder_m_name = html_entity_decode($value->name);
$folder_d_name = html_entity_decode($value->parentName);
if (!file_exists($folder . $folder_d_name .'/' . $folder_m_name)) {
echo "Creando carpeta $folder_m_name en $folder_d_name \n";
@mkdir($folder . '/'. $folder_d_name .'/' . $folder_m_name, 0777, true);
}
save_file($url_tse.$value->url, $folder . $folder_d_name .'/' . $folder_m_name . '/n-' . $key . '.json');
}
}
}
/* Obtener información de cada json por municipio */
if ( $argv[1] === 'actas' ){
echo "Obteniendo archivos JSON de cada municipio\n";
$files = glob($folder . '*/*/*.json');
if(!$files || count($files) == 0){
echo "No existen archivos para leer. Revise si ha ejecutado 'tse.php municipios' antes.\n";
exit(1);
}
foreach ($files as $file) {
$json = json_decode(file_get_contents($file));
$path = explode('/', $file);
array_pop($path);
$depa = end($path);
$path = implode('/', $path);
echo "Creando carpeta de actas en $depa\n";
if (!file_exists($path .'/actas')) @mkdir($path . '/actas', 0777, true);
foreach($json->pollingPlaces as $place){
$folder_name = html_entity_decode($place->name);
echo "Creando carpeta ". $folder_name . "\n";
if (!file_exists($path .'/actas/' . $folder_name)) @mkdir($path . '/actas/'. $folder_name, 0777, true);
$arrays = [];
foreach($place->tallies as $jrv){
foreach($jrv as $child_jrv){ $arrays[] = $child_jrv; }
}
$info = call_user_func_array('array_merge', array_unique($arrays, SORT_REGULAR));
echo "Esto puede tardar varios segundos, dependiendo tu velocidad a internet...\n";
for($i=0;$i<count($info);$i++){
echo "Guardando actas de JRV: #$info[$i]...\n";
if (!file_exists($path .'/actas/' . $folder_name . '/' . $info[$i])){
if(!@mkdir($path . '/actas/'. $folder_name . '/' . $info[$i], 0777, true)){
echo "\033[31mNo se pudo crear carpeta $info[$i]. Deteniendo... \033[0m \n"; exit(1);
}
}
prgr(0,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A.png');
prgr(1,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A1.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A1.png');
prgr(2,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A2.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A2.png');
prgr(3,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A3.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A3.png');
prgr(4,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A4.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A4.png');
prgr(5,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A5.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A5.png');
prgr(6,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A6.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A6.png');
prgr(7,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A7.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A7.png');
prgr(8,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-A8.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-A8.png');
prgr(9,10);
save_file($url_tse . 'cert/'.$info[$i].'-EDAL-B.png', $path . '/actas/' . $folder_name . '/' . $info[$i] .'/'. $info[$i] . '-EDAL-B.png');
prgr(10,10);
print("\n");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment