-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
81 lines (69 loc) · 2.84 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
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
const fs = require('fs/promises');
const path = require('path');
const { parse } = require('csv-parse/sync');
(async () => {
try {
// File paths (these can be adjusted as needed)
const importFilePath = path.resolve(__dirname, 'import_file.csv');
const artistFilePath = path.resolve(__dirname, 'artist_ids.csv');
const outputFilePath = path.resolve(__dirname, 'output.json');
// Read and parse the import file
const importFileContent = await fs.readFile(importFilePath, 'utf-8');
const importData = parse(importFileContent, { columns: true, skip_empty_lines: true });
// Read and parse the artist file
const artistFileContent = await fs.readFile(artistFilePath, 'utf-8');
const artistData = parse(artistFileContent, { columns: true, skip_empty_lines: true });
// Create a mapping of artist names to their IDs
const artistMap = artistData.reduce((map, row) => {
map[row.name] = row.id;
return map;
}, {});
// Helper function to process comma-separated strings into arrays
const processArrayField = (field) => {
if (!field || field === 'None' || field === 'notes' || field === 'keywords, keyword') {
return [];
}
return field.split(',').map(item => item.trim()).filter(item => item);
};
// Helper function to format duration
const formatDuration = (duration) => {
if (!duration) return null;
// Remove decimal portion and ensure proper format
return duration.replace(/^0?(\d+):(\d+)\.\d+$/, '$1:$2');
};
// Group tracks by album (CDTitle)
const albums = {};
importData.forEach(entry => {
const artistId = artistMap[entry.Artist] || null;
const formattedDuration = formatDuration(entry.Duration);
if (!albums[entry.CDTitle]) {
albums[entry.CDTitle] = {
status: 'draft',
library: entry.Library.toLowerCase(),
artist: artistId,
title: entry.CDTitle,
year_released: entry.year_released || null,
tracks: []
};
}
albums[entry.CDTitle].tracks.push({
title: entry.TrackTitle,
year: null,
length: formattedDuration,
scotts_picks: processArrayField(entry.Notes),
gregs_picks: processArrayField(entry.CDDescription),
master: entry.Tape || "NULL",
description: entry.Description || "",
bpm: parseInt(entry.BPM) || null,
tags: processArrayField(entry.Keywords)
});
});
// Convert the albums object to an array
const transformedData = Object.values(albums);
// Write the transformed data to the output JSON file
await fs.writeFile(outputFilePath, JSON.stringify(transformedData, null, 2), 'utf-8');
console.log('Processing completed. Output file created:', outputFilePath);
} catch (error) {
console.error('Error processing files:', error);
}
})();