This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.php
88 lines (78 loc) · 2.64 KB
/
json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
require_once(dirname(__FILE__) . '/padroes.php');
require_once(dirname(__FILE__) . '/vendor/autoload.php');
setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
if (PHP_SAPI !== 'cli') {
http_response_code(401);
exit;
}
if (is_dir(ANO) === false) {
mkdir(ANO);
}
if (is_dir(ANO . '/json') === false) {
mkdir(ANO . '/json');
}
if (is_dir(ANO . '/csv') === false) {
mkdir(ANO . '/csv');
}
$stream_context_create = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
),
);
$dados_abertos = file_get_contents('https://dadosabertos.tse.jus.br/dataset/candidatos-' . ANO, false, stream_context_create($stream_context_create));
if ($dados_abertos === FALSE) {
http_response_code(404);
} else {
$dom = new DOMDocument();
libxml_use_internal_errors(TRUE);
$dom->loadHTML($dados_abertos);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$file = 'https://cdn.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_' . ANO . '.zip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36');
$consulta_candidato = curl_exec($ch);
curl_close($ch);
if ($consulta_candidato === FALSE) {
echo 'Erro ao baixar o ZIP' . PHP_EOL;
die();
} else {
file_put_contents(ANO . '/tse.zip', $consulta_candidato);
$zip = new ZipArchive;
if ($zip->open(ANO . '/tse.zip') === TRUE) {
$zip->extractTo(ANO . '/csv/');
$zip->close();
echo 'Arquivos extraidos' . PHP_EOL;
unlink(ANO . '/tse.zip');
unlink(ANO . '/csv/leiame.pdf');
unlink(ANO . '/csv/consulta_cand_' . ANO . '_BRASIL.csv');
} else {
echo 'Erro ao extrair' . PHP_EOL;
die();
}
foreach (ESTADOS as $uf => $state) {
echo 'UF: ' . strtoupper($uf) . PHP_EOL;
unlink(ANO . '/csv/'. $uf . '.csv');
$candidatos = ANO . '/csv/consulta_cand_' . ANO . '_' . strtoupper($uf) . '.csv';
rename($candidatos, ANO . '/csv/'. $uf . '.csv');
$candidatos = ANO . '/csv/'. $uf . '.csv';
if (file_exists($candidatos)) {
$candidatos = mb_convert_encoding(file_get_contents($candidatos), 'UTF-8', 'US-ASCII');
$csv = new \ParseCsv\Csv();
$csv->auto($candidatos);
$json = json_encode($csv->data, JSON_UNESCAPED_UNICODE);
file_put_contents(ANO . '/json/' . $uf . '.json', $json);
} else {
echo 'Erro ao processar CSV de ' . strtoupper($uf) . PHP_EOL;
die();
}
}
}
}