-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSongs.js
128 lines (101 loc) · 3.59 KB
/
findSongs.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
var fs = require('fs');
var mm = require('musicmetadata');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/stream');
var trackSchema = new mongoose.Schema({
"title" :String,
"artist" :String,
"album" :String,
"location" :String
});
var db = mongoose.connection;
db.on('error', function (err) {
console.log('connection error', err);
});
db.once('open', function () {
console.log('connected.');
});
var track = mongoose.model('Track', trackSchema);
var walkPath = 'music';
var walk = function (dir, done) {
fs.readdir(dir, function (error, list) {
if (error) {
return done(error);
}
var i = 0;
(function next () {
var file = list[i++];
if (!file) {
return done(null);
}
file = dir + '/' + file;
fs.stat(file, function (error, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (error) {
next();
});
} else {
// do stuff to file here
var song = fs.createReadStream(file);
var parser = mm( song, function (err, m) {
if (err) console.log('Error on : ' + file);
track.find(
{title: m.title, album: m.album, artist : m.artist[0], location: file},
function(error, data){
if(data.length){
console.log(m.title + ' - already exists');
} else{
var insert = new track({
title: m.title,
artist: m.artist[0],
album: m.album,
location: file
});
insert.save(function (err, data) {
if (err) console.log(err);
else console.log('Saved : ', data );
});
} // End else
}); // End track find
});
next();
}
});
})();
});
};
// optional command line params
// source for walk path
process.argv.forEach(function (val, index, array) {
if (val.indexOf('source') !== -1) {
walkPath = val.split('=')[1];
}
});
console.log('-------------------------------------------------------------');
console.log('processing...');
console.log('-------------------------------------------------------------');
walk(walkPath, function(error) {
if (error) {
throw error;
} else {
console.log('-------------------------------------------------------------');
console.log('finished.');
console.log('-------------------------------------------------------------');
}
});
/*var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/play');
var schema = mongoose.Schema({
"_id": mongoose.Schema.Types.ObjectId,
"title" :String,
"artist" :String,
"album" :String,
"year" :String,
"location" :String
});
var tracks = mongoose.model('track', schema);
var results = tracks.find(function(err,f){
if (err) return console.error(err);
console.log(f);
});
*/