Skip to content

Instantly share code, notes, and snippets.

@heukirne
Last active August 29, 2015 14:07
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 heukirne/ff94a57e1bfab495bc84 to your computer and use it in GitHub Desktop.
Save heukirne/ff94a57e1bfab495bc84 to your computer and use it in GitHub Desktop.
Candidates Enrichment
<?php include('header.php'); ?>
<div class="jumbotron">
<h1>Candidatos Stats</h1>
<p class="lead">Estatísticas dos Candidatos</br></p>
</div>
<?php
class Connect{
protected $url_acesso = 'http://api.transparencia.org.br/api/v1/';
protected $app_token = 'TzTnQhwxCWmY';
private function sendRequest($getCat){
$safe_filename = './cache/'.preg_replace('/[^0-9a-z\.\_\-]/i','', strtolower($getCat));
if (!file_exists($safe_filename)) {
$ch = curl_init($this->url_acesso.$getCat);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'App-Token: '.$this->app_token,
'Content-Type: application/json',
'Accept: application/json'));
$result = json_decode(curl_exec($ch));
curl_close($ch);
file_put_contents($safe_filename, serialize($result));
}
return unserialize(file_get_contents($safe_filename));
}
public function getAllCandidates($state,$part){
$url = 'candidatos?estado='.$state.'&partido='.$part;
return $this->sendRequest($url);
}
public function getAllBens($id){
$url = "candidatos/$id/bens";
return $this->sendRequest($url);
}
public function getAllPartidos(){
$url = "partidos";
return $this->sendRequest($url);
}
public function getAllStates(){
$url = "estados";
return $this->sendRequest($url);
}
}
?>
<?php
$conn = new Connect();
$parts = $conn->getAllPartidos();
foreach($parts as $part) {
$cand[$part->partidoId] = $conn->getAllCandidates('RS',$part->partidoId);
foreach($cand[$part->partidoId] as $candidate) {
$bens[$candidate->id]['raw'] = $conn->getAllBens($candidate->id);
$bens[$candidate->id]['anos'] = array();
foreach($bens[$candidate->id]['raw'] as $bem) {
if (!isset($bens[$candidate->id]['anos'][$bem->ano])) $bens[$candidate->id]['anos'][$bem->ano] = 0;
$bens[$candidate->id]['anos'][$bem->ano] += $bem->montante;
}
if (count($bens[$candidate->id]['anos']) > 1) {
ksort($bens[$candidate->id]['anos']);
$old = array_shift($bens[$candidate->id]['anos']);
$new = array_pop($bens[$candidate->id]['anos']);
$enrich[$new - $old] = "{$candidate->apelido} - {$part->sigla}";
}
}
}
krsort($enrich);
?>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Enriquecimento</th>
<th>Candidato</th>
</tr>
</thead>
<tbody>
<?php
foreach($enrich as $rich => $name) {
echo "<tr>
<td></td>
<td>R$ ".number_format($rich, 2, ',', '.')."</td>
<td>$name</td>
</tr>";
}
?>
</tbody>
</table>
Link: <a href="http://henrique.gemeos.org/enrich/">http://henrique.gemeos.org/enrich/</a> </br>
Fonte: <a href="http://dev.transparencia.org.br/api-portal/">http://dev.transparencia.org.br/api-portal/</a> </br>
Gist: <a href="https://gist.github.com/heukirne/ff94a57e1bfab495bc84">https://gist.github.com/heukirne/ff94a57e1bfab495bc84</a>
<?php include('footer.php'); ?>
@icaromh
Copy link

icaromh commented Oct 7, 2014

@heukirne só ta quebrando o encode de alguns candidatos. Tirando isso ( que é relevante ) ta muito do caralho 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment