-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvscript.js
More file actions
96 lines (83 loc) · 2.41 KB
/
Copy pathcsvscript.js
File metadata and controls
96 lines (83 loc) · 2.41 KB
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
var request = require('request');
var csv = require('csv');
var mongoose = require("mongoose");
var mongoDatabase = 'mongodb://localhost/theCountedData';
mongoose.connect( mongoDatabase);
var db = mongoose.connection;
db.on('error', function (err) {
console.log('connection error', err);
});
db.once('open', function () {
console.log('connected.');
});
//Define Squid Schema
var Schema = mongoose.Schema;
var personSchema = new Schema({
name: String,
age: String,
sex: String,
race: String,
month: String,
day: String,
year: String,
address: String,
city: String,
state: String,
cause: String,
dept: String,
armed: String,
});
a = [];
// Schema to DB Model
var Person = mongoose.model('Person', personSchema);
request.get('https://interactive.guim.co.uk/2015/the-counted/thecounted-data.zip', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("got it")
var c = [];
a = body.split('\n');
var bcomp = 0;
for(var i = 2; i < a.length - 8; i++){
var indexer = [];
// console.log(a[i]);
while(a[i].length > 0){
var subber = a[i].substring(1,a[i].indexOf('"', 1))
if(subber.length != 1 || (subber.length ==1 && !isNaN(subber))) {indexer.push(subber)};
a[i] = a[i].substring(1, a[i].length)
a[i] = a[i].substring(a[i].indexOf('"'), a[i].length)
}
if(indexer.length != bcomp){
console.log("new length: " + indexer.length)
console.log(indexer);
bcomp = indexer.length;
}
console.log(indexer.length);
var b = indexer;
var g = new Person({
id: b[0].replace(/"/g, ""),
name: b[1].replace(/"/g, ""),
age: b[2].replace(/"/g, ""),
sex: b[3].replace(/"/g, ""),
race: b[4].replace(/"/g, ""),
month: b[5].replace(/"/g, ""),
day: b[6].replace(/"/g, ""),
year: b[7].replace(/"/g, ""),
address: b[8].replace(/"/g, ""),
city: b[9].replace(/"/g, ""),
state: b[10].replace(/"/g, ""),
cause: b[11].replace(/"/g, ""),
dept: b[12].replace(/"/g, ""),
armed: b[13].replace(/"/g, ""),
})
if(i == a.length-9){
console.log("last item")
}
g.save(function(err, data){
if(err){console.log(err)}
console.log(data)
})
}
}
}
, function(error, data){
console.log(data)
});