Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 117 lines (76 sloc) 2.37 KB
#! /usr/bin/env node
/*
* usage: genm3u tracklist.txt target-dir playlist-filename.m3u
*/
var exec = require('child_process').exec,
fs = require('fs'),
readFile = require('fs').readFile;
var index = (function(){
var cache = undefined;
return function(path, callback){
if(cache){
return callback(undefined, cache);
}
exec('find '+path+' > /tmp/file-index', function(err, stdout, stderr){
if(err) return callback(err);
readFile('/tmp/file-index', callback);
});
};
})();
var songs = (function(){
var cache = undefined;
return function(filename, callback){
if(cache){
return callback(undefined, cache);
}
readFile(filename, function(error, bf){
if(error) return callback(error);
cache = bf.toString().split('\n').map(function(name){
var _ = name.split(/\s*-\s*/),
artist = _[0],
title = _[1];
return { 'artist':artist, 'title': title };
});
return callback(undefined, cache);
});
};
})();
function find(sourceFilename, indexPath, outputPath){
console.log(arguments);
var found = [];
songs(sourceFilename,function(error, songs){
if(error) throw error;
index(indexPath, function(error, index){
if(error) throw error;
var i = songs.length,
song;
(function iter(i){
song = songs[i];
if(i >= songs.length){
fs.writeFileSync(outputPath,found.join('\n'));
return;
}
var artistRe = new RegExp('.+'+song.artist+'.+', 'gi');
if(!artistRe.test(index)){
console.error('Artist Not Found: ', song.artist);
return iter(i+1);
}
if(!song.title){
console.error('No title: ', song);
return iter(i+1);
}
exec('cat /tmp/file-index | grep -i "'+song.artist+'"', function(error, matchings){
if(error) throw error;
var test = (new RegExp('.+'+song.title+'.+', 'i')).exec(matchings) || (new RegExp('.+'+song.title.replace(/\s+/g,'_')+'.+', 'i')).exec(matchings);
if(test){
found.push(test[0]);
} else {
console.error('Song not found:', song.title, ' by ', song.artist);
}
iter(i+1);
});
})(0);
});
});
}
find(process.argv[2], process.argv[3], process.argv[4]);
Something went wrong with that request. Please try again.