From 0be8b78e6adec9999a9918038ea2e38c34621f27 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 17 Nov 2014 14:57:35 +0900 Subject: [PATCH] Adding timeout to geocoding script to deal with API rate limiting --- scrape/geocode.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/scrape/geocode.js b/scrape/geocode.js index cc5dead..e1d37cd 100755 --- a/scrape/geocode.js +++ b/scrape/geocode.js @@ -5,23 +5,27 @@ var jf = require('jsonfile'); var _ = require('underscore'); -function queryPosition(gc, address, cache, callback) { +function queryPosition(gc, address, cache, sequence, callback) { if (_.has(cache, address)) { console.log('Cache lookup success for:\n\t%s', address); callback(cache[address]); - return; + return sequence; } - gc.geocode(address, function(err, res) { - if (err) { - console.log('Geocode lookup fail for: \n\t%s', address); - callback(null); - } - else { - console.log('Geocode lookup success for: \n\t%s', address); - callback(cache[address] = res[0]); - } - }); + setTimeout(function() { + gc.geocode(address, function(err, res) { + if (err) { + console.log('Geocode lookup fail for: \n\t%s', address); + callback(null); + } + else { + console.log('Geocode lookup success for: \n\t%s', address); + callback(cache[address] = res[0]); + } + }); + }, sequence * 200); + + return sequence + 1; } @@ -31,9 +35,10 @@ function main() { var srcCount = srcData.length; var cacheData = jf.readFileSync('cache/geo.json', {throws: false}) || {}; var destData = []; + var sequence = 0; _.each(srcData, function(srcItem) { - queryPosition(gc, srcItem.address, cacheData, function(geo) { + sequence = queryPosition(gc, srcItem.address, cacheData, sequence, function(geo) { if (geo) { var destItem = _.clone(srcItem); destItem.geo = geo;