Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 335168c
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Wrapper for [ipinfodb.com](http://www.ipinfodb.com/ip_location_api.php)'s IP geolocation API. | ||
|
||
# Install | ||
|
||
npm install ipinfodb | ||
|
||
# Usage | ||
|
||
```javascript | ||
var key = '....'; | ||
client = require('ipinfodb')({ key: key }); | ||
|
||
client.geolocate('106.186.17.133', function (err, res) { | ||
console.log(res); | ||
}); | ||
|
||
client.ip2timezoneOffset('106.186.17.133', function (err, timezoneOffset) { | ||
console.log(timezoneOffset); | ||
}); | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* opts: | ||
* - key : API key | ||
* - endpoint : (optional) API base URL | ||
*/ | ||
var http = require('http'), | ||
qs = require('querystring'), | ||
path = require('path'); | ||
|
||
function Client(opts) { | ||
this.key = opts.key; | ||
this.endpoint = (opts.endpoint) ? opts.endpoint : 'http://api.ipinfodb.com/v3/ip-city/'; | ||
} | ||
|
||
Client.prototype.geolocate = function (ip, cb) { | ||
var query = { | ||
key: this.key, | ||
ip: ip, | ||
format: 'json' | ||
} | ||
var url = this.endpoint + '?' + qs.stringify(query); | ||
http.get(url, function (res) { | ||
res.on('data', function (data) { | ||
try { | ||
var data = JSON.parse(data); | ||
cb(null, data); | ||
} | ||
catch (err) { | ||
cb(err); | ||
} | ||
}); | ||
}).on('error', function (err) { | ||
cb(err); | ||
});; | ||
} | ||
function timezone2timezoneOffset(timezone) { | ||
timezone = timezone.split(':'); | ||
var hours = parseInt(timezone[0], 10); | ||
var minutes = parseInt(timezone[1], 10); | ||
if (hours < 0) minutes = -minutes; | ||
// convert to minutes | ||
return hours * 60 + minutes; | ||
}; | ||
|
||
Client.prototype.ip2timezoneOffset = function (ip, cb) { | ||
this.geolocate(ip, function (err, res) { | ||
if (err) return cb(err); | ||
|
||
console.log(res.timeZone); | ||
cb(null, timezone2timezoneOffset(res.timeZone)); | ||
}); | ||
} | ||
|
||
module.exports = function (opts) { | ||
return new Client(opts); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "ipinfodb", | ||
"version": "0.0.1", | ||
"description": "Wrapper for ipinfodb.com API (ip to timezone)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/olalonde/node-ipinfodb.git" | ||
}, | ||
"keywords": [ | ||
"ip", | ||
"timezone", | ||
"geolocation", | ||
"ipinfodb" | ||
], | ||
"author": "Olivier Lalonde <olalonde@gmail.com>", | ||
"license": "BSD" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
key.js |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var key = require('./key'), | ||
client = require('../')({ key: key }); | ||
|
||
client.ip2timezoneOffset('106.186.17.133', function (err, timezoneOffset) { | ||
console.log(timezoneOffset); | ||
}); |