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 |
---|
18 | use FindBin; |
---|
19 | use lib "$FindBin::Bin/../perllib"; |
---|
20 | use lib "$FindBin::Bin/../../perllib"; |
---|
21 | use NPEMap; |
---|
22 | use strict; |
---|
23 | |
---|
24 | # Download the npemap file |
---|
25 | my $npemap_url = "http://www.npemap.org.uk/data/currentlist"; |
---|
26 | print `wget -O /tmp/npemap-currentlist -q $npemap_url`; |
---|
27 | if($?) { exit $?; } |
---|
28 | |
---|
29 | # Our entry in the sources table |
---|
30 | my $source = 2; |
---|
31 | |
---|
32 | # Clear out the current entries |
---|
33 | my $dbh = setup_dbh(); |
---|
34 | my $del = $dbh->prepare("DELETE FROM postcodes WHERE source = ?"); |
---|
35 | $del->execute($source); |
---|
36 | |
---|
37 | # Load up the new ones |
---|
38 | my $ins = $dbh->prepare("INSERT INTO postcodes (outward,inward,raw_postcode_outward,raw_postcode_inward,easting,northing,source) VALUES (?,?,?,?,?,?,$source)"); |
---|
39 | |
---|
40 | open(POSTCODES, "</tmp/npemap-currentlist"); |
---|
41 | while(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; |
---|