line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Correios::CEP; |
2
|
2
|
|
|
2
|
|
121921
|
use strict; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
54
|
|
3
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
44
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1250
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
80075
|
|
|
2
|
|
|
|
|
95
|
|
6
|
2
|
|
|
2
|
|
1268
|
use JSON; |
|
2
|
|
|
|
|
17818
|
|
|
2
|
|
|
|
|
10
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = 1.044; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
1193
|
use Encode; |
|
2
|
|
|
|
|
24032
|
|
|
2
|
|
|
|
|
152
|
|
11
|
2
|
|
|
2
|
|
15
|
use utf8; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
14
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
2
|
|
|
2
|
1
|
160
|
my ( $class, $params ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $this = { |
17
|
|
|
|
|
|
|
_user_agent => defined $params->{user_agent} |
18
|
|
|
|
|
|
|
? $params->{user_agent} |
19
|
|
|
|
|
|
|
: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', |
20
|
|
|
|
|
|
|
_lwp_ua => undef, |
21
|
|
|
|
|
|
|
_lwp_options => $params->{lwp_options} || { timeout => 30 }, |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
_post_url => defined $params->{post_url} |
24
|
|
|
|
|
|
|
? $params->{post_url} |
25
|
|
|
|
|
|
|
: 'https://buscacepinter.correios.com.br/app/endereco/carrega-cep-endereco.php', |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
_post_content => defined $params->{post_content} |
28
|
|
|
|
|
|
|
? $params->{post_content} |
29
|
2
|
50
|
50
|
|
|
36
|
: 'pagina=%2Fapp%2Fendereco%2Findex.php&cepaux=&mensagem_alerta=&tipoCEP=ALL&endereco=' |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$this->{_lwp_options}{timeout} = $params->{timeout} |
33
|
2
|
100
|
|
|
|
9
|
if defined $params->{timeout}; |
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
|
|
8
|
return bless $this, $class; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub find { |
39
|
3
|
|
|
3
|
1
|
2646
|
my ( $this, $cep, $as_html_tree ) = @_; |
40
|
|
|
|
|
|
|
|
41
|
3
|
|
|
|
|
12
|
my @list_address = $this->_extractAddress( $cep, $as_html_tree ); |
42
|
3
|
50
|
|
|
|
25
|
$list_address[0]{address_count} = @list_address unless wantarray; |
43
|
|
|
|
|
|
|
|
44
|
3
|
50
|
|
|
|
19
|
return wantarray ? @list_address : $list_address[0]; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _extractAddress { |
48
|
3
|
|
|
3
|
|
9
|
my ( $this, $cep, $as_html_tree ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
3
|
|
|
|
|
8
|
my @result = (); |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
18
|
$cep =~ s/[^\d]//go; |
53
|
3
|
|
|
|
|
19
|
$cep = sprintf( '%08d', $cep ); |
54
|
|
|
|
|
|
|
|
55
|
3
|
50
|
33
|
|
|
33
|
if ( $cep =~ /^00/o || $cep =~ /(\d)\1{7}/ ) { |
56
|
0
|
|
|
|
|
0
|
$result[0]->{status} = "Error: Invalid CEP number ($cep)"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
3
|
100
|
|
|
|
15
|
if ( !defined $this->{_lwp_ua} ) { |
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
3
|
my $ua = LWP::UserAgent->new( %{ $this->{_lwp_options} } ); |
|
2
|
|
|
|
|
22
|
|
62
|
2
|
|
|
|
|
5786
|
$ua->agent( $this->{_user_agent} ); |
63
|
2
|
|
|
|
|
180
|
$ua->timeout( $this->{_lwp_options}{timeout} ); |
64
|
2
|
|
|
|
|
39
|
$this->{_lwp_ua} = $ua; |
65
|
|
|
|
|
|
|
} |
66
|
3
|
|
|
|
|
11
|
my $ua = $this->{_lwp_ua}; |
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
|
|
8
|
my $url = $this->{_post_url}; |
69
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
70
|
|
|
|
|
|
|
POST => $url, |
71
|
|
|
|
|
|
|
[ |
72
|
|
|
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
73
|
|
|
|
|
|
|
'Origin' => 'https://buscacepinter.correios.com.br', |
74
|
|
|
|
|
|
|
'Referer' => |
75
|
|
|
|
|
|
|
'https://buscacepinter.correios.com.br/app/endereco/index.php?t', |
76
|
|
|
|
|
|
|
], |
77
|
3
|
|
|
|
|
36
|
$this->{_post_content} . $cep |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
|
|
14533
|
eval { |
81
|
|
|
|
|
|
|
local $SIG{ALRM} = |
82
|
3
|
|
|
0
|
|
103
|
sub { die "Can't connect to server [alarm timeout]\n" }; |
|
0
|
|
|
|
|
0
|
|
83
|
3
|
|
|
|
|
38
|
alarm( $this->{_lwp_options}{timeout} + 1 ); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Pass request to the user agent and get a response back |
86
|
3
|
|
|
|
|
22
|
my $res = $ua->request($req); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Check the outcome of the response |
89
|
|
|
|
|
|
|
|
90
|
3
|
100
|
|
|
|
4160265
|
if ( $res->is_success ) { |
91
|
2
|
|
|
|
|
33
|
$this->_parseJSON( \@result, $res->content, $as_html_tree ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
1
|
|
|
|
|
13
|
$result[0]->{status} = "Error: " . $res->status_line; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
}; |
97
|
3
|
|
|
|
|
143
|
alarm(0); |
98
|
3
|
50
|
|
|
|
48
|
die $@ if ($@); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
3
|
50
|
|
|
|
17
|
return wantarray ? @result : $result[0]; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _parseJSON { |
105
|
2
|
|
|
2
|
|
36
|
my ( $this, $address_ref, $json, $as_html_tree ) = @_; |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
|
|
11
|
my $obj = from_json($json); |
108
|
|
|
|
|
|
|
|
109
|
2
|
50
|
|
|
|
116
|
for my $p ( @{ $obj->{dados} || [] } ) { |
|
2
|
|
|
|
|
10
|
|
110
|
|
|
|
|
|
|
|
111
|
2
|
100
|
|
|
|
6
|
if ($as_html_tree) { |
112
|
1
|
|
|
|
|
3
|
push( @$address_ref, $p ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
else { |
115
|
1
|
|
|
|
|
2
|
my $address = {}; |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
3
|
$address->{street} = $p->{logradouroDNEC}; |
118
|
1
|
|
|
|
|
3
|
$address->{neighborhood} = $p->{bairro}; |
119
|
|
|
|
|
|
|
$address->{cep} = |
120
|
1
|
|
|
|
|
5
|
substr( $p->{cep}, 0, 5 ) . '-' . substr( $p->{cep}, 5, 3 ); |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
3
|
$address->{location} = $p->{localidade}; |
123
|
1
|
|
|
|
|
3
|
$address->{uf} = $p->{uf}; |
124
|
|
|
|
|
|
|
|
125
|
1
|
|
|
|
|
2
|
$address->{status} = $p->{situacao}; |
126
|
|
|
|
|
|
|
|
127
|
1
|
|
|
|
|
1
|
$address->{raw} = $p; |
128
|
|
|
|
|
|
|
|
129
|
1
|
|
|
|
|
4
|
push( @$address_ref, $address ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
2
|
50
|
|
|
|
7
|
$address_ref->[0]->{status} = 'Error: Address not found' |
134
|
|
|
|
|
|
|
if ( !@$address_ref ); |
135
|
|
|
|
|
|
|
|
136
|
2
|
|
|
|
|
60
|
return 1; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |
140
|
|
|
|
|
|
|
__END__ |