Skip to content

Commit

Permalink
[Bexley] Adapt postcode DB script to take URL for fetching CSV.
Browse files Browse the repository at this point in the history
To allow for automatically pulling in updates via a cron for mysociety/societyworks#4463.
  • Loading branch information
neprune committed Feb 11, 2025
1 parent 6929b72 commit 4715b27
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions bin/bexley/make-bexley-ww-postcode-db
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
make-bexley-ww-postcode-db
=head1 USAGE
make-bexley-ww-postcode-db
=head1 DESCRIPTION
Creates a SQLite database for postcode lookups for Bexley WasteWorks, pulled
Expand All @@ -32,8 +28,32 @@ use DBI;
use FixMyStreet;
use Text::CSV;
use Cwd 'abs_path';
use LWP::Simple;
use IO::String;
use Encode;
use Getopt::Long::Descriptive;

my ($opts, $usage) = describe_options(
'%c %o',
['csv=s', "a path to a LLPG CSV to use"],
['fetch=s', "a url to fetch the LLPG CSV from"],
['help|h', "print usage message and exit" ],
);
print($usage->text), exit if $opts->help;

my $csv_file = $ARGV[0] or die "Please provide a CSV file\n";
my $csv = Text::CSV->new( { binary => 1, auto_diag => 1 } );
my $fh;
if (!($opts->csv) && ($opts->fetch)) {
say "Fetching the LLPG CSV via url."
my $csv_content = LWP::Simple::get($opts->fetch);
$csv_content = decode('UTF-8', $csv_content);
$fh = IO::String->new($csv_content);
} elsif ($opts->csv && !($opts->fetch)) {
say "Using the CSV file provided."
open $fh, '<:encoding(utf8)', $opts->csv;
} else {
die "A url or path to fetch the CSV from must be provided.";
}

# Delete any existing database file first
my $db_file = BexleyAddresses::database_file;
Expand Down Expand Up @@ -119,9 +139,6 @@ my $query_postcodes = $db->prepare(
SQL
);

my $csv = Text::CSV->new( { binary => 1, auto_diag => 1 } );
open my $fh, '<:encoding(utf8)', $csv_file;

my $id_street_descriptor = 15;
my @columns_street_descriptor = (
qw/
Expand Down

0 comments on commit 4715b27

Please sign in to comment.