Skip to content

Instantly share code, notes, and snippets.

Created November 9, 2011 14:11
Show Gist options
  • Save anonymous/1351537 to your computer and use it in GitHub Desktop.
Save anonymous/1351537 to your computer and use it in GitHub Desktop.
# npm install mongoose
# npm install zip
Http = require('http')
Zip = require("zip")
Mongoose = require("mongoose")
Mongoose.connect("mongodb://localhost/location_test")
Location = Mongoose.model('Location',
new Mongoose.Schema
zip: String
address: String
point: { type: [Number], index: '2d'}
)
fetch_csv = (callback) ->
Http.get {host: "www.post.japanpost.jp", port: 80, path: "/zipcode/dl/roman/ken_all_rome.zip"}, (response) ->
buf = ''
response.setEncoding("binary")
response.on "data", (chunk) ->
buf += chunk
response.on "end", ->
reader = new Zip.Reader(new Buffer(buf, "binary"))
reader.forEach (entry) ->
callback(entry.getData().toString("ascii"))
fetch_csv (csv) ->
lines = csv.split(/\n/)
max_length = lines.length - 1
lines.forEach (line, index, collection) ->
if line.length < 1
max_length -= 1
return
[jiscode, zip, street, city, pref] = line.split(/\"?,\"?/)[0..4]
location = new Location(zip: zip, address: [street, city, pref].join(' '))
location.save (error)->
location = null
process.exit() if index >= max_length
null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment