-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·52 lines (39 loc) · 1.46 KB
/
app.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
import * as fs from 'fs';
import { processDataFile } from './process-data-file.js';
import { generateCsv } from './generate-csv.js';
function saveAsJson(year, sourceFilename, data) {
const targetDir = `./json/${year}`;
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
const jsonFilePath = `${targetDir}/${sourceFilename}.json`;
const jsonStr = JSON.stringify(data, null, 2);
fs.writeFileSync(jsonFilePath, jsonStr);
}
function saveAsCsv(year, sourceFilename, data) {
const targetDir = `./csv/${year}`;
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
const csvFilePath = `${targetDir}/${sourceFilename}.csv`;
const csvStr = generateCsv(data);
fs.writeFileSync(csvFilePath, csvStr, 'utf-8');
}
async function processAllFiles() {
const years = fs
.readdirSync('./pdf')
.filter(dir => dir.match(/20\d{2}/));
for (const year of years) {
const files = fs.readdirSync(`./pdf/${year}`).filter(file => file.toLowerCase().endsWith('.pdf'));
for (const file of files) {
const pdfFilePath = `./pdf/${year}/${file}`;
console.log(`Parsing ${pdfFilePath}`);
const data = await processDataFile(pdfFilePath);
saveAsJson(year, file, data);
saveAsCsv(year, file, data);
}
}
}
processAllFiles()
.then(_ => console.log('Done.'))
.catch(console.error);