summaryrefslogtreecommitdiffstats
path: root/util/lib/geo/index.js
blob: 0763b1a47e4034cf7a4b8f99753101c47265e8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function slurp (stream, callback) {
  var data = []
  stream.on('data', function (chunk) {
    data.push(chunk)
  })
  stream.on('end', function () {
    callback(null, Buffer.concat(data))
  })
  stream.resume()
}


var path = require('path')
var city_dat = path.join(__dirname, 'GeoLiteCity.dat')

var geoip = require('geoip')
var city = new geoip.City(city_dat)

slurp(process.stdin, function (err, data) {
  var lines = data.toString().split('\n')
  // remove last, empty element (caused by the [mandatory] final \n)
  if (lines.length > 1 && lines[lines.length - 1] === '') {
    lines.pop()
  }
  var name = 0, addr = 1
  lines
    .map(function (line) { return line.split(' ') })
    .forEach(function (host) {
      //city.lookup(host[addr], function (err, loc) {
      //  if (err) {
      //    console.error('#', host[name], err.message)
      //  } else {
      //    console.log(host[name] + ': ' + loc.longitude + ',' + loc.latitude)
      //  }
      //})
      var loc = city.lookupSync(host[addr])
      //if (!loc) { console.error('#', host[name]) }
      if (loc) {
        var a = loc.latitude
        var b = loc.longitude
        //var c = loc.altitude
        //var geo = 'geo:' + [a,b,c].join(',')
        var geo = 'geo:' + [a,b].join(',')

        console.log(host[name], geo)
      }
    })
})