| 1 | package Tribunais::TRTES; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings 'all'; | ||
| 5 | |||
| 6 | use JSON; | ||
| 7 | use WWW::Mechanize; | ||
| 8 | use HTML::TreeBuilder::XPath; | ||
| 9 | use utf8; | ||
| 10 | |||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my $class = shift; | ||
| 15 | my $self = { | ||
| 16 | _tiponumeracao => shift, # antiga ou unificada | ||
| 17 | _numero => shift, | ||
| 18 | _ano => shift, | ||
| 19 | _vara => shift, | ||
| 20 | _recurso => shift, | ||
| 21 | _digito => shift, | ||
| 22 | }; | ||
| 23 | bless $self, $class; | ||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub sincroniza | ||
| 28 | { | ||
| 29 | |||
| 30 | my($self) = @_; | ||
| 31 | |||
| 32 | |||
| 33 | my $tiponumeracao=$self->{_tiponumeracao}; # antiga / unificada | ||
| 34 | my $numero=$self->{_numero}; | ||
| 35 | my $ano=$self->{_ano}; | ||
| 36 | my $vara=$self->{_vara}; | ||
| 37 | my $recurso=$self->{_recurso}; #00 | ||
| 38 | my $digito=$self->{_digito}; | ||
| 39 | |||
| 40 | |||
| 41 | my $mech = WWW::Mechanize->new; | ||
| 42 | $mech->agent_alias( 'Windows IE 6' ); | ||
| 43 | |||
| 44 | my $URI; | ||
| 45 | if($tiponumeracao eq "antiga") | ||
| 46 | { | ||
| 47 | #numero antigo | ||
| 48 | $URI="http://www.trtes.jus.br/sij/sijproc/AcompanhamentoProcessual/paginainicial.aspx?id=236&sequencial=$numero&ano=$ano&vara=$vara&recurso=$recurso"; | ||
| 49 | } | ||
| 50 | elsif($tiponumeracao eq "unificada") | ||
| 51 | { | ||
| 52 | #numero unificado | ||
| 53 | $URI="http://www.trtes.jus.br/sij/sijproc/AcompanhamentoProcessual/paginainicial.aspx?id=236&numero=$numero&digito=$digito&ano=$ano&origem=$vara"; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | $mech->post($URI); | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | my $titulo = $mech->title(); | ||
| 63 | my $html = $mech->content; | ||
| 64 | |||
| 65 | my $tree= HTML::TreeBuilder::XPath->new; | ||
| 66 | $tree->parse( $html ); | ||
| 67 | |||
| 68 | my $tabela = $tree->findnodes('//div[@class=\'ajax__tab_panel\'][2]//table[1]'); | ||
| 69 | my $tabela_com_dados_html = $tabela->[0]->as_HTML; # | ||
| 70 | |||
| 71 | |||
| 72 | my $table_rows = $tree->findnodes( '//div[@class=\'ajax__tab_panel\'][2]//table[1]/tr' ); | ||
| 73 | |||
| 74 | my $count = 0; | ||
| 75 | |||
| 76 | my @items; | ||
| 77 | foreach my $row ( $table_rows->get_nodelist ) | ||
| 78 | { | ||
| 79 | if ( $count > 1 ) | ||
| 80 | { | ||
| 81 | my $tree_tr = HTML::TreeBuilder::XPath->new; | ||
| 82 | $tree_tr->parse( $row->as_HTML ); | ||
| 83 | my $strdata=$tree_tr->findvalue( '//td[2]' ); | ||
| 84 | if(length($strdata)>5) | ||
| 85 | { | ||
| 86 | my $row_data = { | ||
| 87 | data => $tree_tr->findvalue( '//td[2]' ), | ||
| 88 | andamento => $tree_tr->findvalue( '//td[4]' ), | ||
| 89 | classe => $tree_tr->findvalue( '//td[1]' ), | ||
| 90 | local => $tree_tr->findvalue( '//td[3]' ), | ||
| 91 | }; | ||
| 92 | push(@items, $row_data); | ||
| 93 | } | ||
| 94 | $tree_tr->delete; | ||
| 95 | } | ||
| 96 | $count++; | ||
| 97 | } | ||
| 98 | |||
| 99 | my $json = { fases => \@items }; # cria hash com nome de $json, insere no hash uma key com nome de andamentos tendo o array @registros no value dessa key | ||
| 100 | |||
| 101 | my $string_json = to_json($json); # encoda o hash com nome de $json em uma string no formato JSON, e atribui à var $string_json | ||
| 102 | |||
| 103 | return $string_json; | ||
| 104 | } | ||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | __END__ |