File size: 3,466 Bytes
7f88cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* ************************************************************************** */
/* All the parameters you need for the clean-up and transformation are here   */
/* ************************************************************************** */

// There is a quota for the Google geolocation APIs we use for this script
var GOOGLE_THROTTLE_SECONDS = 3;

/* ************************************************************************** */
/* ************************************************************************** */
/* ************************************************************************** */

var _ = require('underscore');
_.mixin(require('underscore.string').exports());
var argv = require('optimist') // https://github.com/substack/node-optimist
    .usage('Usage: $0 --out [filename]')
    .demand([ 'out' ])
    .alias('out', 'o')
    .argv; 
var async = require('async');
var cheerio = require('cheerio');
var csv = require('csv'); // http://www.adaltas.com/projects/node-csv/
var geocoder = require('geocoder'); // https://github.com/wyattdanger/geocoder
var request = require('request');

// with help from http://okfnlabs.org/blog/2013/01/15/web-scraping-with-node-css-selectors.html
request("http://www.london-fire.gov.uk/a-zfirestations.asp", 
	function (err, resp, body) {

      function save (err) {
        csv()
        .from.array(
            [ _.keys(stations[_.keys(stations)[0]]) ] 
                .concat(_.values(stations)), 
            { columns: true })
        .to.path(__dirname + '/' + argv.out, {
            header: true,
        })
        .on('end', function (count) {
          console.log('Station list produced, ' + count + 
            ' stations found.');
        })
        .on('error', function (error) {
          console.log(error.message);
        });
      }

      console.log("The script execution may take some time, as the Google geolocation APIs are constrained by a quota and");
      console.log("we throttle our calls to them. Be patient.");
		  // I parse the webpage with the list of fire stations
  		$ = cheerio.load(body);
  		var stationNames = _.map($("dl dd h2"), function (x) {
  			return _.clean($(x).html());
  		});
  		var boroughs = _.map($("dl dd p a"), function (x) {
  			return _.clean($(x).html());
  		});
  		var stationAddresses = _.map($("dl dd p"), function (x) {
  			return _.filter(_.map($(x).html().split("<br>"), _.clean),
  							function (y) {
								return !y.match(/^Borough/);
							})
              .concat([ "London", "Greater London" ])
  						.join(", ");
  		});
  		var stations = [ ];
  		async.eachSeries(_.range(0, stationNames.length), 
                       _.throttle(function (i, eachCallback) { 
                           console.log("Geolocating " + (i + 1) + " of " + stationNames.length + "...");
                           geocoder.geocode(stationAddresses[i], function (err, data) {        
                             stations.push({ 
                                 name: stationNames[i], 
                                 address: data.results[0].formatted_address,
				 borough: boroughs[i], 
                                 latitude: data.results[0].geometry.location.lat,
                                 longitude: data.results[0].geometry.location.lng,
                             });
                             eachCallback(null);
                          });
                       }, GOOGLE_THROTTLE_SECONDS * 1000), save);
  	});