ZipLookup.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ZipLookup/Address.pm ↑ Summary
1 #
2 # ZipLookup.pm
3 #
4 # Perl 5 module to standardize U.S. postal addresses by referencing
5 # the U.S. Postal Service's web site:
6 #
7 # http://www.usps.com/zip4/
8 #
9 # BE SURE TO READ, UNDERSTAND, AND ABIDE BY THE TERMS OF USE FOR THE
10 # USPS WEB SITE. LINKS ARE PROVIDED IN THE TERMS OF USE SECTION IN THE
11 # DOCUMENTATION OF THIS PROGRAM, WHICH MAY BE FOUND AT THE END OF THIS
12 # SOURCE CODE FILE.
13 #
14 # Copyright (C) 1999-2012 Gregor N. Purdy, Sr. All rights reserved.
15 # This program is free software. It is subject to the same license as Perl.
16 #
17 # [ $Id$ ]
18 #
19  
20 package Scrape::USPS::ZipLookup;
21  
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use encoding 'utf-8';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 our $VERSION = '2.6';
27  
28 use LWP::UserAgent; # To communicate with USPS and get HTML
29 use HTTP::Request::Common;
30 use HTML::TreeBuilder::XPath; # To parse HTML
31 use XML::XPathEngine; # To extract data
32  
33 use Scrape::USPS::ZipLookup::Address;
34  
35 my $start_url = 'https://tools.usps.com/go/ZipLookupAction!input.action?mode=0';
36  
37  
38 #
39 # new()
40 #
41  
42 sub new
43 {
44 my $class = shift;
45 my $self = bless {
46 VERBOSE => 0,
47 }, $class;
48  
49 return $self;
50 }
51  
52  
53 #
54 # verbose()
55 #
56  
57 sub verbose
58 {
59 my $self = shift;
60  
61 if (@_) {
62 $self->{VERBOSE} = $_[0];
63 return $_[0];
64 } else {
65 return $self->{VERBOSE};
66 }
67 }
68  
69  
70 #
71 # dump()
72 #
73  
74 sub dump
75 {
76 my $self = shift;
77 my ($response) = @_;
78  
79 my $request = $response->request;
80
81 print "-" x 79, "\n";
82 print "HTTP Request:\n";
83 $request->dump;
84
85 print "-" x 79, "\n";
86 print "HTTP Response:\n";
87 $response->dump;
88 }
89  
90 #
91 # std_inner()
92 #
93 # The inner portion of the process, so it can be shared by
94 # std_addr() and std_addrs().
95 #
96  
97 sub std_inner
98 {
99 my $self = shift;
100  
101 #
102 # Turn the input into an Address instance:
103 #
104  
105 my $addr = Scrape::USPS::ZipLookup::Address->new(@_);
106  
107 if ($self->verbose) {
108 print ' ', '_' x 77, ' ', "\n";
109 print '/', ' ' x 77, '\\', "\n";
110 $addr->dump("Input");
111 print "\n";
112 }
113  
114 my $response = undef;
115
116 #
117 # Submit the form to the USPS web server:
118 #
119 # Unless we are in verbose mode, we make the WWW::Mechanize user agent be
120 # quiet. At the time this was written [2003-01-28], it generates a warning
121 # about the "address" form field being read-only if its not in quiet mode.
122 #
123 # We set the form's Selection field to "1" to indicate that we are doing
124 # regular zip code lookup.
125 #
126  
127 my $ua = LWP::UserAgent->new(cookie_jar => { }); # We need a cookie jar for USPS to let is through
128 $response = $ua->get($start_url);
129
130 if ($self->verbose) {
131 $self->dump($response);
132 }
133  
134 my $query_url = 'https://tools.usps.com/go/ZipLookupResultsAction!input.action';
135 my $temp = POST $query_url, [
136 resultMode => '0',
137 companyName => '',
138 address1 => $addr->delivery_address // '',
139 address2 => '',
140 city => $addr->city // '',
141 state => $addr->state // '',
142 urbanCode => '',
143 postalCode => $addr->zip_code // '',
144 zip => ''
145 ];
146
147 $response = $ua->request($temp);
148
149 if ($self->verbose) {
150 $self->dump($response);
151 }
152  
153 my $content = $response->decoded_content;
154  
155 #
156 # Time to Parse:
157 #
158  
159 my @matches;
160  
161 my $tree = HTML::TreeBuilder::XPath->new();
162 $tree->parse($content);
163 my @html_matches = $tree->findnodes('//div[@class="data"]');
164  
165 my $xp = XML::XPathEngine->new();
166  
167 for my $node (@html_matches) {
168 # $node->dump();
169  
170 my $firm = undef;
171 my $address = undef;
172 my $city = undef;
173 my $state = undef;
174 my $zip4 = undef;
175 my $zip = undef;
176  
177 my $found;
178  
179 $found = $xp->find('p[@class="std-address"]/span[@class="address1 range"]', $node);
180 for my $x ($found->get_nodelist) {
181 $address = $x->as_trimmed_text();
182  
183 # my $firm_node = $xp->find('preceding-sibling::text()', $x);
184 # for my $y ($firm_node->get_nodelist) {
185 # $firm .= $y->as_trimmed_text();
186 # }
187  
188 last;
189 }
190  
191 $found = $xp->find('p[@class="std-address"]/span[@class="city range"]', $node);
192 for my $x ($found->get_nodelist) {
193 $city = $x->as_trimmed_text();
194 last;
195 }
196  
197 $found = $xp->find('p[@class="std-address"]/span[@class="state range"]', $node);
198 for my $x ($found->get_nodelist) {
199 $state = $x->as_trimmed_text();
200 last;
201 }
202  
203 $found = $xp->find('p[@class="std-address"]/span[@class="zip4"]', $node);
204 for my $x ($found->get_nodelist) {
205 $zip4 = $x->as_trimmed_text();
206 last;
207 }
208  
209 $found = $xp->find('p[@class="std-address"]/span[@class="zip"]', $node);
210 for my $x ($found->get_nodelist) {
211 $zip = $x->as_trimmed_text() . (defined($zip4) ? ('-' . $zip4) : '');
212 last;
213 }
214  
215 my %details;
216  
217 my $dts = $xp->find('div/dl[@class="details"]/dt', $node);
218 for my $dt ($dts->get_nodelist) {
219 my $key = $dt->as_trimmed_text();
220  
221 my $dds = $xp->find('following-sibling::dd[1]', $dt);
222  
223 for my $dd ($dds->get_nodelist) {
224 my $value = $dd->as_trimmed_text();
225 $details{$key} = $value;
226 }
227 }
228  
229 my $carrier_route = $details{'Carrier Route'};
230 my $county = $details{'County'};
231 my $delivery_point = $details{'Delivery Point Code'};
232 my $check_digit = $details{'Check Digit'};
233  
234 my $commercial_mail_receiving_agency = $details{'Commercial Mail Receiving Agency'};
235  
236 my $lac_indicator = $details{"LAC\x{2122}"};
237 my $elot_sequence = $details{"eLOT\x{2122}"};
238 my $elot_indicator = $details{'eLOT Ascending/Descending Indicator'};
239 my $record_type = $details{'Record Type Code'};
240 my $pmb_designator = $details{'PMB Designator'};
241 my $pmb_number = $details{'PMB Number'};
242 my $default_address = $details{'Default Flag'};
243 my $early_warning = $details{'EWS Flag'};
244 my $valid = $details{'DPV Confirmation Indicator'};
245  
246 if ($self->verbose) {
247 print("-" x 70, "\n");
248  
249 print "Firm: $firm\n" if defined $firm;
250  
251 print "Address: $address\n";
252 print "City: $city\n";
253 print "State: $state\n";
254 print "Zip: $zip\n";
255  
256 print "Carrier Route: $carrier_route\n" if defined $carrier_route;
257 print "County: $county\n" if defined $county;
258 print "Delivery Point: $delivery_point\n" if defined $delivery_point;
259 print "Check Digit: $check_digit\n" if defined $check_digit;
260 print "Commercial Mail Receiving Agency: $commercial_mail_receiving_agency\n" if defined $commercial_mail_receiving_agency;
261 print "LAC Indicator: $lac_indicator\n" if defined $lac_indicator;
262 print "eLOT Sequence: $elot_sequence\n" if defined $elot_sequence;
263 print "eLOT Indicator: $elot_indicator\n" if defined $elot_indicator;
264 print "Record Type: $record_type\n" if defined $record_type;
265 print "PMB Designator: $pmb_designator\n" if defined $pmb_designator;
266 print "PMB Number: $pmb_number\n" if defined $pmb_number;
267 print "Default Address: $default_address\n" if defined $default_address;
268 print "Early Warning: $early_warning\n" if defined $early_warning;
269 print "Valid: $valid\n" if defined $valid;
270  
271 print "\n";
272 }
273  
274 my $match = Scrape::USPS::ZipLookup::Address->new($address, $city, $state, $zip);
275  
276 $match->firm($firm);
277  
278 $match->carrier_route($carrier_route);
279 $match->county($county);
280 $match->delivery_point($delivery_point);
281 $match->check_digit($check_digit);
282 $match->commercial_mail_receiving_agency($commercial_mail_receiving_agency);
283 $match->lac_indicator($lac_indicator);
284 $match->elot_sequence($elot_sequence);
285 $match->elot_indicator($elot_indicator);
286 $match->record_type($record_type);
287 $match->pmb_designator($pmb_designator);
288 $match->pmb_number($pmb_number);
289 $match->default_address($default_address);
290 $match->early_warning($early_warning);
291 $match->valid($valid);
292  
293 push @matches, $match;
294 }
295  
296 print('\\', '_' x 77, '/', "\n") if $self->verbose;
297  
298 return @matches;
299 }
300  
301  
302 #
303 # std_addr()
304 #
305  
306 sub std_addr
307 {
308 my $self = shift;
309  
310 return $self->std_inner(@_);
311 }
312  
313  
314 #
315 # std_addrs()
316 #
317  
318 sub std_addrs
319 {
320 my $self = shift;
321  
322 my @result;
323  
324 foreach my $addr (@_) {
325 my @addr = $self->std_inner(@$addr);
326  
327 push @result, [ @addr ];
328 }
329  
330 return @result;
331 }
332  
333  
334 #
335 # trim()
336 #
337 # A purely internal utility subroutine.
338 #
339  
340 sub trim
341 {
342 my $string = shift;
343 $string =~ s/\x{a0}/ /sg; # Remove this odd character.
344 $string =~ s/^\s+//s; # Trim leading whitespace.
345 $string =~ s/\s+$//s; # Trim trailing whitespace.
346 $string =~ s/\s+/ /sg; # Coalesce interior whitespace.
347 return $string;
348 }
349  
350  
351 #
352 # Proper module termination:
353 #
354  
355 1;
356  
357 __END__
« ZipLookup/Address.pm ↑ Summary