Created
March 11, 2019 10:32
-
-
Save niaeashes/3bee32dc4aa76e0733a5cacd39ceba85 to your computer and use it in GitHub Desktop.
位置参照情報ダウンロードサービスからデータを落としてきて適当に整形して JSON にまとめる感じのやつ
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
// http://nlftp.mlit.go.jp/isj/agreement.html | |
const csv2json = require('csv2json'); | |
const iconv = require("iconv-lite") | |
const fs = require('fs') | |
const path = require('path') | |
const items = fs.readdirSync(__dirname) | |
const promises = items.map(async (name) => { | |
const filepath = path.join(__dirname, name) | |
const stats = fs.statSync(filepath) | |
if ( stats.isDirectory() === false ) return [] | |
const items = fs.readdirSync(filepath) | |
const names = await Promise.all(items.map((name) => { | |
if ( path.extname(name) !== '.csv' ) return undefined | |
return path.join(filepath, name) | |
})) | |
return names | |
}) | |
const stream = (filepath) => { | |
return new Promise(resolve => { | |
const datafilepath = path.join(path.dirname(filepath), path.basename(filepath, '.csv') + '.json') | |
return resolve(datafilepath) | |
fs.createReadStream(filepath) | |
.pipe(iconv.decodeStream('windows-31j')) | |
.pipe(csv2json()) | |
.pipe(fs.createWriteStream(datafilepath)) | |
.on('finish', () => { | |
console.log('finish:', filepath) | |
resolve(datafilepath) | |
}) | |
}) | |
} | |
Promise.all(promises) | |
.then((items) => items.reduce((list, it) => list.concat(it), []).filter(it => it !== undefined)) | |
.then((names) => Promise.all(names.map(stream)).then((it) => it)) | |
.then(async (files) => { | |
const stream = fs.createWriteStream('./result.json') | |
stream.write('[') | |
let total = files.length | |
let nextSeparator = '' | |
while ( files.length > 0 ) { | |
const filepath = files.shift() | |
const reader = fs.createReadStream(filepath, { encoding: 'utf8' }) | |
let buffer = "" | |
const jsonChunk = (chunk) => { | |
buffer += chunk | |
while ( buffer.match(/{(?<content>.+)}/) !== null ) { | |
const match = buffer.match(/{(?<content>.+)}/) | |
const { index, groups } = match | |
const { content } = groups | |
buffer = buffer.substring(index + match[0].length) | |
try { | |
const raw = JSON.parse('{' + content + '}') | |
const parsed = { | |
prefecture: raw["都道府県名"], | |
city: raw["市区町村名"], | |
latitude: raw["緯度"], | |
longitude: raw["経度"], | |
} | |
stream.write(nextSeparator + JSON.stringify(parsed), 'utf8') | |
nextSeparator = ',\n' | |
} catch (err) { | |
console.error(content, err) | |
} | |
} | |
} | |
reader.on('data', jsonChunk) | |
console.log('Start to import data from:', filepath) | |
await new Promise(resolve => reader.on('end', () => resolve())) | |
console.log('Finish to import data from:', filepath, total - files.length, '/', total) | |
} | |
stream.write(']') | |
stream.end() | |
}) | |
.then(() => process.exit(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment