Created
          August 3, 2013 01:28 
        
      - 
      
- 
        Save mkoryak/6144682 to your computer and use it in GitHub Desktop. 
    reading maxmind geoip cities csv
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | var lazy = require("lazy"); | |
| var iconv = require('iconv-lite') | |
| var removeQuotes = function(str){ | |
| return str.substring(1, str.length -1); | |
| }; | |
| lazy(fs.createReadStream(path.join(__dirname,'..', 'datafiles', 'cities.csv'))) | |
| .lines | |
| .map(function(byteArray) { | |
| return iconv.decode(byteArray, 'latin1'); | |
| }) | |
| .skip(count) | |
| .map(function (line) { | |
| var row = line.split(","); | |
| var country = removeQuotes(row[1]); | |
| var data = { | |
| 'country': country, | |
| 'state': (country == 'US') ? removeQuotes(row[2]) : null, | |
| 'city': removeQuotes(row[3]), | |
| 'postalCode': removeQuotes(row[4]), | |
| 'latitude': row[5], | |
| 'longitude': row[6] | |
| } | |
| var coords = {lat: parseFloat(data.latitude), lon: parseFloat(data.longitude)}; | |
| if(_.isNaN(coords.lat) || _.isNaN(coords.lon)){ | |
| console.log("SKIPPING WIRDNESS", data) | |
| return; | |
| } | |
| var location = db.Location({ | |
| country: data.country.toUpperCase(), | |
| city: data.city, | |
| normalizedCity: stripAccents(data.city.toLowerCase()), | |
| state: data.state, | |
| postalCode: data.postalCode, | |
| coords: coords, | |
| metroGroup: false | |
| }); | |
| //LocationSchema.index({normalizedCity: 1, state: 1, country: 1}, {unique: true, dropdupes:true}); | |
| location.save((function(err){ | |
| var msg = "Saved " | |
| if(err){ | |
| msg = 'Skipping duplicate '; | |
| } | |
| })); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment