-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_postcard_data.js
More file actions
executable file
·228 lines (191 loc) · 8.01 KB
/
Copy pathfetch_postcard_data.js
File metadata and controls
executable file
·228 lines (191 loc) · 8.01 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
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
#!/usr/bin/env node
// Load environment variables from .env file
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const https = require('https');
// Configuration
const LOB_API_BASE = 'https://api.lob.com/v1';
const LOB_API_KEY = process.env.LOB_API_KEY; // Set this environment variable
if (!LOB_API_KEY) {
console.error('Error: LOB_API_KEY environment variable is required');
console.error('Please set it with: export LOB_API_KEY="your_api_key_here"');
process.exit(1);
}
// Function to make authenticated request to Lob API
function fetchPostcardData(postcardId) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.lob.com',
port: 443,
path: `/v1/postcards/${postcardId}`,
method: 'GET',
rejectUnauthorized: false,
headers: {
'Authorization': `Basic ${Buffer.from(LOB_API_KEY + ':').toString('base64')}`,
'Content-Type': 'application/json'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
try {
const postcardData = JSON.parse(data);
resolve(postcardData);
} catch (error) {
reject(new Error(`Failed to parse JSON for postcard ${postcardId}: ${error.message}`));
}
} else {
reject(new Error(`API request failed for postcard ${postcardId}: ${res.statusCode} - ${data}`));
}
});
});
req.on('error', (error) => {
reject(new Error(`Request failed for postcard ${postcardId}: ${error.message}`));
});
req.end();
});
}
// Function to parse CSV file
function parseCSV(csvContent) {
const lines = csvContent.trim().split('\n');
const headers = lines[0].split(',').map(header => header.replace(/"/g, ''));
const rows = [];
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
const values = lines[i].split(',').map(value => value.replace(/"/g, ''));
const row = {};
headers.forEach((header, index) => {
row[header] = values[index];
});
rows.push(row);
}
}
return { headers, rows };
}
// Function to escape CSV values
function escapeCSV(value) {
if (value === null || value === undefined) {
return '';
}
const stringValue = String(value);
if (stringValue.includes(',') || stringValue.includes('"') || stringValue.includes('\n')) {
return `"${stringValue.replace(/"/g, '""')}"`;
}
return stringValue;
}
// Function to convert object to CSV row
function objectToCSVRow(obj, headers) {
return headers.map(header => escapeCSV(obj[header] || '')).join(',');
}
// Function to extract original POST body fields from postcard data
function extractOriginalFields(postcardData) {
return {
id: postcardData.id,
mail_type: postcardData.mail_type,
from_name: postcardData.from?.name || '',
from_address_line1: postcardData.from?.address_line1 || '',
from_address_country: postcardData.from?.address_country || '',
from_address_state: postcardData.from?.address_state || '',
from_address_city: postcardData.from?.address_city || '',
from_address_zip: postcardData.from?.address_zip || '',
to_name: postcardData.to?.name || '',
to_address_line1: postcardData.to?.address_line1 || '',
to_address_country: postcardData.to?.address_country || '',
to_address_state: postcardData.to?.address_state || '',
to_address_city: postcardData.to?.address_city || '',
to_address_zip: postcardData.to?.address_zip || '',
description: postcardData.description || '',
metadata_user: postcardData.metadata?.user || '',
metadata_campaign_id: postcardData.metadata?.campaign_id || '',
metadata_environment: postcardData.metadata?.environment || '',
metadata_client: postcardData.metadata?.client || '',
size: postcardData.size || '',
front: postcardData.front || '',
back: postcardData.back || ''
};
}
// Main function
async function processCSV(inputFilePath) {
try {
console.log(`Processing CSV file: ${inputFilePath}`);
// Read the input CSV
const csvContent = fs.readFileSync(inputFilePath, 'utf8');
const { headers, rows } = parseCSV(csvContent);
console.log(`Found ${rows.length} postcard IDs to process`);
// Define output headers
const outputHeaders = [
'id', 'mail_type', 'from_name', 'from_address_line1', 'from_address_country',
'from_address_state', 'from_address_city', 'from_address_zip',
'to_name', 'to_address_line1', 'to_address_country', 'to_address_state',
'to_address_city', 'to_address_zip', 'description', 'metadata_user',
'metadata_campaign_id', 'metadata_environment', 'metadata_client',
'size', 'front', 'back'
];
const outputRows = [];
// Process each postcard ID
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const postcardId = row.id;
console.log(`Processing postcard ${i + 1}/${rows.length}: ${postcardId}`);
try {
const postcardData = await fetchPostcardData(postcardId);
const extractedFields = extractOriginalFields(postcardData);
outputRows.push(extractedFields);
console.log(`✓ Successfully fetched data for ${postcardId}`);
} catch (error) {
console.error(`✗ Failed to fetch data for ${postcardId}: ${error.message}`);
// Add empty row to maintain order
const emptyRow = { id: postcardId };
outputHeaders.forEach(header => {
if (header !== 'id') emptyRow[header] = '';
});
outputRows.push(emptyRow);
}
// Add a small delay to avoid rate limiting
if (i < rows.length - 1) {
await new Promise(resolve => setTimeout(resolve, 100));
}
}
// Generate output filename
const inputBasename = path.basename(inputFilePath, '.csv');
const outputFilename = `${inputBasename}_original_data.csv`;
const outputPath = path.join(path.dirname(inputFilePath), outputFilename);
// Write output CSV
const csvLines = [outputHeaders.join(',')];
outputRows.forEach(row => {
csvLines.push(objectToCSVRow(row, outputHeaders));
});
fs.writeFileSync(outputPath, csvLines.join('\n'));
console.log(`\n✓ Processing complete!`);
console.log(`✓ Output saved to: ${outputPath}`);
console.log(`✓ Processed ${outputRows.length} postcards`);
} catch (error) {
console.error(`Error processing CSV: ${error.message}`);
process.exit(1);
}
}
// Command line interface
function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Usage: node fetch_postcard_data.js <csv_file_path>');
console.log('Example: node fetch_postcard_data.js 3900_navaho_st_sw_postcards.csv');
process.exit(1);
}
const inputFilePath = args[0];
if (!fs.existsSync(inputFilePath)) {
console.error(`Error: File '${inputFilePath}' does not exist`);
process.exit(1);
}
processCSV(inputFilePath);
}
// Run the script
if (require.main === module) {
main();
}
module.exports = { processCSV, fetchPostcardData, extractOriginalFields };