Skip to content

Instantly share code, notes, and snippets.

@qcom
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qcom/fccfd5d46a63e6127f9b to your computer and use it in GitHub Desktop.
Save qcom/fccfd5d46a63e6127f9b to your computer and use it in GitHub Desktop.
trying async module
var pg = require('pg');
var request = require('request');
var async = require('async');
function getUrl(location) {
return 'https://maps.googleapis.com/maps/api/geocode/json?address=' + location.address + ', ' + location.city + ', ' + location.state + ' ' + location.zip_code;
}
var count = 0;
function crashAndBurn(err) { if (err) throw err; }
async.waterfall([
function(cb) {
pg.connect('postgres://zach:n1nt3nd0!@localhost/waste', function(err, client) {
cb(err, client);
});
},
function(client, cb) {
client.query('SELECT id, address, city, state, zip_code FROM companies WHERE pos IS NULL', function(err, result) {
cb(err, client, result.rows);
});
},
function(client, companies, cb) {
console.log(companies.length + ' to go');
async.each(companies, function(company, fn) {
request(getUrl(company), { json : true }, function(err, res, body) {
if (err) return fn(err);
if (body.status === 'OK') {
var location = body.results[0].geometry.location;
client.query('UPDATE companies SET pos = point($1, $2) WHERE id = $3;', [location.lng, location.lat, company.id], function(err, result) {
if (err) return fn(err);
console.log('Done ' + ++count);
/*console.log(company.id);
console.log(location);
console.log(getUrl(company));*/
if (count === 1500) process.exit();
});
} else {
console.log('ERROR: AT COMPANY ID ' + company.id);
}
});
}, cb);
}
], crashAndBurn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment