-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
149 lines (118 loc) · 3.96 KB
/
process.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
138
139
140
141
142
143
144
145
146
147
148
149
var fs = require('fs');
var beautify = require('js-beautify').js_beautify;
var collectionsNames = [
'posts',
'organizations',
'memberships',
'persons'
];
var deleteFields = {
'posts': ['html_url', 'url', 'memberships'],
'organizations': ['html_url', 'url', 'memberships'],
'memberships': ['html_url', 'url'],
'persons': ['memberships', 'links', 'identifiers', 'other_names', 'html_url', 'url', 'images', 'proxy_image']
}
var collections = {};
collectionsNames.forEach(function(colName){
collections[colName] = JSON.parse(fs.readFileSync(colName + '.json').toString());
console.log('Processing', collections[colName].length, colName);
collections[colName] = cleanupCollection(collections[colName], deleteFields[colName]);
// console.log(getNonEmptyKeys(collections[colName]));
});
var hashes = {};
hashes['persons'] = collections.persons.reduce(function(memo, item){ memo[item.id] = item; return memo;},{});
hashes['posts'] = collections.posts.reduce(function(memo, item){ memo[item.id] = item; return memo;},{});
hashes['organizations'] = collections.organizations.reduce(function(memo, item){ memo[item.id] = item; return memo;},{});
var outFields = [
['persons', 'name'],
['persons', 'birth_date'],
['persons', 'sources'],
['persons', 'summary'],
['persons', 'image'],
['persons', 'name'],
['organizations', 'name'],
['organizations', 'classification'],
['posts', 'label'],
['posts', 'role'],
['posts', 'cargonominal'],
['posts', 'cargotipo'],
['posts', 'cargoclase'],
['posts', 'duracioncargo'],
['memberships', 'label'],
['memberships', 'role'],
['memberships', 'cargonominal'],
['memberships', 'start_date'],
['memberships', 'end_date'],
['memberships', 'end_date_accuracy'],
['memberships', 'start_date_accuracy'],
];
var rows = [];
rows.push([]);
outFields.forEach(function(field){
rows[0].push( enquote( field.join('-') ) ) ;
})
collections.memberships.forEach(function(membership){
var row = [];
var person = hashes['persons'][membership.person_id] || {}
var post = hashes['posts'][membership.post_id] || {}
var organization = hashes['organizations'][membership.organization_id] || {}
outFields.forEach(function(field){
if(field[0]=='persons'){
row.push( enquote ( person[field[1]]) )
}else if(field[0]=='posts'){
row.push( enquote ( post[field[1]]) )
}else if(field[0]=='organizations'){
row.push( enquote ( organization[field[1]]) )
}else if(field[0]=='memberships'){
row.push( enquote ( membership[field[1]]) )
}else{
throw "ERror not a valid item"
}
})
rows.push(row);
});
fs.writeFileSync('out.csv', rows.join('\n'))
console.log('done');
function enquote(s){
//Double quotes
if(typeof s == 'undefined') s = ''
if(typeof s != 'string'){
if(s.toString) s = s.toString(); else s = '';
}
s = s.replace(/\"/g, '""');
return '"' + s + '"';
}
//collections.persons = cleanupCollection(collections.persons, ['memberships', 'links', 'identifiers', 'other_names', 'html_url', 'url', 'images', 'proxy_image'])
// console.log(beautify(JSON.stringify(collections.posts)));
// console.log("Persons")
// .forEach(function(k){ console.log(k); });
function cleanupCollection(collection, deleteFields){
collection.forEach(function(item){
deleteFields.forEach(function(d){
delete item[d];
})
})
var nonEmptyKeys = getNonEmptyKeys(collection);
var mappedCol = collection.map(function(p){
var s = {}
nonEmptyKeys.forEach(function(k){
if(p && ( (typeof p[k] === 'string' && p[k] !='') || ( ( Object.prototype.toString.call( p[k] ) === '[object Array]' && p[k].length > 0 ) ) ) ){ s[k] = p[k]}
})
return s;
})
return mappedCol;
}
function getNonEmptyKeys(col){
var s, keys = {}, arrk = [];
col.forEach(function(item){
Object.keys(item).forEach(function(k){
if(typeof item[k] === 'string' && item[k] != '' ){
keys[k] = k;
}else if( Object.prototype.toString.call( item[k] ) === '[object Array]' && item[k].length > 0 ){
keys[k] = k;
}
})
});
for(s in keys){ arrk.push(s); }
return arrk;
}