Skip to content

Commit

Permalink
Attempt to deal with downloads that report success in the HTTP status…
Browse files Browse the repository at this point in the history
… but then fail half-way through leaving us with an incomplete XML file
  • Loading branch information
DrHyde committed Nov 6, 2024
1 parent a01f192 commit ab657b3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions build-data.nanp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ foreach my $npanxx (keys %US) {
warn("Extracting non-US/CA operators\n");
foreach my $NPA (Number::Phone::Country::_non_US_CA_area_codes()) {
warn(" ... $NPA\n");
my $data = _get_xml_data($NPA);
my $xml = XML::XPath->new(xml => $data);
my $xml = _get_xml_data($NPA);
my %ops = ();

my @ten_thousand_prefixes = $xml->find("/root/prefixdata[x = 'A']")->get_nodelist();
Expand Down Expand Up @@ -225,6 +224,7 @@ sub _get_xml_data {
my $NPA = shift;
if(!-e "$NPA.xml" || -M "$NPA.xml" > 14) {
my $retries = 0;
my $xml;
my $data;
my $ua = LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0 },
Expand All @@ -233,8 +233,11 @@ sub _get_xml_data {
);
TRY: my $res = $ua->get("https://localcallingguide.com/xmlprefix.php?npa=$NPA&blocks=1");
if($res->is_success()) {
# OK, we got data - but is it a complete download?
$data = $res->content();
} else {
eval { $xml = XML::XPath->new(xml => $data); } || warn("Can't parse XML for $NPA: $@\n");
}
if(!$xml) {
$retries++;
if($retries < 3) {
warn("Can't talk to localcallingguide.com for $NPA: ".$res->status_line()."\n".$res->decoded_content()."\n");
Expand All @@ -253,7 +256,8 @@ sub _get_xml_data {
open(my $fh, '<', "data-files/$NPA.xml") || die("Can't read data-files/$NPA.xml: $!\n");
my $data = join('', <$fh>);
close($fh);
return $data;
$xml = XML::XPath->new(xml => $data);
return $xml;
}

sub _write_operator_data {
Expand Down

0 comments on commit ab657b3

Please sign in to comment.