-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (172 loc) · 6.41 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
const fs = require('fs');
const path = require('path');
const ioUtils = require('./lib/io-utils');
const indexUtils = require('./lib/index-utils');
const workerFarm = require('worker-farm');
const workers = workerFarm(require.resolve('./lib/process-song'));
const workersZip = workerFarm(require.resolve('./lib/process-zip'));
const Song = require('./lib/song');
const inputDir = '..\\input';
const outputDir = 'F:\songs';
const duplicateDir = '..\\duplicates';
/**
* Write songs in a file when done
* @param songs
*/
const finish = (config, songs) => {
// Kill farms
workerFarm.end(workers);
workerFarm.end(workersZip);
// Create indexes
try {
fs.statSync(path.join(config.outputDir, 'index.json'));
console.log('Had one previous index. Merging');
const previousIndex = indexUtils.loadPreviousIndex(config.outputDir);
songs = songs.concat(previousIndex);
} catch (err) {}
indexUtils.createSongIndexByArtist(config.outputDir, songs);
indexUtils.createSongIndexByTitle(config.outputDir, songs);
indexUtils.saveIndex(config.outputDir, songs);
};
const setNextSongId = () => {
try {
fs.statSync(path.join(outputDir, 'index.json'));
console.log('Had one previous index. Setting song id to latest id');
let nextSongId = 0;
indexUtils.loadPreviousIndex(outputDir).forEach((s) => {
if (s.id > nextSongId) {
nextSongId = s.id;
}
});
Song.nextId = ++nextSongId;
} catch (err) {}
};
/**
* Read all inputs
*/
const readInputs = () => {
let allSongs = [];
let folderCount = 0;
setNextSongId();
console.log('Will read input');
ioUtils.readdir(path.join(inputDir))
.then((files) => {
console.log('Found ' + files.length + ' folders');
files.forEach((f) => {
const karaokeRoot = path.join(inputDir, f);
const parserFile = path.join(karaokeRoot, 'parser.json');
ioUtils.access(parserFile, fs.constants.F_OK)
.then(() => {
// File parser.js exists, we are in an official karaoke directory.
console.log('Parser file found: ' + parserFile);
ioUtils.readFile(parserFile, {encoding: 'utf8'})
.then((data) => {
console.log('Started processing folder ' + f);
folderCount++;
const config = JSON.parse(data);
config.inputDir = inputDir;
config.outputDir = outputDir;
config.duplicateDir = duplicateDir;
processFolder(karaokeRoot, f, config, (songs) => {
console.log('Finished processing folder ' + f);
allSongs = allSongs.concat(songs);
--folderCount;
console.log('There are ' + folderCount + ' left');
if (folderCount == 0) {
finish(config, allSongs);
}
});
})
.catch((err) => {
throw err;
});
})
.catch((err) => {
// This means parser.json does not exist.
console.log('This folder had no parser.json. Will be ignored');
});
});
})
.catch((err) => {
throw err;
});
};
/**
* Process a karaoke folder
* @param root the root folder containing the first level of songs
* @param collection the name of the collection
* @param config the parser configuration for this folder (pattern, etc...)
* @param callback when done
*/
const processFolder = (root, collection, config, callback) => {
console.log('Will process folder ' + root);
let fileCount = 0;
let children = 0;
let songs = [];
ioUtils.readdir(root)
.then((files) => {
files.forEach((f) => {
ioUtils.stat(path.join(root, f))
.then((stats) => {
if (stats.isFile() && f.toLowerCase().endsWith('.cdg')) {
// Found karaoke file
++fileCount;
workers(config, root, collection, f.substr(0, f.length - '.cdg'.length), Song.nextId++, (err, song) => {
if (!err) {
if (config.debug) {
console.log('Processed song ' + JSON.stringify(song));
}
if (!song.isDuplicate) {
songs.push(song);
}
}
if (--fileCount == 0 && children == 0) {
callback(songs);
}
});
} else if (stats.isFile() && f.toLowerCase().endsWith('.zip')) {
// Found zip file. Rename and move
++fileCount;
workersZip(config, root, collection, f.substr(0, f.length - '.zip'.length), Song.nextId++, (err, song) => {
if (!err) {
if (config.debug) {
console.log('Processed zip ' + JSON.stringify(song));
}
if (!song.isDuplicate) {
songs.push(song);
}
}
if (--fileCount == 0 && children == 0) {
callback(songs);
}
});
} else if (stats.isDirectory()) {
console.log('Found subfolder ' + f);
++children;
// Directory == new collection. Recursive call.
processFolder(path.join(root, f), collection + '__' + f, config, (songsOfChild) => {
--children;
console.log('Processed subfolder ' + collection + '. there are ' + children + ' subfolders left');
songs = songs.concat(songsOfChild);
if (fileCount == 0 && children == 0) {
callback(songs);
}
});
}
})
.catch((err) => {
console.log(err);
});
});
})
.catch((err) => {
console.log(err);
});
};
// Create output directories
ioUtils.mkdirp(path.join(outputDir, 'duplicates'));
ioUtils.mkdirp(path.join(duplicateDir, 'duplicates'));
ioUtils.mkdirp(path.join(outputDir, 'errors'));
ioUtils.mkdirp(path.join(outputDir, 'processed'));
// Read inputs
readInputs();