Skip to content

Instantly share code, notes, and snippets.

@tomahock tomahock/parser.php
Last active Aug 11, 2017

Embed
What would you like to do?
Fogos.pt first PDF parser
<?php
/**
* Created by PhpStorm.
* User: tomahock
* Date: 10/08/15
* Time: 01:05
*/
require_once '../vendor/autoload.php';
require_once '../lib/DatStore.php';
require_once '../lib/NotificationStore.php';
$ionicHeaders = [
'verify' => false,
'allow_redirects' => true,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => ''
]
];
$settings = [
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => "",
];
$twitterUrl = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
const DEFAULT_URL = '';
const DEFAULT_TOKEN = '';
$firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
$users = json_decode($firebase->get('/users'));
switch (date('m')) {
case 1:
$month = 'Janeiro';
$abbr = 'JAN';
break;
case 2:
$month = 'Fevereiro';
$abbr = 'FEV';
break;
case 3:
$month = 'Marco';
$abbr = 'MAR';
break;
case 4:
$month = 'Abril';
$abbr = 'ABR';
break;
case 5:
$month = 'Maio';
$abbr = 'MAI';
break;
case 6:
$month = 'Junho';
$abbr = 'JUN';
break;
case 7:
$month = 'Julho';
$abbr = 'JUL';
break;
case 8:
$month = 'Agosto';
$abbr = 'AGO';
break;
case 9:
$month = 'Setembro';
$abbr = 'SET';
break;
case 10:
$month = 'Outubro';
$abbr = 'OUT';
break;
case 11:
$abbr = 'NOV';
$month = 'Novembro';
break;
case 12:
$month = 'Dezembro';
$abbr = 'DEZ';
break;
}
$year = date('Y');
$day = date('d');
$url = "http://www.prociv.pt/cnos/HAI/{$month}%20{$year}/Inc%C3%AAndios%20Rurais%20-%20Hist%C3%B3rico%20do%20Dia%20{$day}{$abbr}.pdf";
//file_put_contents("Tmpfile.pdf", fopen("http://www.prociv.pt/cnos/HAI/Agosto/Inc%C3%AAndios%20Rurais%20-%20Hist%C3%B3rico%20do%20Dia%2001SET.pdf", 'r'));
file_put_contents("Tmpfile.pdf", fopen($url, 'r'));
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile("Tmpfile.pdf");
$text = $pdf->getText();
$fires = array();
$fireTypes = array(
'MATO',
'POVOAMENTO',
'FLORESTAL',
'AGRÍCOLA',
);
$fireTypesAlt = array(
'Mato',
'Povoamento',
'Florestal',
'Agrícolas',
);
$status = array(
'CURSO' => 1,
'ALERTA' => 3,
'RESOLUÇÃO' => 1,
'CONCLUSÃO' => 0,
'ENCERRADA' => 0,
'TO' => 2
);
$statusColors = array(
' Curso' => 'FF0000',
'Em Curso' => 'FF0000',
'ALERTA' => 'FF9900',
' Resolução' => '3366FF',
' Conclusão' => '66FF33',
' Encerrada' => '4D4D4D',
' DESPACHO DE 1º ALERTA' => 'FF9900',
'TO' => '800000'
);
$statusColorsAlt = array(
' CURSO' => 'FF0000',
' EM CURSO' => 'FF0000',
' EM' => 'FF0000',
' ALERTA' => 'FF9900',
'ALERTA' => 'FF9900',
' EM RESOLUÇÃO' => '3366FF',
' CONCLUSÃO' => '66FF33',
' ENCERRADA' => '4D4D4D',
' DESPACHO DE 1º ALERTA' => 'FF9900',
'TO' => '800000'
);
//read file line by line
$lines = array();
foreach (preg_split("/((\r?\n)|(\r\n?))/", $text) as $line) {
$lines[] = $line;
if (preg_match('/^Informação válida/', $line)) {
echo 'set update' . PHP_EOL;
DataStore::setUpdate($line);
}
$fire = explode(' ', $line);
$total = count($fire) - 1;
if (preg_match('/^[0-9]/', $line) && $total > 7) {
$fire = explode(' ', $line);
$total = count($fire) - 1;
if (!empty($fire[$total])) {
$total = count($fire);
}
if (!DataStore::getItemById($fire[0])) {
$location = '';
for ($i = 3; $i <= count($fire); $i++) {
if (!in_array($fire[$i], $fireTypes)) {
$location .= ' ' . $fire[$i];
} else {
break;
}
}
$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&language=pt&key=&address=';
$url = $url . urlencode($location);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$gMaps = json_decode(curl_exec($curl));
$district = null;
$concelho = null;
if (!empty($gMaps->results)) {
foreach ($gMaps->results as $r) {
if ($district) {
continue;
}
foreach ($r->address_components as $r1) {
if (isset($r1->types[0]) && $r1->types[0] === 'administrative_area_level_2') {
$concelho = $r1->short_name;
}
if (isset($r1->types[0]) && $r1->types[0] === 'administrative_area_level_1') {
$district = $r1->short_name;
}
}
}
$point = array(
'id' => $fire[0],
'coords' => true,
'date' => $fire[1],
'hour' => $fire[2],
'location' => $location,
'aerial' => $fire[$total - 1],
'terrain' => $fire[$total - 2],
'man' => $fire[$total - 3],
'district' => $district,
'concelho' => $concelho,
'lat' => $gMaps->results[0]->geometry->location->lat,
'lng' => $gMaps->results[0]->geometry->location->lng,
'notification' => true
);
} else {
$point = array(
'id' => $fire[0],
'coords' => false,
'date' => $fire[1],
'hour' => $fire[2],
'location' => $location,
'aerial' => $fire[$total - 1],
'terrain' => $fire[$total - 2],
'man' => $fire[$total - 3],
'district' => $district,
'concelho' => $concelho,
'notification' => true
);
}
DataStore::save($point);
NotificationStore::create($district, $location);
foreach ($users as $u) {
if (isset($u->device)) {
if (isset($u->distritos)) {
foreach ($u->distritos as $d) {
if (preg_match('/' . $d->name . '/', $district) && $d->selected) {
if (isset($u->os) && $u->os == 'android') {
$client = new GuzzleHttp\Client();
$ionicHeaders['json'] = array(
"tokens" => [$u->device],
"profile" => "android",
"notification" => [
"title" => "Fogos.pt",
"message" => 'Novo incêndio em ' . $location,
"android" => array(
"data" => array(
"title" => "",
"message" => "",
"style" => "inbox",
"summaryText" => "Há %n% novos incêndios"
)
)
]
);
$res = $client->request('POST', 'https://api.ionic.io/push/notifications', $ionicHeaders);
echo 'Sending ' . $district . ' to ' . $u->uid . PHP_EOL;
} elseif (isset($u->os) && $u->os == 'ios') {
$ionicHeaders['json'] = array(
"tokens" => [$u->device],
"profile" => "iosprod",
"notification" => [
"title" => "Fogos.pt",
"message" => 'Novo incêndio em ' . $location,
'ios' => array(
'title' => 'Fogos.pt',
"message" => 'Novo incêndio em ' . $location,
)
]
);
$res = $client->request('POST', 'https://api.ionic.io/push/notifications', $ionicHeaders);
echo 'Sending ' . $district . ' to ' . $u->uid . PHP_EOL;
} else {
$client = new GuzzleHttp\Client();
$ionicHeaders['json'] = array(
"tokens" => [$u->device],
"profile" => "android",
"notification" => [
"title" => "Fogos.pt",
"message" => 'Novo incêndio em ' . $location,
"android" => array(
"data" => array(
"title" => "",
"message" => "",
"style" => "inbox",
"summaryText" => "Há %n% novos incêndios"
)
)
]
);
$res = $client->request('POST', 'https://api.ionic.io/push/notifications', $ionicHeaders);
echo 'Sending ' . $district . ' to ' . $u->uid . PHP_EOL;
}
}
}
} else {
echo 'User without districts: ' . json_encode($u) . PHP_EOL;
}
}
}
$postfields = [
'status' => 'Novo incêndio em' . $location . ' https://fogos.pt',
];
print_r($postfields);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($twitterUrl, $requestMethod)
->setPostfields($postfields)
->performRequest();
}
$statusOffset = $status[$fire[$total - 4]];
$currentStatus = '';
for ($i = 0; $i <= $statusOffset; $i++) {
if ($fire[$total - 4] == 'ALERTA' && $fire[$total - 5] !== '') {
$currentStatus = 'ALERTA';
break;
} else {
if (in_array($fire[$total - 4 - $statusOffset + $i], $fireTypes)) {
$currentStatus .= ' ' . $fire[$total - 3 - $statusOffset + $i];
} else {
$currentStatus .= ' ' . $fire[$total - 4 - $statusOffset + $i];
}
}
}
$statusColor = $statusColors[$currentStatus];
if (is_null($statusColor)) {
if ($currentStatus == ' DESPACHO DE 1º ALERTA') {
$currentStatus = 'ALERTA';
}
$statusColor = $statusColorsAlt[$currentStatus];
}
var_dump($currentStatus);
var_dump($statusColor);
$currentStatus = trim($currentStatus);
$data = array(
'aerial' => $fire[$total - 1],
'terrain' => $fire[$total - 2],
'man' => $fire[$total - 3],
'status' => $currentStatus,
'statusColor' => $statusColor
);
DataStore::updateById($fire[0], $data);
}
}
@DiogonGamerCodeAcademy

This comment has been minimized.

Copy link

DiogonGamerCodeAcademy commented Aug 11, 2017

O primeiro código PHP do maior site sobre incêndios portugueses!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.