-
Notifications
You must be signed in to change notification settings - Fork 0
/
createInternetArchiveCsv.js
executable file
·80 lines (68 loc) · 1.99 KB
/
createInternetArchiveCsv.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
#!/usr/bin/env node
'use strict';
var Airtable = require('airtable');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const fs = require('fs');
const { argv } = require('process');
const fields = require('./fields');
const airtableFields = require('./airtableFields');
const {normalize} = require('./helpers');
const year = argv[2];
const month = argv[3];
let formula;
if (year) {
if (month) {
formula = `FIND("_${year}${month}",Identifier)>0`;
} else {
formula = `FIND("_${year}",Identifier)>0`;
}
}
async function getRecords () {
return new Promise(resolve => {
const allRecords = [];
try {
var base = new Airtable({apiKey: process.env.APIKEY}).base(process.env.BASE);
base('Oral Histories').select({
view: "Archival View (Comprehensive)",
cellFormat: "string",
timeZone: "America/New_York",
userLocale: "en-ca",
filterByFormula: formula,
fields: airtableFields
}).eachPage(function page(records, fetchNextPage) {
allRecords.push(...records);
fetchNextPage();
}, function done(err) {
if (err) {
console.error(err);
return process.exit(1);
} else {
resolve(allRecords);
}
});
} catch (e) {
console.error(e);
return process.exit(1);
}
});
}
function getCsvRow (record) {
return fields.reduce((row, field) => {
row[field.name] = field.getValue(record);
return row;
}, {});
}
const stagedDirectories = new Set(fs.readdirSync(process.env.IA_STAGING).map(normalize));
getRecords().then(airtableRecords => {
const rows = airtableRecords
.filter(record => stagedDirectories.has(normalize(record.get('Identifier'))))
.map(getCsvRow);
const header = fields.map(field => ({id: field.name, title: field.name}));
const csvWriter = createCsvWriter({
path: 'upload.csv',
header: header
});
return csvWriter.writeRecords(rows);
}).then(() => {
console.log('Done!');
});