-
Notifications
You must be signed in to change notification settings - Fork 0
/
createTweetCsv.js
47 lines (39 loc) · 1.08 KB
/
createTweetCsv.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
// **************************
// ****** DEPENDENCIES ******
// **************************
var _ = require('lodash');
var database = require('./api/database');
var schema = require('./api/schema');
var json2csv = require('json2csv');
var fs = require('fs');
// **************************
// ********** MAIN **********
// **************************
var outputFile = './logs/csv/test.csv';
database.connect(function() {
console.log('Opened db connection.');
database.findTweets({}, function(err, tweets) {
if (err) {
bail('Failed to get tweets.', err);
}
json2csv({data: tweets, fields: ['id', 'text']}, function(err, csv) {
if (err) {
bail('failed to convert json to csv', err);
}
fs.writeFile(outputFile, csv, function(err) {
if (err) {
bail('failed to write csv file', err);
}
console.log('Saved csv.');
process.exit(0);
});
});
});
}, function(err) {
bail('mongo connection failed', err);
});
bail = function(msg, err) {
console.log(msg);
console.log(err);
process.exit(1);
};