forked from benkaiser/stretto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary_functions.js
More file actions
221 lines (205 loc) · 6.69 KB
/
library_functions.js
File metadata and controls
221 lines (205 loc) · 6.69 KB
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
var fs = require('fs');
var mm = require('musicmetadata');
var taglib = require('taglib');
var md5 = require('MD5');
var util = require(__dirname + '/util.js');
var config = require(__dirname + '/config').config();
var running = false;
var hard_rescan = false;
var app = null;
var cnt = 0;
var song_list = [];
var song_extentions = ["mp3", "m4a", "aac", "ogg", "wav", "flac", "raw"];
function findNextSong(){
if(cnt < song_list.length && running){
findSong(song_list[cnt], function(err){
if(err){
console.log({error: err, file: song_list[cnt]});
}
cnt++;
setTimeout(findNextSong, 0);
});
} else {
console.log("finished!");
broadcast("update", {count: song_list.length, completed: song_list.length, details: "Finished"});
// reset for next scan
cnt = 0;
song_list = [];
running = false;
}
}
function findSong(item, callback){
app.db.songs.findOne({location: item}, function(err, doc){
// only scan if we haven't scanned before, or we are scanning every document again
if(doc == null || hard_rescan){
// insert the new song
var parser = new mm(fs.createReadStream(item));
parser.on('metadata', function(result){
// add the location
var song = {
title: result.title,
album: result.album,
artist: result.artist,
albumartist: result.albumartist,
display_artist: normaliseArtist(result.albumartist, result.artist),
genre: result.genre,
year: result.year,
duration: result.duration,
play_count: (doc == null) ? 0 : doc.play_count || 0,
location: item
};
// write the cover photo as an md5 string
if(result.picture.length > 0){
pic = result.picture[0];
pic["format"] = pic["format"].replace(/[^a-z0-9]/gi, '_').toLowerCase();
filename = __dirname + '/dbs/covers/' + md5(pic['data']) + "." + pic["format"];
song.cover_location = filename;
fs.exists(filename, function(exists){
if(!exists){
fs.writeFile(filename, pic['data'], function(err){
if(err) console.log(err);
console.log("Wrote file!");
});
}
})
}
if(doc == null){
// insert the song
app.db.songs.insert(song, function (err, newDoc){
taglib_fetch(item, newDoc._id);
// update the browser the song has been added
broadcast("update", {
count: song_list.length,
completed: cnt,
details: "Added: " + newDoc["title"] + " - " + newDoc["albumartist"]
});
});
} else if (hard_rescan){
app.db.songs.update({location: item}, song, {}, function(err, numRplaced){
taglib_fetch(item, doc._id);
broadcast("update", {
count: song_list.length,
completed: cnt,
details: "Updated: " + song["title"] + " - " + song["artist"]
});
})
}
});
parser.on('done', function (err) {
if (err) {
var ext = "";
if(item.lastIndexOf(".") > 0) {
ext = item.substr(item.lastIndexOf(".")+1, item.length);
}
// if it was a metadata error and the file appears to be audio, add it
if(err.toString().indexOf("Could not find metadata header") > 0
&& util.contains(song_extentions, ext)){
console.log("Could not find metadata. Adding the song by filename.");
// create a song with the filename as the title
var song = {
title: item.substr(item.lastIndexOf("/") + 1, item.length),
album: "Unknown (no tags)",
artist: "Unknown (no tags)",
albumartist: "Unknown (no tags)",
display_artist: "Unknown (no tags)",
genre: "Unknown",
year: "Unknown",
duration: 0,
play_count: (doc == null) ? 0 : doc.play_count || 0,
location: item
};
if(doc == null){
app.db.songs.insert(song, function (err, newDoc){
taglib_fetch(item, newDoc._id);
// update the browser the song has been added
broadcast("update", {
count: song_list.length,
completed: cnt,
details: "Added: " + newDoc["title"]
});
});
} else if (hard_rescan){
app.db.songs.update({location: item}, song, {}, function(err, numRplaced){
taglib_fetch(item, doc._id);
broadcast("update", {
count: song_list.length,
completed: cnt,
details: "Updated: " + song["title"]
});
})
}
callback(null);
} else {
callback(err);
}
} else {
callback(null);
}
});
} else {
// TODO: make this less frequent
// broadcast("update", {
// count: song_list.length,
// completed: cnt,
// details: "Already scanned item: " + item
// });
// perform rescan? hard rescan option?
callback(null);
}
})
}
function normaliseArtist(albumartist, artist){
if(typeof(albumartist) != 'string'){
if(albumartist.length == 0){
albumartist = '';
} else {
albumartist = albumartist.join('/');
}
}
if(typeof(artist) != 'string'){
if(artist.length == 0){
artist = '';
} else {
artist = artist.join('/');
}
}
return (artist.length > albumartist.length) ? artist : albumartist;
}
function taglib_fetch(path, id){
// use taglib to fetch duration
taglib.read(path, function(err, tag, audioProperties) {
app.db.songs.update({ _id: id }, { $set: { duration: audioProperties.length} });
});
}
// clear all the songs with `location` not in the dbs
function clearNotIn(list){
app.db.songs.remove({location: { $nin: list }}, {multi: true}, function(err, numRemoved){
console.log(numRemoved + " tracks deleted");
});
}
exports.scanItems = function(app_ref, locations){
app = app_ref;
hard_rescan = true;
running = true;
song_list = song_list.concat(locations);
findNextSong();
}
exports.scanLibrary = function(app_ref, hard){
app = app_ref;
hard_rescan = hard;
util.walk(config.music_dir, function(err, list){
if(err){
console.log(err);
}
clearNotIn(list);
song_list = list;
findNextSong();
});
running = true;
}
exports.stopScan = function(app){
running = false;
}
function broadcast(id, message){
app.io.room('scanners').broadcast(id, message);
}