-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-parser.js
201 lines (173 loc) · 6.22 KB
/
csv-parser.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const fs = require('fs');
const jp = require('jsonpath');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvParser = require('csv-parse/lib/sync');
const dedupe = require('dedupe');
const csvWriter = createCsvWriter({
path: './test_file.csv',
header: [
{id: 'id', title: 'ID'},
{id: 'category', title: 'Category'},
{id: 'name', title: 'Name'},
{id: 'documents', title: 'Documents'},
{id: 'eligibility', title: 'Eligibility'},
{id: 'hours', title: 'Hours'},
{id: 'location', title: 'Location'},
{id: 'latitude', title: 'Latitude'},
{id: 'longitude', title: 'Longitude'},
{id: 'phone', title: 'Phone'},
{id: 'service_description', title: 'Service Description'},
]
});
let rows = [];
let rowId = 1;
const parseSmartColumbusJSON = (fileLocation) => {
let sampleFile = JSON.parse(fs.readFileSync(fileLocation));
let foodServiceNames = ["Food Banks/Food Distribution Warehouses", "Food Pantries", "Food Pantries * Veterans", "Soup Kitchens", "Community Meals", "Food Donation Programs", ]
const getSite = (data) => {
return jp.query(data, '$..site');
}
const getLatitude = (site) => {
let [latitude, ] = jp.query(site, '$..latitude');
return latitude;
}
const getlongitude = (site) => {
let [longitude, ] = jp.query(site, '$..longitude');
return longitude;
}
const getPhoneNumbers = (site) => {
let phoneNumbers = [];
let stgphones = jp.query(site, '$..stgphones..phone');
let stphone = jp.query(site, '$..stphones..phone');
if (stgphones != []) {
phoneNumbers.push(...stgphones);
}
if (stphone != []) {
phoneNumbers.push(...stphone);
}
return (phoneNumbers.length > 0 ? phoneNumbers.join('\r\n') : phoneNumbers.join(''));
}
const getAddress = (site) => {
let [city, ] = jp.query(site, '$..address..city')
let [line1, ] = jp.query(site, '$..address..line1');
let [line2, ] = jp.query(site, '$..address..line2');
let [line3, ] = jp.query(site, '$..address..line3');
let [state, ] = jp.query(site, '$..address..state');
let [zip, ] = jp.query(site, '$..address..zip');
// Nice spacing things.
line1 = (line2 != '' ? line1 + ' ' : line1);
line2 = (line3 != '' ? line2 + ' ' : line2);
return line1 + line2 + line3 + ', ' + city + ' ' + state + ' ' + zip;
}
const getSiteInfo = (data) => {
return jp.query(data, '$..site_info');
}
const getSiteInfoElement = (site_info, element_name) => {
let [element, ] = jp.query(site_info, '$..detailtext[?(@.label=="' + element_name + '")].text');
return element;
}
sampleFile.forEach(element => {
let [category, ] = jp.query(element, '$..taxonomy.service_name');
if (foodServiceNames.includes(category)) {
let name = element.provider_name;
let site = getSite(element);
let siteInfo = getSiteInfo(element);
let latitude = getLatitude(site);
let longitude = getlongitude(site);
let service_description = getSiteInfoElement(siteInfo, "Service Description");
let hours = getSiteInfoElement(siteInfo, "Hours");
let documents = getSiteInfoElement(siteInfo, "Documents");
let eligibility = getSiteInfoElement(siteInfo, "Eligibility");
let phone = getPhoneNumbers(site);
let location = getAddress(site);
rows.push(
{
id: rowId,
category: category,
name: name,
documents: documents,
eligibility: eligibility,
hours: hours,
location: location,
latitude: latitude,
longitude: longitude,
phone: phone,
service_description: service_description
}
);
rowId++;
}
});
}
const parseHscCsv = (fileLocation) => {
const csv = fs.readFileSync(fileLocation);
let parsedCSV = csvParser(csv, {
columns: true,
skip_empty_lines: true
});
parsedCSV.forEach(data => {
rows.push(
{
id: rowId,
category: null,
name: data["Name"],
documents: null,
eligibility: null,
hours: null,
location: null,
latitude: data["Latitude"],
longitude: data["Longitude"],
phone: null,
service_description: data["Description"]
}
)
rowId++;
});
}
const parseCOVIDCsv = (fileLocation) => {
const csv = fs.readFileSync(fileLocation);
let parsedCSV = csvParser(csv, {
columns: true,
skip_empty_lines: true
});
parsedCSV.forEach(data => {
rows.push(
{
id: rowId,
category: data["Category"],
name: data["Name"],
documents: data["Documents"],
eligibility: data["Eligibility"],
hours: data["Hours"],
location: data["Location"],
latitude: data["Latitude"],
longitude: data["Longitude"],
phone: data["Phone"],
service_description: data["Service Description"]
}
)
rowId++;
});
}
parseCOVIDCsv('./covid19_resources2.csv')
parseSmartColumbusJSON('./pantry.json');
parseHscCsv('./hsc.csv');
// Use a custom hash to look for exact duplicates and remove them - examine all fields but the id
console.log("before dedupe rows " + rows.length);
rows = dedupe(rows, value => [
value.name,
value.location,
value.latitude,
value.longitude,
value.service_description,
value.documents,
value.hours,
value.phone,
value.eligibility,
value.category
]);
console.log("after dedupe rows " + rows.length);
csvWriter.writeRecords(rows)
.then(() => {
console.log('File Written!');
});