source: trunk/npemap.org.uk/scripts/npemap-importer/importer.pl @ 591

Last change on this file since 591 was 591, checked in by Nick Burch, 12 years ago

Rename import script

  • Property svn:executable set to *
File size: 1.7 KB
Line 
1#!/usr/bin/perl
2#                   NPE Maps API Mirroring
3#                   ----------------------
4#
5# This script handles downloading the latest NPEMaps dataset export (but
6#  not the latest FreeThePostcode export), and importing it into a local
7#  copy of the NPE Maps database.
8# This allows you to run your own copy of the NPE Maps api, and have it
9#  loaded with the latest data nightly / weekly
10# (You will almost certainly want to schedule a run of the
11#  freethepostcode importer too)
12#
13# Warning - you should not be tracking bad postcodes if you use this
14#  script, as it will zap everything it has previously imported between
15#   runs!
16
17# Find our private perl libraries
18use FindBin;
19use lib "$FindBin::Bin/../perllib";
20use lib "$FindBin::Bin/../../perllib";
21use NPEMap;
22use strict;
23
24# Download the npemap file
25my $npemap_url = "http://www.npemap.org.uk/data/currentlist";
26print `wget -O /tmp/npemap-currentlist -q $npemap_url`;
27if($?) { exit $?; }
28
29# Our entry in the sources table
30my $source = 2;
31
32# Clear out the current entries
33my $dbh = setup_dbh();
34my $del = $dbh->prepare("DELETE FROM postcodes WHERE source = ?");
35$del->execute($source);
36
37# Load up the new ones
38my $ins = $dbh->prepare("INSERT INTO postcodes (outward,inward,raw_postcode_outward,raw_postcode_inward,easting,northing,source) VALUES (?,?,?,?,?,?,$source)");
39
40open(POSTCODES, "</tmp/npemap-currentlist");
41while(my $line = <POSTCODES>) {
42        chomp $line;
43        if($line =~ /^\#/) { next };
44        my ($outward,$inward,$e,$n) = 
45                ($line =~ /^([\w\d]+),([\w\d]*),(\d+),(\-?\d+),/);
46        if($outward) {
47                $ins->execute($outward,$inward,$outward,$inward,$e,$n);
48        } else {
49                warn("Invalid line '$line'\n");
50        }
51}
52
53# Close our connection
54$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.