Created
June 24, 2014 23:57
-
-
Save tgriesser/8b4fdd3af1419bce4d49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require("bluebird"); | |
var request = Promise.promisify(require('request')); | |
var knex = require('knex'); | |
var pg = knex({ | |
client: 'pg', | |
connection: connString | |
}); | |
function getUrl(location) { | |
return 'https://maps.googleapis.com/maps/api/geocode/json?address=' + location.address + ', ' + location.city + ', ' + location.state + ' ' + location.zip_code; | |
} | |
pg.select('id', 'address', 'city', 'state', 'zip_code').from('companies').whereNull('pos').map(function(company) { | |
return request(getUrl(company)).then(function(body) { | |
body = JSON.parse(body) | |
if (body.status == 'OK') { | |
return pg('companies').update({ | |
pos: pg.raw('point(?, ?)', [location.lng, location.lat]) | |
}).where({id: company.id}) | |
} else { | |
console.log('ERROR: AT COMPANY ID ' + company.id); | |
} | |
}) | |
}).then(function(rows) { | |
process.exit() | |
}).catch(function(e) { | |
console.log(e.stack); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment