From ad51891f515742ece50a224887404e4020ae5ad8 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Thu, 21 Sep 2017 16:57:14 +0100 Subject: [PATCH] Made script able to import into database --- lib/Pear/LocalLoop/Command/codepoint_open.pm | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/Pear/LocalLoop/Command/codepoint_open.pm b/lib/Pear/LocalLoop/Command/codepoint_open.pm index 6a54e09..fbc9bb0 100644 --- a/lib/Pear/LocalLoop/Command/codepoint_open.pm +++ b/lib/Pear/LocalLoop/Command/codepoint_open.pm @@ -13,7 +13,7 @@ sub run { my ( $self, @args ) = @_; getopt \@args, - 'o|outcodes=s' => \my @outcodes; + 'o|outcodes=s' => \my @outcodes, 'q|quiet' => \my $quiet_mode; 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 ); -use Devel::Dwarn; -Dwarn \@outcodes; - - print 'Importing data for ' . $#outcodes ? join( ' ', @outcodes ) : 'all' . 'outcodes\n' + printf( "Importing data for %s outcode(s)\n", @outcodes ? join( ' ', @outcodes ) : 'all' ) unless $quiet_mode; - my $i = 0; + my $iter = $cpo->read_iterator( outcodes => \@outcodes, include_lat_long => 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