Last active
April 16, 2017 15:14
-
-
Save vitormattos/02e70f208b99b636aef9 to your computer and use it in GitHub Desktop.
Importação de notas CEDERJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use ProgressBar\Manager; | |
require 'vendor/autoload.php'; | |
$dom = new DOMDocument(); | |
@$dom->loadHTML(file_get_contents('notas.html')); | |
$domxpath = new \DOMXPath($dom); | |
$list = $domxpath->query("//div[contains(@class, 'h5')]"); | |
$db = new \PDO('mysql:host=localhost;dbname=cederj', 'usuario', 'senha'); | |
$db->exec('DROP TABLE IF EXISTS notas;'); | |
$db->exec("CREATE TABLE `notas` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`nome` varchar(255) DEFAULT NULL, | |
`curso` varchar(255) DEFAULT NULL, | |
`polo` varchar(255) DEFAULT NULL, | |
`instituicao` varchar(255) DEFAULT NULL, | |
`inscricao` varchar(255) DEFAULT NULL, | |
`nota` float DEFAULT NULL, | |
`situacao` varchar(255) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); | |
$progressBar = new Manager(0, $list->length); | |
$rows = array(); | |
foreach($list as $i => $div) { | |
$progressBar->update($i); | |
$class = $div->getAttribute('class'); | |
preg_match('/(x[3,5,9,a,8])/', $div->getAttribute('class'), $class); | |
if(!isset($class[1])) continue; | |
switch($class[1]) { | |
case 'x3'://nome | |
$reg['nome'] = iconv("UTF-8", "ISO-8859-1", $div->nodeValue); | |
break; | |
case 'x5'://curso - polo (instituicao) | |
preg_match('/^(?P<curso>.*) - (?P<polo>.*) \((?P<instituicao>.*)\)$/', $div->nodeValue, $tmp); | |
if(!isset($tmp['curso'])) { | |
$bug[$i] = $reg; | |
$bug['curso'] = $div->nodeValue; | |
$reg = array(); | |
continue; | |
} | |
$reg['curso'] = iconv("UTF-8", "ISO-8859-1", $tmp['curso']); | |
$reg['polo'] = iconv("UTF-8", "ISO-8859-1", $tmp['polo']); | |
$reg['instituicao'] = iconv("UTF-8", "ISO-8859-1", $tmp['instituicao']); | |
break; | |
case 'x9'://inscricao | |
$reg['inscricao'] = $div->nodeValue; | |
break; | |
case 'xa'://nota | |
$reg['nota'] = str_replace(',', '.', $div->nodeValue); | |
break; | |
case 'x8'://situacao | |
$reg['situacao'] = iconv("UTF-8", "ISO-8859-1", $div->nodeValue); | |
$rows = array_merge($rows, array_values($reg)); | |
$reg = array('nome'=>null,'curso'=>null,'polo'=>null,'instituicao'=>null,'inscricao'=>null,'nota'=>null,'situacao'=>null); | |
break; | |
} | |
if($rows && (count($rows)/7)%1000 == 0) { | |
$sql = | |
'INSERT INTO notas (nome, curso, polo, instituicao, inscricao, nota, situacao) '. | |
'VALUES '.trim(str_repeat('('.trim(str_repeat('?, ', 7), ', ')."),\n",count($rows)/7),",\n"); | |
$sth = $db->prepare($sql); | |
$sth->execute($rows); | |
$rows = array(); | |
} | |
} | |
if(count($rows)) { | |
$sth = $db->prepare( | |
'INSERT INTO notas (nome, curso, polo, instituicao, inscricao, nota, situacao) '. | |
'VALUES '.trim(str_repeat('('.trim(str_repeat('?, ', 7), ', ')."),\n",count($rows)/7),",\n") | |
); | |
$sth->execute($rows); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dom = new DOMDocument (); | |
@$dom->loadHTML ( file_get_contents ( 'notas.html' ) ); | |
$domxpath = new \DOMXPath ( $dom ); | |
$list = $domxpath->query ( "//div[contains(@class, 'h3')]" ); | |
$db = new \PDO ( 'pgsql:host=localhost;dbname=lupu', 'postgres', 'postgres' ); | |
$result = $db->exec ( 'DROP TABLE IF EXISTS notas;' ); | |
$db->exec ( "CREATE TABLE notas ( | |
id serial PRIMARY KEY, | |
nome varchar(255) DEFAULT NULL, | |
curso varchar(255) DEFAULT NULL, | |
polo varchar(255) DEFAULT NULL, | |
instituicao varchar(255) DEFAULT NULL, | |
inscricao varchar(255) DEFAULT NULL, | |
situacao varchar(255) DEFAULT NULL, | |
nota float DEFAULT NULL | |
);" ); | |
$rows = array (); | |
$reg = array ( | |
'nome' => null, | |
'curso' => null, | |
'polo' => null, | |
'instituicao' => null, | |
'inscricao' => null, | |
'nota' => null, | |
'situacao' => null | |
); | |
foreach ( $list as $i => $div ) { | |
$total = round ( ($i + 1) * 100 / $list->length, 4 ); | |
echo "$total \r"; | |
$class = $div->getAttribute ( 'class' ); | |
preg_match ( '/(x[3|1|8|7|9])/', $div->getAttribute ( 'class' ), $class ); | |
if (! isset ( $class [1] )) | |
continue; | |
switch ($class [1]) { | |
case "x3" : // nome | |
$reg ['nome'] = iconv ( "UTF-8", "ISO-8859-1", $div->nodeValue ); | |
break; | |
case "x1" : // curso - polo (instituicao) | |
preg_match ( '/^(?P<curso>.*) - (?P<polo>.*) \((?P<instituicao>.*)\)$/', $div->nodeValue, $tmp ); | |
if (! isset ( $tmp ['curso'] )) { | |
$bug [$i] = $reg; | |
$bug ['curso'] = $div->nodeValue; | |
$reg = array (); | |
continue; | |
} | |
$reg ['curso'] = iconv ( "UTF-8", "ISO-8859-1", $tmp ['curso'] ); | |
$reg ['polo'] = iconv ( "UTF-8", "ISO-8859-1", $tmp ['polo'] ); | |
$reg ['instituicao'] = iconv ( "UTF-8", "ISO-8859-1", $tmp ['instituicao'] ); | |
break; | |
case "x8" : // inscricao | |
$reg ['inscricao'] = iconv ( "UTF-8", "ISO-8859-1", $div->nodeValue ); | |
break; | |
case "x7" : // situacao | |
$reg ['situacao'] = iconv ( "UTF-8", "ISO-8859-1", $div->nodeValue ); | |
$rows = array_merge ( $rows, array_values ( $reg ) ); | |
$reg = array ( | |
'nome' => null, | |
'curso' => null, | |
'polo' => null, | |
'instituicao' => null, | |
'inscricao' => null, | |
'nota' => null, | |
'situacao' => null | |
); | |
break; | |
case "x9" : // nota | |
$reg ['nota'] = str_replace ( ',', '.', $div->nodeValue ); | |
break; | |
} | |
if ($rows && (count ( $rows ) / 7) % 1000 == 0) { | |
$sql = 'INSERT INTO notas (nome, curso, polo, instituicao, inscricao, nota, situacao) ' . 'VALUES ' . trim ( str_repeat ( '(' . trim ( str_repeat ( '?, ', 7 ), ', ' ) . "),\n", count ( $rows ) / 7 ), ",\n" ); | |
$sth = $db->prepare ( $sql ); | |
$sth->execute ( $rows ); | |
$rows = array (); | |
} | |
} | |
if (count ( $rows )) { | |
$sth = $db->prepare ( 'INSERT INTO notas (nome, curso, polo, instituicao, inscricao, nota, situacao) ' . 'VALUES ' . trim ( str_repeat ( '(' . trim ( str_repeat ( '?, ', 7 ), ', ' ) . "),\n", count ( $rows ) / 7 ), ",\n" ) ); | |
$sth->execute ( $rows ); | |
} | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crie o arquivo html convertendo o pdf para html com o comando:
pdf2htmlEX --embed cfijo input.pdf output.html