-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·354 lines (287 loc) · 9.12 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env nodejs
var fs = require('fs');
var http = require('http');
var path = require('path');
var util = require('util');
var clone = require('clone');
var prompt = require('prompt');
var argv = require('minimist')(process.argv.slice(2), {
boolean: ['h', 'b', 'local', 'reallyPayMoney', 'labelOnly']
});
var Label = require('./label.js');
var parseIndiegogo = require('./parse_indiegogo.js');
var buyShippingLabel = require('./buyShippingLabel.js');
var settings = require('./settings.js');
var packages = require('./packages.js');
var easypost = null;
if(!argv.labelOnly) {
if(argv.reallyPayMoney) {
if(!settings.easypost.apiKey) {
console.error("You must set easypost.apiKey in settings.js");
process.exit(1);
}
easypost = require('node-easypost')(settings.easypost.apiKey);
} else {
if(!settings.easypost.apiKeyTesting) {
console.error("You must set easypost.apiKeyTesting in settings.js");
process.exit(1);
}
easypost = require('node-easypost')(settings.easypost.apiKeyTesting);
}
}
function fail(str, line, addr) {
if(line) {
str += " on line " + line;
}
console.error(str);
if(addr) {
console.error(' Address: ' + util.inspect(addr));
}
process.exit(1);
}
function isHouseNumber(str) {
if(str.match(/^\d+[\s\.,]*\w?$/)) {
return true;
}
return false;
}
function mailingLabel(label, line, addr, country, inverse, local, require, ignore) {
try {
// convert "USA" / "usa" / "U.S.A." to "United States"
if(addr.country && addr.country.match(/^\s*u\.?s\.?a\.?\s*$/i)) {
addr.country = "United States";
}
if(addr.country.match(/^\s*united\s+states\s*$/i) && !inverse) {
require.state_province = true;
}
if(country) {
var r = new RegExp(country, 'i');
if(!addr.country) {
return false;
}
if(!inverse && !addr.country.match(r)) {
return false;
}
if(inverse && addr.country.match(r)) {
return false;
}
}
if(!addr.name && !ignore.name) {
fail("Address is missing a name", line, addr);
}
if(addr.name) {
label.write(addr.name, 'bold');
}
if(!addr.address && !addr.address_2 && !ignore.address) {
fail("Address is missing a street address", line, addr);
}
if(addr.address && addr.address_2) {
if(isHouseNumber(addr.address) || isHouseNumber(addr.address_2)) {
label.write(addr.address + ' ' + addr.address_2);
} else {
label.write(addr.address);
label.write(addr.address_2);
}
} else {
if(addr.address) {
label.write(addr.address);
}
if(addr.address_2 && (addr.address_2 != addr.address)) {
label.write(addr.address_2);
}
}
if(!addr.city && !ignore.city) {
fail("Address is missing a city", line, addr);
}
if(addr.city) {
label.write(addr.city);
}
var key;
if(require) {
for(key in require) {
if(!addr[key]) {
fail("Address is missing required field: " + key);
}
}
}
if(!addr.zip_postal_code) {
fail("Address is missing a zip or postal code", line, addr);
}
var state_province_zip = [];
if(addr.state_province) {
state_province_zip.push(addr.state_province);
}
if(addr.zip_postal_code) {
state_province_zip.push(addr.zip_postal_code);
}
if(state_province_zip.length > 0) {
label.write(state_province_zip.join(', '));
}
if(!addr.country && !local) {
fail("Address is missing a country", line, addr);
}
if(addr.country && !local) {
label.write(addr.country);
}
} catch(e) {
fail(e, line, addr);
}
return true;
}
// find size if a size file was specified
// and add size to perk name
function findSize(person, callback) {
if(!argv.sizeFile) {
return callback(null, person);
}
var defaultSize = (argv.defaultSize || 'medium').toLowerCase();
parseIndiegogo(argv.sizeFile, function(err, line, sperson, next) {
var size;
if(err) return callback(err);
if(sperson.pledge_id == person.pledge_id) {
size = (sperson.size || defaultSize).toLowerCase();
if(argv.size && argv.size != size) {
return callback();
}
person.perk += ' ' + size;
return callback(null, person)
}
next();
}, function() {
person.perk += ' ' + defaultSize;
callback(null, person);
});
}
// verify address using easyPosst API
function verifyAddress(address, callback) {
if(!easypost) {
return;
}
easypost.Address.create(address, function(err, address) {
fromAddress.verify(function(err, response) {
if (err) {
callback('Address is invalid: ' + util.inspect(address));
} else if (response.message !== undefined && response.message !== null) {
callback("Address: " + address + ". Is valid but has an issue: " + response.message);
} else {
callback(null, response);
}
});
});
}
// returns true if any of the values in a hash equal the specified value
function hasValue(h, val) {
var key;
for(key in h) {
if(h[key] == val) {
return true;
}
}
return false;
}
function arrayToHash(arr) {
var h = {};
var i;
for(i=0; i < arr.length; i++) {
h[arr[i]] = true;
}
return h;
}
function usage(f) {
f = f || console.error;
f("Usage: " + __filename + " contributions.csv [output_dir]");
}
// ==================
// End function defs
// ==================
if((argv._.length < 1)) {
usage();
process.exit(1);
}
if(argv.h || argv.help) {
usage(console.log);
process.exit(0);
}
var inFile = argv._[0];
var outDir = process.cwd();
if(argv._.length > 1) {
outDir = argv._[1];
}
settings.allowLineBreaks = argv.b || settings.allowLineBreaks;
settings.font.size = argv.fontSize || settings.font.size;
if(argv.country) {
settings.country = argv.country || settings.country;
settings.countryInverseMatch = false;
} else if(argv.notCountry) {
settings.country = argv.notCountry || settings.country;
settings.countryInverseMatch = true;
}
if(settings.country) {
settings.country = settings.country.replace(/\s+/, ' ');
} else {
}
settings.local = argv.local || settings.local;
settings.require = settings.require || [];
settings.ignore = settings.ignore || [];
if(argv.require) {
var reqs = argv.require.split(',');
settings.require = settings.require.concat(reqs);
}
if(argv.ignore) {
var igns = argv.ignore.split(',');
settings.ignore = settings.ignore.concat(igns);
}
settings.require = arrayToHash(settings.require);
settings.ignore = arrayToHash(settings.ignore);
if(easypost && argv.perk && !packages[argv.perk]) {
console.error("The specified perk is not defined in packages.js");
process.exit(1);
}
var numLabels = 0;
parseIndiegogo(inFile, function(err, line, person, next) {
if(err) return fail(err, line, person);
if(argv.perk) {
if(!person.perk) {
next();
return;
}
if(argv.perk.toLowerCase() != person.perk.toLowerCase()) {
next();
return;
}
}
findSize(person, function(err, person) {
if(err) return fail(err, line, person);
// if this person was size-filtered
if(!person) {
next();
return;
}
if(easypost && argv.perk && packages[argv.perk]) {
console.log("Generating package label (address and postage)");
buyShippingLabel(easypost, person, argv.perk, outDir, argv, function(err, shipment, filepath) {
if(err) {
console.error(err);
process.exit(1);
}
console.log("Writing file: " + filepath);
next();
});
numLabels++;
} else {
console.log("Generating letter label (address only)");
var label = new Label(settings);
if(!mailingLabel(label, line, person, settings.country, settings.countryInverseMatch, settings.local, settings.require, settings.ignore)) {
return false;
}
numLabels++;
var outPath = path.join(outDir, 'label'+person.pledge_id+'.png');
label.saveImage(outPath, function() {
console.log("Wrote label: " + outPath);
next();
});
}
});
}, function() {
console.log("Successfully wrote " + numLabels + " label(s).");
});