| 1 | |||
| 2 | package WWW::Scraper::ZIPplus4; | ||
| 3 | |||
| 4 | |||
| 5 | ##################################################################### | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | @EXPORT = qw(); | ||
| 9 | @EXPORT_OK = qw(trimTags); | ||
| 10 | @ISA = qw(WWW::Scraper Exporter); | ||
| 11 | $VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/); | ||
| 12 | |||
| 13 | use Carp (); | ||
| 14 | use WWW::Scraper(qw(3.03 generic_option addURL trimTags trimLFs trimComments)); | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | my $scraperRequest = | ||
| 19 | { | ||
| 20 | 'type' => 'GET' | ||
| 21 | ,'formNameOrNumber' => '1' | ||
| 22 | ,'submitButton' => 'Submit' | ||
| 23 | |||
| 24 | # This is the basic URL on which to get the form to build the query. | ||
| 25 | # ,'url' => 'http://www.usps.com/ncsc/lookups/lookup_zip+4.html' | ||
| 26 | # _OLD ,'url' => 'http://www.usps.com/cgi-bin/zip4/zip4inq2?' | ||
| 27 | ,'url' => 'http://www.usps.com/zip4/zip4_response.jsp?' | ||
| 28 | # specify defaults, by native field names | ||
| 29 | # ,'nativeQuery' => 'Delivery+Address' | ||
| 30 | ,'nativeDefaults' => { | ||
| 31 | 'Selection' => '1' | ||
| 32 | ,'urbanization' => '' | ||
| 33 | ,'firm' => '' | ||
| 34 | ,'address2' => '' | ||
| 35 | ,'Submit.x' => '1' | ||
| 36 | ,'Submit.y' => '1' | ||
| 37 | } | ||
| 38 | |||
| 39 | # specify translations from canonical fields to native fields | ||
| 40 | ,'defaultRequestClass' => 'ZIPplus4' | ||
| 41 | ,'fieldTranslations' => | ||
| 42 | { | ||
| 43 | '*' => | ||
| 44 | { | ||
| 45 | 'City' => 'city' | ||
| 46 | ,'State' => 'state' | ||
| 47 | ,'ZipCode' => 'zipcode' | ||
| 48 | ,'DeliveryAddress' => 'address' | ||
| 49 | ,'address1' => 'address' # Weird but true! | ||
| 50 | ,'*' => '*' # Thanks to Klemens Schmid (klemens.schmid@gmx.de)! | ||
| 51 | } # See FormSniffer at http://www.wap2web.de/formsniffer2.aspx | ||
| 52 | } | ||
| 53 | # Miscellaneous options for the Scraper operation. | ||
| 54 | ,'cookies' => 0 | ||
| 55 | }; | ||
| 56 | |||
| 57 | |||
| 58 | my $scraperFrame = | ||
| 59 | [ 'HTML', | ||
| 60 | [ | ||
| 61 | [ 'BODY', '<!--<Address Table>-->', '<!--</Address Table>-->', | ||
| 62 | [ | ||
| 63 | [ 'HIT*' , | ||
| 64 | [ | ||
| 65 | ['REGEX', '(<tr[\s>].*?<!--<Firm Line/>-->.*?</tr>)', \&trimComments, \&trimLFs, 'firm'] | ||
| 66 | ,['REGEX', '(<tr[\s>].*?<!--<Address Line/>-->.*?</tr>)', \&trimComments, \&trimLFs, 'address'] | ||
| 67 | ,['REGEX', '(<tr[\s>].*?<!--<City-State-ZIP/>-->.*?</tr>)', \&trimComments, \&trimLFs, \&parseCity, 'city'] | ||
| 68 | ,['REGEX', '(<tr[\s>].*?<!--<Carrier Route/>-->.*?</tr>)', \&trimComments, \&trimLFs, \&cleanUpUsps, 'carrierRoute'] | ||
| 69 | ,['REGEX', '(<tr[\s>].*?<!--<County/>-->.*?</tr>)', \&trimComments, \&trimLFs, 'county'] | ||
| 70 | ,['REGEX', '(<tr[\s>].*?<!--<Delivery Point/>-->.*?</tr>)', \&trimComments, \&trimLFs, \&cleanUpUsps, 'deliveryPoint'] | ||
| 71 | ,['REGEX', '(<tr[\s>].*?<!--<Check Digit/>-->.*?</tr>)', \&trimComments, \&trimLFs, \&cleanUpUsps, 'checkDigit'] | ||
| 72 | # this regex never matches; just lets us declare fields. | ||
| 73 | ,[ 'REGEX', 'neverMatch', 'state', 'zipcode' ] | ||
| 74 | ] | ||
| 75 | ] | ||
| 76 | ] | ||
| 77 | ] | ||
| 78 | ] | ||
| 79 | ]; | ||
| 80 | |||
| 81 | |||
| 82 | my $scraperFrame_OLD = | ||
| 83 | [ 'HTML', | ||
| 84 | [ | ||
| 85 | [ 'BODY', 'The standardized address is:', '<CENTER', | ||
| 86 | [ | ||
| 87 | [ 'HIT*' , | ||
| 88 | [ | ||
| 89 | [ 'REGEX', '<b>(.*?(<BR>)?.*?)<BR>\s*(.*?)\s(..)\s(\d\d\d\d\d-\d\d\d\d)<BR>.*?<b>(.*?)</b>.*?<b>(.*?)</b>.*?<b>(.*?)</b>.*?<b>(.*?)</b>' | ||
| 90 | ,'address', undef, 'city', 'state', 'zip', 'carrierRoute', 'county', 'deliveryPoint' , 'checkDigit' ] | ||
| 91 | ] | ||
| 92 | ] | ||
| 93 | ,[ 'HIT*' , | ||
| 94 | [ | ||
| 95 | [ 'REGEX', '<b>(.*?)</b>.*?<b>(.*?)\s(..)\s(\d\d\d\d\d-\d\d\d\d)</b>.*?<b>(.*?)</b>.*?<b>(.*?)</b>.*?<b>(.*?)</b>.*?<b>(.*?)</b>' | ||
| 96 | ,'address', 'city', 'state', 'zip', 'carrierRoute', 'county', 'deliveryPoint' , 'checkDigit' ] | ||
| 97 | ] | ||
| 98 | ] | ||
| 99 | ] | ||
| 100 | ] | ||
| 101 | ] | ||
| 102 | ]; | ||
| 103 | |||
| 104 | |||
| 105 | sub testParameters { | ||
| 106 | my ($self) = @_; | ||
| 107 | |||
| 108 | if ( ref $self ) { | ||
| 109 | $self->{'isTesting'} = 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | return { | ||
| 113 | 'SKIP' => ''#'ZIPplus4 test parameters have not yet been fixed' | ||
| 114 | ,'testNativeQuery' => '20500' | ||
| 115 | ,'testNativeOptions' => { | ||
| 116 | 'address' => '1600 Pennsylvannia Ave' | ||
| 117 | ,'city' => 'Washington' | ||
| 118 | ,'state' => 'DC' | ||
| 119 | ,'zipcode' => '' | ||
| 120 | } | ||
| 121 | ,'expectedOnePage' => 1 | ||
| 122 | ,'expectedMultiPage' => 1 | ||
| 123 | ,'expectedBogusPage' => 1 | ||
| 124 | }; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | # Access methods for the structural declarations of this Scraper engine. | ||
| 129 | sub scraperRequest { $scraperRequest } | ||
| 130 | sub scraperFrame { $_[0]->SUPER::scraperFrame($scraperFrame); } | ||
| 131 | |||
| 132 | |||
| 133 | sub cleanUpUsps { | ||
| 134 | my ($self, $hit, $dat) = @_; | ||
| 135 | $dat = $self->trimLFs($hit, $dat); | ||
| 136 | $dat =~ s/^County://gs; | ||
| 137 | $dat =~ s/^Carrier Route://gs; | ||
| 138 | $dat =~ s/^Delivery Point://gs; | ||
| 139 | $dat =~ s/^Check Digit://gs; | ||
| 140 | $dat =~ s/\s*-->//gs; | ||
| 141 | return $dat; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub parseCity { | ||
| 145 | my ($self, $hit, $dat) = @_; | ||
| 146 | $dat = $self->cleanUpUsps($hit, $dat); | ||
| 147 | $dat =~ s/^(.*)\s+(\w+)\s+(\d\d\d\d\d)\s?(-\d\d\d\d)$/$1/s; | ||
| 148 | $hit->plug_elem('state', $2); | ||
| 149 | $hit->plug_elem('zipcode', "$3$4"); | ||
| 150 | return $dat; | ||
| 151 | } | ||
| 152 | |||
| 153 | { package AddressDedup; | ||
| 154 | # This package helps ZipPlus4.pl to de-duplicate the address list. | ||
| 155 | # With minor or no modification, it might be useful to others, too. | ||
| 156 | use Class::Struct; | ||
| 157 | struct ( 'AddressDedup' => | ||
| 158 | [ | ||
| 159 | 'Address' => '$' | ||
| 160 | ,'City' => '$' | ||
| 161 | ,'State' => '$' | ||
| 162 | ,'Zip' => '$' | ||
| 163 | ,'Name' => '$' | ||
| 164 | ,'_allColumns' => '$' | ||
| 165 | ,'_zipColumn' => '$' | ||
| 166 | ] | ||
| 167 | ); | ||
| 168 | |||
| 169 | sub isEqual { | ||
| 170 | my ($self, $other) = @_; | ||
| 171 | |||
| 172 | return 0 unless ($self->_isEqualAddress($other->Address)); | ||
| 173 | return 0 unless ($self->_isEqualCity($other->City)); | ||
| 174 | return 0 unless ($self->_isEqualState($other->State)); | ||
| 175 | return 0 unless ($self->_isEqualZip($other->Zip)); | ||
| 176 | # return 0 unless ($self->_isEqualName($other->Name)); | ||
| 177 | |||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | sub _isEqualAddress { | ||
| 181 | my ($self, $str) = @_; | ||
| 182 | return ($self->Address eq $str); | ||
| 183 | } | ||
| 184 | sub _isEqualCity { | ||
| 185 | my ($self, $str) = @_; | ||
| 186 | return ($self->City eq $str); | ||
| 187 | } | ||
| 188 | sub _isEqualState { | ||
| 189 | my ($self, $str) = @_; | ||
| 190 | return ($self->State eq $str); | ||
| 191 | } | ||
| 192 | sub _isEqualZip { | ||
| 193 | my ($self, $str) = @_; | ||
| 194 | return ($self->Zip eq $str); | ||
| 195 | } | ||
| 196 | sub _isEqualName { | ||
| 197 | my ($self, $str) = @_; | ||
| 198 | return ($self->Name eq $str); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | sub setValue { | ||
| 203 | my ($self, $colNums, $fullLine) = @_; | ||
| 204 | |||
| 205 | chomp $fullLine; | ||
| 206 | my @cols = split ',', $fullLine; | ||
| 207 | $self->_allColumns(\@cols); | ||
| 208 | |||
| 209 | $self->Address($cols[$colNums->{'colAddress'}]); | ||
| 210 | $self->City($cols[$colNums->{'colCity'}]); | ||
| 211 | $self->State($cols[$colNums->{'colState'}]); | ||
| 212 | $self->Zip($cols[$colNums->{'colZip'}]); | ||
| 213 | |||
| 214 | $self->_zipColumn($colNums->{'colZip'}); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub isEmpty { | ||
| 218 | my ($self) = @_; | ||
| 219 | return 0 if $self->Address; | ||
| 220 | return 0 if $self->City; | ||
| 221 | return 0 if $self->State; | ||
| 222 | return 0 if $self->Zip; | ||
| 223 | return 0 if $self->Name; | ||
| 224 | return 1; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub asString { | ||
| 228 | my ($self) = @_; | ||
| 229 | |||
| 230 | my $allColumns = $self->_allColumns(); | ||
| 231 | |||
| 232 | $$allColumns[$self->_zipColumn] = $self->Zip; | ||
| 233 | |||
| 234 | return join ',', @$allColumns; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | 1; | ||
| 238 | |||
| 239 | __END__ |