-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvfy.js
137 lines (126 loc) · 3.85 KB
/
csvfy.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
This script transforms every json files in `settings.paths.folia path` in custom csv files.
The csv files are then stored in the same `settings.paths.folia path`
*/
var settings = require('./settings'),
fs = require('fs'),
path = require('path'),
helpers = require('./helpers'),
async = require('async'),
_ = require('lodash'),
json2csv = require('json2csv'),
clc = require('cli-color'),
options = {};
async.waterfall([
function init(callback){
console.log(clc.magentaBright('merit'));
fs.readdir('folia', function(err, files){
options.folia = _(files)
.filter(function(d) {
return d.indexOf('.json') != -1
})
.map(function(d){
return {
src: d,
name: d.replace(/\.json*$/, '')
}
})
.value()
callback(null, options);
});
},
function csvfy(options, callback){
console.log('found', clc.magentaBright(options.folia.length), 'json files, e.g:')
console.log(_.take(options.folia, 2))
options.merged = [];
var q = async.queue(function(folia, nextFolia){
// read file content
console.log(clc.blackBright(' file:', clc.cyanBright(folia.src), 'remaining:'), q.length())
async.waterfall([
function getJsondata(next){
fs.readFile(path.join('folia', folia.src), 'utf8', next);
},
function parse(content, next) {
next(null, _(JSON.parse(content)).map(function(sentence){
var combinations = [
{
name: sentence.name,
s: sentence.s,
p: sentence.p,
text: sentence.text,
place: '',
lat: '',
lng: '',
is_known: '',
group: '',
code : ''
}
];
if(sentence.locations.length >0) {
combinations = sentence.locations.map(function(location){
return {
name: sentence.name,
s: sentence.s,
p: sentence.p,
text: sentence.text,
place: location.query,
lat: location.lat,
lng: location.lng,
is_known: location.partial? 0: 1,
group: sentence.group,
code : ''
};
});
};
if(sentence.matches.length > 0)
combinations = _.map(combinations, function(d){
d.code = _.map(sentence.matches, 'code').join('||');
return d
});
return combinations;
})
.flatten()
.compact()
.value()
);
},
function transform(data, next){
options.merged = options.merged.concat(data)
json2csv({
data: data,
fields: [
"name", "s", "p", "text", "place", "lat", "lng", "is_known", "group", "code"
]
}, next);
},
function writeCsv(csvdata, next){
fs.writeFile(path.join('folia', folia.name + '.csv'), csvdata, 'utf8', next);
}
], function(err) {
if(err){
q.kill();
callback(err);
} else {
nextFolia()
}
});
}, 1);
// q.push(_.take(options.folia,2))
q.push(options.folia)
q.drain = function(){
json2csv({
data: options.merged,
fields: [
"name", "s", "p", "text", "place", "lat", "lng","is_known", "group", "code"
]
}, function(err, csvdata){
fs.writeFile(path.join('folia', 'merged.csv'), csvdata, 'utf8', callback);
});
}
}
], function(err){
if(err)
console.log(err)
else
console.log(clc.greenBright('Done!'));
});