-
Notifications
You must be signed in to change notification settings - Fork 33
/
prefecture.js
62 lines (56 loc) · 1.69 KB
/
prefecture.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const rp = require('request-promise-native')
const fs = require('fs')
const csv = require('csv')
const { Transform } = require('stream')
const LANGLINKS_SERVER_ENDPOINT = process.env.LANGLINKS_SERVER_ENDPOINT
? process.env.LANGLINKS_SERVER_ENDPOINT : 'http://localhost:8080'
// See regular_cities.csv
const transformPrefecture = csv.transform((record) => {
if (record[2]) {
return null
}
return {
// See outputColumns() in app.js
id: record[0].substr(0, 5),
prefecture_id: record[0].substr(0, 2),
prefecture_en: null,
prefecture_ja: record[1]
}
})
const addLangLinks = () => {
const transformer = new Transform({ objectMode: true })
transformer._transform = (record, encoding, callback) => {
rp({
uri: LANGLINKS_SERVER_ENDPOINT + '/search/' + encodeURIComponent(record.prefecture_ja),
json: true
})
.then((data) => {
if (!data.en) throw new Error('city name in English not found')
record.prefecture_en = data.en
callback(null, record)
})
.catch((reason) => {
console.error(reason)
})
}
return transformer
}
const fixErrors = () => {
const transformer = new Transform({ objectMode: true })
transformer._transform = (record, encoding, callback) => {
// Wikipedia has a wrong link
if (record.prefecture_ja === '熊本県') {
record.prefecture_en = 'Kumamoto Prefecture'
}
callback(null, record)
}
return transformer
}
const output = process.argv[3] ? fs.createWriteStream(process.argv[3]) : process.stdout
fs.createReadStream(process.argv[2])
.pipe(csv.parse())
.pipe(transformPrefecture)
.pipe(addLangLinks())
.pipe(fixErrors())
.pipe(csv.stringify())
.pipe(output)