-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
247 lines (231 loc) · 7.13 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const klaw = require('klaw');
const spawn = require('child_process').spawn;
const fs = require('fs-extra');
const path = require('path');
// if your target drive doesnt have enough space, thats on you
const inDir = '/home/kisama/Music/_incoming';
const outDir = '/home/kisama/Music/_outgoing';
// TODO? support more than flac? thats all I have so whatever
const sourceFormat = 'flac';
const targetFormat = 'ogg';
// I rather include unwanted files than possibly exclude wanted files
const bannedOtherFileTypes = [
'.cue',
'.db',
'.log',
'.m3u8',
'.thumb',
'.accurip'
]
let audioCount = 0;
let audioCompeleted = 0;
const albums = {}; // we are assuming the top level folder is an album for now
let albumCount = 0;
let albumsCompleted = 0;
let dump = [];
const delay = ms =>
new Promise(resolve =>
setTimeout(() => resolve(), ms));
(async () => {
await fs.ensureDir(outDir);
// only wrote this for handling the massive jcore library
/*await getAlbumFile();
console.log(Object.keys(albums).length);
if (!albums || !Object.keys(albums).length) {
await scanDir();
organizeFiles();
}
await fs.writeJson('./albums.json', albums);*/
await scanDir();
organizeFiles();
albumCount = Object.keys(albums).length;
console.log(`${audioCount} files, ${albumCount} albums to clone`);
await cloneAlbums();
})();
async function getAlbumFile() {
try {
albums = await fs.readJson('./albums.json');
} catch {
}
return;
}
async function cloneAlbums() {
const stack = [];
const inDirObj = path.parse(inDir);
const folderNames = Object.keys(albums);
for (let i = 0; i < folderNames.length; i++) {
stack.push(albums[folderNames[i]])
while (stack.length) {
let currentFolder = stack.pop();
// process folder files
const newFolderPath = `${outDir}${currentFolder.path.split(inDirObj.base)[1]}`;
await fs.ensureDir(newFolderPath);
for (let j = 0; j < currentFolder.audioFiles.length; j++) {
const fileObj = path.parse(currentFolder.audioFiles[j]);
await convertAudioFile(
currentFolder.audioFiles[j],
`${newFolderPath}/${fileObj.name}.${targetFormat}`
);
audioCompeleted += 1;
console.log(`${audioCount-audioCompeleted} tracks left`)
}
for (let j = 0; j < currentFolder.otherFiles.length; j++) {
const fileObj = path.parse(currentFolder.otherFiles[j]);
await copyFile(
currentFolder.otherFiles[j],
`${newFolderPath}/${fileObj.base}`
);
}
// add nested folders to the stack if any
const folderNames = Object.keys(currentFolder.folders);
for (let j = 0; j < folderNames.length; j++) {
stack.push(currentFolder.folders[folderNames[j]])
}
}
albumsCompleted+=1
console.log(`completed album "${folderNames[i]}"`)
}
console.log(`${albumsCompleted} albums, ${audioCompeleted} songs processed`)
}
let iterations = 0;
function organizeFiles() {
const inDirObj = path.parse(inDir);
console.log(`dump file count ${dump.length}`)
let i = dump.length;
while (i--) {
console.log(`${i} on path `, dump[i])
const fileObj = path.parse(dump[i]);
const pathArr = dump[i].split(inDirObj.base)[1].split('/');
pathArr.splice(0,1);
const currentFolder = recurseAlbum(albums[pathArr[0]],pathArr,0);
if (currentFolder) {
// ran into path.parse results that were all fucked up, lets just parse
// the last item in the array to clean things up
const lastObj = path.parse(pathArr[pathArr.length-1]);
if (!lastObj.ext.length) {
// directory
if (!currentFolder.folders[fileObj.base]) {
// dont overwrite
currentFolder.folders[fileObj.base] = {
path: dump[i],
audioFiles: [],
otherFiles: [],
folders: {},
}
}
dump.splice(i, 1)
} else {
// file
// TODO: support other lossless formats maybe?? dont care
if (lastObj.ext.toLowerCase().endsWith(sourceFormat)) {
if (currentFolder.audioFiles.indexOf(dump[i]) === -1) {
currentFolder.audioFiles.push(dump[i]);
audioCount += 1;
}
dump.splice(i, 1)
} else if (!bannedOtherFileTypes.includes(lastObj.ext.toLowerCase())) {
if (currentFolder.otherFiles.indexOf(dump[i]) === -1) {
currentFolder.otherFiles.push(dump[i]);
}
dump.splice(i, 1)
}else {
// remove unwanted file
dump.splice(i, 1)
}
}
}
}
if (!dump.length) {
console.log(`completed organization after ${iterations} iterations`);
return;
}
// basically, we better not encounter n levels of nesting, because shit damn
if (dump.length && iterations > 10) {
// we failed to parse all the thingiess
console.log(`${dump.length} files left, quitting the process`);
console.log(`
${Object.keys(albums).length} albums parsed
${audioCount} songs parsed`);
} else {
console.log('parsing all files again');
iterations += 1;
organizeFiles();
}
}
function recurseAlbum(folder, arr, index) {
if ((index+2) < arr.length) {
if (!folder) return;
return recurseAlbum(folder.folders[arr[index+1]], arr, index+1);
} else {
return folder;
}
}
async function scanDir() {
const inDirObj = path.parse(inDir);
const promise = new Promise(resolve =>
klaw(inDir)
.on('data', item => {
const fileObj = path.parse(item.path);
console.log('path',item.path);
// ignore base directory
if (item.path !== inDir) {
if (fileObj.dir.endsWith(inDirObj.base)) {
// create album
const album = {
path: item.path,
audioFiles: [],
otherFiles: [],
folders: {},
};
albums[fileObj.base] = album;
} else {
dump.push(item.path);
}
}
})
.on('end', () => resolve()));
return promise;
}
async function convertAudioFile(source, target) {
console.log(`source:${source}, target:${target}`);
const promise = new Promise((resolve, reject) => {
const p = spawn('sox', [source, target]);
p.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
p.stderr.on('data', (data) => {
if (data.indexOf('sox WARN dither') > -1) {
} else {
console.error(data);
}
//console.error(`stderr: ${data}`);
});
p.on('close', (code) => {
if (code === 0) resolve();
else {
throw `received code ${code} attempting to covert audio`
resolve(); // fuck it for now
}
});
});
return promise;
}
async function copyFile(from, to) {
const promise = new Promise((resolve, reject) => {
const p = spawn('cp', [from, to]);
p.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
p.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
p.on('close', (code) => {
if (code === 0) resolve();
else {
console.log(`received code ${code} attempting to copy file ${from},${to}`)
resolve();
}
});
});
return promise;
}