This repository has been archived by the owner on Aug 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
import_exclusions.js
102 lines (87 loc) · 3.04 KB
/
import_exclusions.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
var fs = require('fs')
, csv = require('csv')
, cheerio = require('cheerio')
, request = require('request')
, mongoose = require('mongoose')
, sys = require('sys')
, exec = require('child_process').exec;
if (process.env.MONGOHQ_URL) {
global.DB = mongoose.createConnection(process.env.MONGOHQ_URL)
} else {
global.DB = mongoose.createConnection('localhost', 'rfpez-apis');
}
var Exclusion = require('../models/exclusion');
var dataFolder = __dirname + '/../_data/epls/';
var mongooseKeyFor = function (key) {
return key.replace(/\/|\s|-/g, '_').replace(/_+/, '_').toLowerCase();
};
var parse = function(req, res) {
if (process.argv.indexOf('--sample-data') !== -1) {
console.log("++++++++ USING SAMPLE DATA ++++++++");
var csv_file = dataFolder + 'exclusions_sample.csv';
} else {
var csv_file = dataFolder + 'exclusions.csv';
}
var exclusions = new Array();
csv()
.fromPath(csv_file, {
columns: true
})
.on('data',function(data,index){
newRecord = {};
for (key in data) {
newRecord[mongooseKeyFor(key)] = data[key];
}
exclusions.push(newRecord);
})
.on('end',function(count){
var saveExclusions = function (exclusions) {
var newExclusion = new Exclusion(exclusions.shift());
var runCb = function() {
if (exclusions.length > 0) {
console.log(exclusions.length + ' records remaining.');
saveExclusions(exclusions);
} else {
process.exit();
}
}
Exclusion.findOne({sam_number: newExclusion.sam_number}, function(err, exists){
if (exists) {
console.log('exclusion already exists.');
runCb();
} else {
newExclusion.save(function(){
console.log('saved record.');
runCb();
});
}
});
}
saveExclusions(exclusions);
})
};
request('https://www.sam.gov/public-extracts/SAM-Public/', function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var href = $('a').last().attr('href');
console.log("Grabbing file: https://www.sam.gov/public-extracts/SAM-Public/" + href);
console.log("Writing temp zip file to " + dataFolder + 'exclusions.zip');
var savedZip = fs.createWriteStream(dataFolder + 'exclusions.zip');
request("https://www.sam.gov/public-extracts/SAM-Public/" + href)
.pipe(savedZip)
.on('close', function () {
console.log('Zip file saved!. Now uncompressing...');
exec("cd " + dataFolder + "; unzip " + dataFolder + "exclusions.zip; mv " + dataFolder + "SAM*.CSV " + dataFolder + "exclusions.csv", function (error, stdout, stderr) { // one easy function to capture data/errors
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log('exec error: ' + error);
process.exit();
} else {
console.log("Now parsing...");
parse();
}
});
});
}
});