Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing utf8 encoding #4

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/wikigeo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
"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": "[email protected]:edsu/wikigeo.git"
},
"dependencies": {
"coffee-script": "^1.7.1",
"request": "^2.35.0"
"request": "^2.35.0",
"utf8": "^2.0.0"
},
"devDependencies": {
"chai": "^1.9.1",
Expand Down
18 changes: 11 additions & 7 deletions src/wikigeo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ example:

###

geojson = (geo, opts={}, callback) =>
utf8 = require 'utf8'
crypto = require 'crypto'
request = require 'request'

geojson = (geo, opts={}, callback) ->
if typeof opts == "function"
callback = opts
opts = {}
Expand All @@ -41,7 +45,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"
Expand Down Expand Up @@ -83,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
Expand Down Expand Up @@ -172,7 +177,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(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
Expand Down Expand Up @@ -208,14 +213,13 @@ _clean = (list) ->

_fetch = (uri, opts, callback) ->
request uri, qs: opts.params, json: true, (e, r, data) ->
callback(data)
callback(e, data)

_browserFetch = (uri, opts, callback) ->
$.ajax url: uri, data: opts.params, dataType: "jsonp", success: (response) ->
callback(response)
callback(response)

try
request = require('request')
fetch = _fetch
catch error
fetch = _browserFetch
Expand Down
2 changes: 1 addition & 1 deletion test/wikigeo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down