source: trunk/npemap.org.uk/scripts/api-mirror/mirror-api-data @ 589

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

Make a start on an importer from the full npemaps list

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