Made script able to import into database

This commit is contained in:
Tom Bloor 2017-09-21 16:57:14 +01:00
parent db1bbab10f
commit ad51891f51

View file

@ -13,7 +13,7 @@ sub run {
my ( $self, @args ) = @_; my ( $self, @args ) = @_;
getopt \@args, getopt \@args,
'o|outcodes=s' => \my @outcodes; 'o|outcodes=s' => \my @outcodes,
'q|quiet' => \my $quiet_mode; 'q|quiet' => \my $quiet_mode;
my $cpo_dir = $self->app->home->child('etc')->child('code-point-open'); my $cpo_dir = $self->app->home->child('etc')->child('code-point-open');
@ -27,22 +27,28 @@ sub run {
my $cpo = Geo::UK::Postcode::CodePointOpen->new( path => $output_dir ); my $cpo = Geo::UK::Postcode::CodePointOpen->new( path => $output_dir );
use Devel::Dwarn; printf( "Importing data for %s outcode(s)\n", @outcodes ? join( ' ', @outcodes ) : 'all' )
Dwarn \@outcodes;
print 'Importing data for ' . $#outcodes ? join( ' ', @outcodes ) : 'all' . 'outcodes\n'
unless $quiet_mode; unless $quiet_mode;
my $i = 0;
my $iter = $cpo->read_iterator( my $iter = $cpo->read_iterator(
outcodes => \@outcodes, outcodes => \@outcodes,
include_lat_long => 1, include_lat_long => 1,
split_postcode => 1, split_postcode => 1,
); );
while ( my $pc = $iter->() ) {
$i++;
}
Dwarn $i; use Devel::Dwarn;
my $pc_rs = $self->app->schema->resultset('GbPostcode');
while ( my $pc = $iter->() ) {
$pc_rs->find_or_create(
{
outcode => $pc->{Outcode},
incode => $pc->{Incode},
latitude => $pc->{Latitude},
longitude => $pc->{Longitude},
},
{ key => 'primary' },
);
}
} }
=head1 SYNOPSIS =head1 SYNOPSIS