-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
182 lines (169 loc) · 5.31 KB
/
helpers.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var settings = require('./settings'),
fs = require('fs'),
path = require('path'),
csv = require('csv'),
clc = require('cli-color'),
request = require('request'),
_ = require('lodash');
module.exports = {
csv: {
stringify: function(options, callback) {
console.log(clc.yellowBright('\n tasks.helpers.csv.stringify'));
csv.stringify(options.records, {
delimiter: options.delimiter || '\t',
columns: options.fields,
header: true
}, function (err, data) {
fs.writeFile(options.filepath,
data, function (err) {
if(err) {
callback(err);
return
}
callback(null, options);
})
});
},
/*
REQUIRE an absolute or relative to this file task
*/
parse: function(options, callback) {
console.log(clc.yellowBright('\n tasks.helpers.csv.parse'));
if(!options.source) {
return callback(' Please specify the file path with --source=path/to/source.tsv');
}
csv.parse(''+fs.readFileSync(options.source), {
columns : true,
delimiter: options.delimiter || '\t'
}, function (err, data) {
if(err) {
callback(err);
return;
}
console.log(clc.blackBright(' parsing csv file completed,', clc.magentaBright(data.length), 'records found'));
options.data = data;
callback(null, options);
});
}
},
cache: {
/*
*/
naming: function(options) {
var md5 = require('md5');
return path.join(settings.paths.cache[options.namespace], options.ref = md5(options.ref) + '.json')
},
write: function(contents, options, next) {
console.log(' writing in cache...')
if(_.isEmpty(settings.paths.cache[options.namespace]))
next(IS_EMPTY)
else
fs.writeFile(module.exports.cache.naming(options), contents, next)
},
read: function(options, next) {
if(_.isEmpty(settings.paths.cache[options.namespace]))
next(IS_EMPTY)
else
fs.readFile(module.exports.cache.naming(options), 'utf8', function (err, contents) {
if(err)
next(err);
else {
try {
next(null, JSON.parse(contents))
} catch(e) {
console.log(e)
next(null, contents);
}
}
})
},
unlink: function(options, next) {
if(_.isEmpty(settings.paths.cache[options.namespace]))
next(IS_EMPTY)
else
fs.unlink(module.exports.cache.naming(options), next);
}
},
geocoding: function(options, next) {
if(!settings.geocoding ||_.isEmpty(settings.geocoding.key)) {
next(null, []);
return;
}
// is it a file named ...
request.get({
url: settings.geocoding.endpoint,
qs: _.assign({
key: settings.geocoding.key
}, options, {
q: options.address
}),
json: true
}, function (err, res, body) {
if(err) {
console.log('service geocoding failed')
next(err);
return;
}
// console.log(url)
if(!body.results.length) {
if(body.error_message) {
console.log('service geocoding failed')
next(body.error_message)
return;
}
next(null, []);
return;
};
// adding name, fcl and country code as well: it depends on the level.
next(null, _.take(body.results.map(function (result) {
var name = result.formatted_address,
fcl,
country;
if(result.types.indexOf('continent') != -1) {
fcl = 'L';
} else if(result.types.indexOf('country') != -1) {
fcl = 'A';
} else if(result.types.indexOf('locality') != -1) {
fcl = 'P';
} else {
// console.log(result, options.address)
// throw 'stop'
}
country = _.find(result.address_components, function (d){
return d.types.indexOf('country') != -1
});
if(!country){
country = result.address_components[0]
}
if(!country){
console.log(result)
throw 'stop'
}
return _.assign(result, {
_id: result.place_id,
_name: name,
_fcl: fcl,
_country: country.short_name,
_query: options.address,
});
}), 1));
// if()
// console.log(body.results[0].formatted_address, 'for', options.address);
// console.log(body.results[0].address_components)
// var country = _.find(body.results[0].address_components, function (d){
// return d.types[0] == 'country';
// }),
// locality = _.find(body.results[0].address_components, function (d){
// return d.types[0] == 'locality';
// });
// // the entity name
// var name_partials = [];
// if(locality && locality.long_name)
// name_partials.push(locality.long_name);
// if(country && country.long_name)
// name_partials.push(country.long_name);
// var name = name_partials.length? name_partials.join(', '): body.results[0].formatted_address;
// console.log(body.results[0], name)
})
}
}