From 2503b7d53d4b0f74fafcec851f9fff8ce166d527 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Wed, 4 Jun 2014 22:15:29 +0200 Subject: [PATCH 01/11] Fix test --- test/wikigeo.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/wikigeo.coffee b/test/wikigeo.coffee index b46ea39..799107a 100644 --- a/test/wikigeo.coffee +++ b/test/wikigeo.coffee @@ -64,7 +64,7 @@ describe 'wikigeo', -> it 'limit should cause more results to come in', (done) -> geojson [-77.0155, 39.0114], limit: 100, (data) -> - assert.ok data.features.length > 5 and data.features.length < 100 + assert.ok data.features.length > 5 and data.features.length < 101 done() it 'respects maximum limit', (done) -> From 9dca075c15d9f8bbe0863770bb8e3053b9eba4c1 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Thu, 5 Jun 2014 12:19:52 +0200 Subject: [PATCH 02/11] Remove todo --- src/wikigeo.coffee | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index 94f100a..8b40bfb 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -170,9 +170,8 @@ _convert = (results, opts, callback) -> ] if opts.images - if article.pageprops - # TODO: convert to full URL - # https://www.mediawiki.org/wiki/Manual:$wgHashedUploadDirectory + if article.pageprops?.page_image + # @see https://www.mediawiki.org/wiki/Manual:$wgHashedUploadDirectory md5sum = require('crypto').createHash('md5').update(article.pageprops.page_image).digest("hex") image = article.pageprops.page_image imageUrl = "https://upload.wikimedia.org/wikipedia/commons/#{md5sum[0]}/#{md5sum[0..1]}/#{article.pageprops.page_image}" From 0ce83044a106b13c3f7d592000f60317d0b1a9a3 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Thu, 5 Jun 2014 12:21:03 +0200 Subject: [PATCH 03/11] Fix ignoring error --- src/wikigeo.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index 8b40bfb..fb33529 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -208,6 +208,7 @@ _clean = (list) -> _fetch = (uri, opts, callback) -> request uri, qs: opts.params, json: true, (e, r, data) -> + throw e if e callback(data) _browserFetch = (uri, opts, callback) -> From 208a06c8485642e7010535fd28f9c80f2849cc45 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Thu, 5 Jun 2014 12:27:26 +0200 Subject: [PATCH 04/11] Add repository field to package.json --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index ee3f7db..7d86975 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,10 @@ "watch": "coffee -w --output lib src/wikigeo.coffee", "test": "mocha --colors --reporter nyan --compilers coffee:coffee-script/register test/wikigeo.coffee" }, + "repository": { + "type": "git", + "url": "git@github.com:edsu/wikigeo.git" + }, "dependencies": { "coffee-script": "^1.7.1", "request": "^2.35.0" From 84f8a96137ae730676b1805a3c0c1a3391d4e120 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Thu, 5 Jun 2014 12:29:00 +0200 Subject: [PATCH 05/11] Update build --- lib/wikigeo.js | 253 +++++++++++++++++++++++++------------------------ 1 file changed, 127 insertions(+), 126 deletions(-) diff --git a/lib/wikigeo.js b/lib/wikigeo.js index 3c2fe7b..f1e8ab8 100644 --- a/lib/wikigeo.js +++ b/lib/wikigeo.js @@ -1,4 +1,5 @@ -// Generated by CoffeeScript 1.6.2 +// Generated by CoffeeScript 1.7.1 + /* geojson takes a geo coordinate (longitude, latitude) and a callback that will @@ -16,144 +17,143 @@ example: geojson([-77.0155, 39.0114], {radius: 5000}, function(data) { console.log(data); }) -*/ - -var error, fetch, geojson, request, root, _browserFetch, _clean, _convert, _fetch, _search, - _this = this; - -geojson = function(geo, opts, callback) { - if (opts == null) { - opts = {}; - } - if (typeof opts === "function") { - callback = opts; - opts = {}; - } - if (!opts.limit) { - opts.limit = 10; - } - if (!opts.radius) { - opts.radius = 10000; - } - if (!opts.language) { - opts.language = "en"; - } - if (opts.radius > 10000) { - throw new Error("radius cannot be greater than 10000"); - } - if (opts.limit > 500) { - throw new Error("limit cannot be greater than 500"); - } - return _search(geo, opts, callback); -}; + */ +var error, fetch, geojson, request, root, _browserFetch, _clean, _convert, _fetch, _search; -_search = function(geo, opts, callback, results, queryContinue) { - var continueParams, name, param, q, url; - - url = "http://" + opts.language + ".wikipedia.org/w/api.php"; - q = { - action: "query", - prop: "info|coordinates", - generator: "geosearch", - ggsradius: opts.radius, - ggscoord: "" + geo[1] + "|" + geo[0], - ggslimit: opts.limit, - format: "json" - }; - if (opts.images) { - q.prop += "|pageprops"; - } - if (opts.summaries) { - q.prop += "|extracts"; - q.exlimit = "max"; - q.exintro = 1; - q.explaintext = 1; - } - if (opts.templates) { - q.prop += "|templates"; - q.tllimit = 500; - } - if (opts.categories) { - q.prop += "|categories"; - q.cllimit = 500; - } - continueParams = { - extracts: "excontinue", - coordinates: "cocontinue", - templates: "tlcontinue", - categories: "clcontinue" - }; - if (queryContinue) { - for (name in continueParams) { - param = continueParams[name]; - if (queryContinue[name]) { - q[param] = queryContinue[name][param]; - } +geojson = (function(_this) { + return function(geo, opts, callback) { + if (opts == null) { + opts = {}; } - } - return fetch(url, { - params: q - }, function(response) { - var article, articleId, first, newValues, prop, resultsArticle, _ref; + if (typeof opts === "function") { + callback = opts; + opts = {}; + } + if (!opts.limit) { + opts.limit = 10; + } + if (!opts.radius) { + opts.radius = 10000; + } + if (!opts.language) { + opts.language = "en"; + } + if (opts.radius > 10000) { + throw new Error("radius cannot be greater than 10000"); + } + if (opts.limit > 500) { + throw new Error("limit cannot be greater than 500"); + } + return _search(geo, opts, callback); + }; +})(this); - if (!results) { - first = true; - results = response; - } else { - first = false; +_search = (function(_this) { + return function(geo, opts, callback, results, queryContinue) { + var continueParams, name, param, q, url; + url = "http://" + opts.language + ".wikipedia.org/w/api.php"; + q = { + action: "query", + prop: "info|coordinates", + generator: "geosearch", + ggsradius: opts.radius, + ggscoord: "" + geo[1] + "|" + geo[0], + ggslimit: opts.limit, + format: "json" + }; + if (opts.images) { + q.prop += "|pageprops"; } - if (!(response.query && response.query.pages)) { - _convert(results, opts, callback); - return; + if (opts.summaries) { + q.prop += "|extracts"; + q.exlimit = "max"; + q.exintro = 1; + q.explaintext = 1; } - _ref = response.query.pages; - for (articleId in _ref) { - article = _ref[articleId]; - resultsArticle = results.query.pages[articleId]; - if (!resultsArticle) { - continue; - } - for (prop in continueParams) { - param = continueParams[prop]; - if (prop === 'extracts') { - prop = 'extract'; + if (opts.templates) { + q.prop += "|templates"; + q.tllimit = 500; + } + if (opts.categories) { + q.prop += "|categories"; + q.cllimit = 500; + } + continueParams = { + extracts: "excontinue", + coordinates: "cocontinue", + templates: "tlcontinue", + categories: "clcontinue" + }; + if (queryContinue) { + for (name in continueParams) { + param = continueParams[name]; + if (queryContinue[name]) { + q[param] = queryContinue[name][param]; } - newValues = article[prop]; - if (!newValues) { + } + } + return fetch(url, { + params: q + }, function(response) { + var article, articleId, first, newValues, prop, resultsArticle, _ref; + if (!results) { + first = true; + results = response; + } else { + first = false; + } + if (!(response.query && response.query.pages)) { + _convert(results, opts, callback); + return; + } + _ref = response.query.pages; + for (articleId in _ref) { + article = _ref[articleId]; + resultsArticle = results.query.pages[articleId]; + if (!resultsArticle) { continue; } - if (Array.isArray(newValues)) { - if (!resultsArticle[prop]) { - resultsArticle[prop] = newValues; - } else if (!first && resultsArticle[prop][-1] !== newValues[-1]) { - resultsArticle[prop] = resultsArticle[prop].concat(newValues); + for (prop in continueParams) { + param = continueParams[prop]; + if (prop === 'extracts') { + prop = 'extract'; + } + newValues = article[prop]; + if (!newValues) { + continue; + } + if (Array.isArray(newValues)) { + if (!resultsArticle[prop]) { + resultsArticle[prop] = newValues; + } else if (!first && resultsArticle[prop][-1] !== newValues[-1]) { + resultsArticle[prop] = resultsArticle[prop].concat(newValues); + } + } else { + resultsArticle[prop] = article[prop]; } - } else { - resultsArticle[prop] = article[prop]; } } - } - if (response['query-continue']) { - if (!queryContinue) { - queryContinue = response['query-continue']; - } else { - for (name in continueParams) { - param = continueParams[name]; - if (response['query-continue'][name]) { - queryContinue[name] = response['query-continue'][name]; + if (response['query-continue']) { + if (!queryContinue) { + queryContinue = response['query-continue']; + } else { + for (name in continueParams) { + param = continueParams[name]; + if (response['query-continue'][name]) { + queryContinue[name] = response['query-continue'][name]; + } } } + return _search(geo, opts, callback, results, queryContinue); + } else { + return _convert(results, opts, callback); } - return _search(geo, opts, callback, results, queryContinue); - } else { - return _convert(results, opts, callback); - } - }); -}; + }); + }; +})(this); _convert = function(results, opts, callback) { - var article, articleId, feature, geo, image, imageUrl, md5sum, titleEscaped, url, _ref; - + var article, articleId, feature, geo, image, imageUrl, md5sum, titleEscaped, url, _ref, _ref1; geo = { type: "FeatureCollection", features: [] @@ -184,7 +184,7 @@ _convert = function(results, opts, callback) { } }; if (opts.images) { - if (article.pageprops) { + if ((_ref1 = article.pageprops) != null ? _ref1.page_image : void 0) { md5sum = require('crypto').createHash('md5').update(article.pageprops.page_image).digest("hex"); image = article.pageprops.page_image; imageUrl = "https://upload.wikimedia.org/wikipedia/commons/" + md5sum[0] + "/" + md5sum.slice(0, 2) + "/" + article.pageprops.page_image; @@ -211,10 +211,8 @@ _convert = function(results, opts, callback) { _clean = function(list) { var i; - return (function() { var _i, _len, _results; - _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { i = list[_i]; @@ -229,6 +227,9 @@ _fetch = function(uri, opts, callback) { qs: opts.params, json: true }, function(e, r, data) { + if (e) { + throw e; + } return callback(data); }); }; From 6999087d3bf9eb314931382246150117f46d5f9f Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Wed, 29 Oct 2014 13:50:41 +0100 Subject: [PATCH 06/11] :lipstick: Fix indentation --- src/wikigeo.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index fb33529..da0cd1e 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -213,7 +213,7 @@ _fetch = (uri, opts, callback) -> _browserFetch = (uri, opts, callback) -> $.ajax url: uri, data: opts.params, dataType: "jsonp", success: (response) -> - callback(response) + callback(response) try request = require('request') From 1a4dc863805b18b46ee02fbda9af1b53caf72b5d Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Wed, 29 Oct 2014 13:51:09 +0100 Subject: [PATCH 07/11] :lipstick: Remove unnecessary fat arrows --- src/wikigeo.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index da0cd1e..7b58007 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -18,7 +18,7 @@ example: ### -geojson = (geo, opts={}, callback) => +geojson = (geo, opts={}, callback) -> if typeof opts == "function" callback = opts opts = {} @@ -41,7 +41,7 @@ geojson = (geo, opts={}, callback) => # recursive function to collect the results from all search result pages # -_search = (geo, opts, callback, results, queryContinue) => +_search = (geo, opts, callback, results, queryContinue) -> url = "http://#{ opts.language }.wikipedia.org/w/api.php" q = action: "query" @@ -83,7 +83,7 @@ _search = (geo, opts, callback, results, queryContinue) => if queryContinue[name] q[param] = queryContinue[name][param] - fetch url, params: q, (response) => + fetch url, params: q, (response) -> if not results first = true From 66c54bfaf0f83165e4c7a2ee0d0fbde5a21a6436 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Wed, 29 Oct 2014 13:52:17 +0100 Subject: [PATCH 08/11] :lipstick: move requires to top --- src/wikigeo.coffee | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index 7b58007..4c3f15b 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -18,6 +18,9 @@ example: ### +crypto = require 'crypto' +request = require 'request' + geojson = (geo, opts={}, callback) -> if typeof opts == "function" callback = opts @@ -172,7 +175,7 @@ _convert = (results, opts, callback) -> if opts.images if article.pageprops?.page_image # @see https://www.mediawiki.org/wiki/Manual:$wgHashedUploadDirectory - md5sum = require('crypto').createHash('md5').update(article.pageprops.page_image).digest("hex") + md5sum = crypto.createHash('md5').update(article.pageprops.page_image).digest("hex") image = article.pageprops.page_image imageUrl = "https://upload.wikimedia.org/wikipedia/commons/#{md5sum[0]}/#{md5sum[0..1]}/#{article.pageprops.page_image}" else @@ -216,7 +219,6 @@ _browserFetch = (uri, opts, callback) -> callback(response) try - request = require('request') fetch = _fetch catch error fetch = _browserFetch From 25fd83403676b1a99117017d0b0f6ece8e81e92f Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Wed, 29 Oct 2014 13:53:25 +0100 Subject: [PATCH 09/11] :bug: Fix missing utf8 encoding Wikipedia hashes UTF-8 strings. JS strings are ASCII strings, so hashing file names containing non ASCII characters results in wrong hashes. That results in wrong image URLs. Encoding the file names in UTF-8 before hashing fixes this. --- package.json | 3 ++- src/wikigeo.coffee | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7d86975..3e945cd 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ }, "dependencies": { "coffee-script": "^1.7.1", - "request": "^2.35.0" + "request": "^2.35.0", + "utf8": "^2.0.0" }, "devDependencies": { "chai": "^1.9.1", diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index 4c3f15b..bc66a2f 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -18,6 +18,7 @@ example: ### +utf8 = require 'utf8' crypto = require 'crypto' request = require 'request' @@ -175,7 +176,7 @@ _convert = (results, opts, callback) -> if opts.images if article.pageprops?.page_image # @see https://www.mediawiki.org/wiki/Manual:$wgHashedUploadDirectory - md5sum = crypto.createHash('md5').update(article.pageprops.page_image).digest("hex") + md5sum = crypto.createHash('md5').update(utf8.encode(article.pageprops.page_image)).digest("hex") image = article.pageprops.page_image imageUrl = "https://upload.wikimedia.org/wikipedia/commons/#{md5sum[0]}/#{md5sum[0..1]}/#{article.pageprops.page_image}" else From 481cf502de1df89d0a435ac0420893c1aef118c7 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Wed, 29 Oct 2014 14:27:51 +0100 Subject: [PATCH 10/11] Update build --- lib/wikigeo.js | 243 +++++++++++++++++++++++++------------------------ 1 file changed, 122 insertions(+), 121 deletions(-) diff --git a/lib/wikigeo.js b/lib/wikigeo.js index f1e8ab8..e28a944 100644 --- a/lib/wikigeo.js +++ b/lib/wikigeo.js @@ -18,139 +18,141 @@ example: console.log(data); }) */ -var error, fetch, geojson, request, root, _browserFetch, _clean, _convert, _fetch, _search; +var crypto, error, fetch, geojson, request, root, utf8, _browserFetch, _clean, _convert, _fetch, _search; -geojson = (function(_this) { - return function(geo, opts, callback) { - if (opts == null) { - opts = {}; - } - if (typeof opts === "function") { - callback = opts; - opts = {}; - } - if (!opts.limit) { - opts.limit = 10; - } - if (!opts.radius) { - opts.radius = 10000; - } - if (!opts.language) { - opts.language = "en"; - } - if (opts.radius > 10000) { - throw new Error("radius cannot be greater than 10000"); - } - if (opts.limit > 500) { - throw new Error("limit cannot be greater than 500"); - } - return _search(geo, opts, callback); - }; -})(this); +utf8 = require('utf8'); -_search = (function(_this) { - return function(geo, opts, callback, results, queryContinue) { - var continueParams, name, param, q, url; - url = "http://" + opts.language + ".wikipedia.org/w/api.php"; - q = { - action: "query", - prop: "info|coordinates", - generator: "geosearch", - ggsradius: opts.radius, - ggscoord: "" + geo[1] + "|" + geo[0], - ggslimit: opts.limit, - format: "json" - }; - if (opts.images) { - q.prop += "|pageprops"; - } - if (opts.summaries) { - q.prop += "|extracts"; - q.exlimit = "max"; - q.exintro = 1; - q.explaintext = 1; - } - if (opts.templates) { - q.prop += "|templates"; - q.tllimit = 500; +crypto = require('crypto'); + +request = require('request'); + +geojson = function(geo, opts, callback) { + if (opts == null) { + opts = {}; + } + if (typeof opts === "function") { + callback = opts; + opts = {}; + } + if (!opts.limit) { + opts.limit = 10; + } + if (!opts.radius) { + opts.radius = 10000; + } + if (!opts.language) { + opts.language = "en"; + } + if (opts.radius > 10000) { + throw new Error("radius cannot be greater than 10000"); + } + if (opts.limit > 500) { + throw new Error("limit cannot be greater than 500"); + } + return _search(geo, opts, callback); +}; + +_search = function(geo, opts, callback, results, queryContinue) { + var continueParams, name, param, q, url; + url = "http://" + opts.language + ".wikipedia.org/w/api.php"; + q = { + action: "query", + prop: "info|coordinates", + generator: "geosearch", + ggsradius: opts.radius, + ggscoord: "" + geo[1] + "|" + geo[0], + ggslimit: opts.limit, + format: "json" + }; + if (opts.images) { + q.prop += "|pageprops"; + } + if (opts.summaries) { + q.prop += "|extracts"; + q.exlimit = "max"; + q.exintro = 1; + q.explaintext = 1; + } + if (opts.templates) { + q.prop += "|templates"; + q.tllimit = 500; + } + if (opts.categories) { + q.prop += "|categories"; + q.cllimit = 500; + } + continueParams = { + extracts: "excontinue", + coordinates: "cocontinue", + templates: "tlcontinue", + categories: "clcontinue" + }; + if (queryContinue) { + for (name in continueParams) { + param = continueParams[name]; + if (queryContinue[name]) { + q[param] = queryContinue[name][param]; + } } - if (opts.categories) { - q.prop += "|categories"; - q.cllimit = 500; + } + return fetch(url, { + params: q + }, function(response) { + var article, articleId, first, newValues, prop, resultsArticle, _ref; + if (!results) { + first = true; + results = response; + } else { + first = false; } - continueParams = { - extracts: "excontinue", - coordinates: "cocontinue", - templates: "tlcontinue", - categories: "clcontinue" - }; - if (queryContinue) { - for (name in continueParams) { - param = continueParams[name]; - if (queryContinue[name]) { - q[param] = queryContinue[name][param]; - } - } + if (!(response.query && response.query.pages)) { + _convert(results, opts, callback); + return; } - return fetch(url, { - params: q - }, function(response) { - var article, articleId, first, newValues, prop, resultsArticle, _ref; - if (!results) { - first = true; - results = response; - } else { - first = false; - } - if (!(response.query && response.query.pages)) { - _convert(results, opts, callback); - return; + _ref = response.query.pages; + for (articleId in _ref) { + article = _ref[articleId]; + resultsArticle = results.query.pages[articleId]; + if (!resultsArticle) { + continue; } - _ref = response.query.pages; - for (articleId in _ref) { - article = _ref[articleId]; - resultsArticle = results.query.pages[articleId]; - if (!resultsArticle) { + for (prop in continueParams) { + param = continueParams[prop]; + if (prop === 'extracts') { + prop = 'extract'; + } + newValues = article[prop]; + if (!newValues) { continue; } - for (prop in continueParams) { - param = continueParams[prop]; - if (prop === 'extracts') { - prop = 'extract'; - } - newValues = article[prop]; - if (!newValues) { - continue; - } - if (Array.isArray(newValues)) { - if (!resultsArticle[prop]) { - resultsArticle[prop] = newValues; - } else if (!first && resultsArticle[prop][-1] !== newValues[-1]) { - resultsArticle[prop] = resultsArticle[prop].concat(newValues); - } - } else { - resultsArticle[prop] = article[prop]; + if (Array.isArray(newValues)) { + if (!resultsArticle[prop]) { + resultsArticle[prop] = newValues; + } else if (!first && resultsArticle[prop][-1] !== newValues[-1]) { + resultsArticle[prop] = resultsArticle[prop].concat(newValues); } + } else { + resultsArticle[prop] = article[prop]; } } - if (response['query-continue']) { - if (!queryContinue) { - queryContinue = response['query-continue']; - } else { - for (name in continueParams) { - param = continueParams[name]; - if (response['query-continue'][name]) { - queryContinue[name] = response['query-continue'][name]; - } + } + if (response['query-continue']) { + if (!queryContinue) { + queryContinue = response['query-continue']; + } else { + for (name in continueParams) { + param = continueParams[name]; + if (response['query-continue'][name]) { + queryContinue[name] = response['query-continue'][name]; } } - return _search(geo, opts, callback, results, queryContinue); - } else { - return _convert(results, opts, callback); } - }); - }; -})(this); + return _search(geo, opts, callback, results, queryContinue); + } else { + return _convert(results, opts, callback); + } + }); +}; _convert = function(results, opts, callback) { var article, articleId, feature, geo, image, imageUrl, md5sum, titleEscaped, url, _ref, _ref1; @@ -185,7 +187,7 @@ _convert = function(results, opts, callback) { }; if (opts.images) { if ((_ref1 = article.pageprops) != null ? _ref1.page_image : void 0) { - md5sum = require('crypto').createHash('md5').update(article.pageprops.page_image).digest("hex"); + md5sum = crypto.createHash('md5').update(utf8.encode(article.pageprops.page_image)).digest("hex"); image = article.pageprops.page_image; imageUrl = "https://upload.wikimedia.org/wikipedia/commons/" + md5sum[0] + "/" + md5sum.slice(0, 2) + "/" + article.pageprops.page_image; } else { @@ -246,7 +248,6 @@ _browserFetch = function(uri, opts, callback) { }; try { - request = require('request'); fetch = _fetch; } catch (_error) { error = _error; From a9bcaff990d06936e3b1212e28e12d4006d82889 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Fri, 25 Sep 2015 16:36:09 +0200 Subject: [PATCH 11/11] :bug: handle errors correctly --- lib/wikigeo.js | 10 +++++----- src/wikigeo.coffee | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/wikigeo.js b/lib/wikigeo.js index e28a944..d2d2fb6 100644 --- a/lib/wikigeo.js +++ b/lib/wikigeo.js @@ -97,8 +97,11 @@ _search = function(geo, opts, callback, results, queryContinue) { } return fetch(url, { params: q - }, function(response) { + }, function(error, response) { var article, articleId, first, newValues, prop, resultsArticle, _ref; + if (error) { + return callback(error); + } if (!results) { first = true; results = response; @@ -229,10 +232,7 @@ _fetch = function(uri, opts, callback) { qs: opts.params, json: true }, function(e, r, data) { - if (e) { - throw e; - } - return callback(data); + return callback(e, data); }); }; diff --git a/src/wikigeo.coffee b/src/wikigeo.coffee index bc66a2f..e897e61 100644 --- a/src/wikigeo.coffee +++ b/src/wikigeo.coffee @@ -87,7 +87,8 @@ _search = (geo, opts, callback, results, queryContinue) -> if queryContinue[name] q[param] = queryContinue[name][param] - fetch url, params: q, (response) -> + fetch url, params: q, (error, response) -> + return callback(error) if error if not results first = true @@ -212,8 +213,7 @@ _clean = (list) -> _fetch = (uri, opts, callback) -> request uri, qs: opts.params, json: true, (e, r, data) -> - throw e if e - callback(data) + callback(e, data) _browserFetch = (uri, opts, callback) -> $.ajax url: uri, data: opts.params, dataType: "jsonp", success: (response) ->