-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_scanner.js
116 lines (101 loc) · 2.9 KB
/
file_scanner.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
var sys = require('sys');
var fs = require('fs');
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var db = new Db('files-index', new Server('localhost', Connection.DEFAULT_PORT, {}), {native_parser:true});
var Id3 = require('./id3-reader').Id3Reader;
var totalOpenFiles = 0;
var totalFiles = 0;
var totalFilesParsed = 0;
var filesQueue = [];
var results = [];
function check_queue() {
if(totalOpenFiles < 101) {
if(filesQueue.length > 0) {
read_file(filesQueue.pop());
}
}//else we may want to pause and check again?
}
function read_file(trackInfo) {
id3 = new Id3(trackInfo.path + "/" + trackInfo.name);
totalOpenFiles++;
id3.readData(function(data) {
totalOpenFiles--;
totalFilesParsed++;
check_queue();
var tempName = trackInfo.name.split(".");
trackInfo.ext = tempName[tempName.length - 1];
var lowercaseName = trackInfo.name.toLowerCase();
var nameNoExt = lowercaseName.replace("." + trackInfo.ext, "");
var checkChars = ["(", ")", "&", "^"];
checkChars.forEach(function(remove) {
while(nameNoExt.indexOf(remove) >= 0) {
nameNoExt = nameNoExt.replace(remove, "");
}
});
trackInfo.tags = nameNoExt.split(" ");
if (data) {
trackInfo.title = data['TIT2'];
trackInfo.album = data['TALB'];
trackInfo.track = parseInt(data['TRCK']);
trackInfo.artist = data['TPE1'];
} else {
trackInfo.title = nameNoExt;
trackInfo.artist = "unknown";
trackInfo.album = "unknown";
}
results.push(trackInfo);
sys.print(totalFilesParsed + '/' + totalFiles + ' parsed ('+totalFilesParsed/totalFiles+'%)\n');
if(totalFilesParsed >= totalFiles) {
store_data();
}
});
}
function read_directory(path) {
fs.readdir(path, function(err, files) {
files.forEach(function (filename) {
fs.stat(path + "/" + filename, function(err, stat) {
if(stat) {
if (!stat.isDirectory()) {
var tempResults = {};
tempResults.path = path;
tempResults.size = stat.size;
tempResults.name = filename;
totalFiles++;
if(totalOpenFiles < 100) {
read_file(tempResults);
} else {
//log the missed files to a queue, then check the queue every second
filesQueue.push(tempResults);
}
} else {
read_directory(path + "/" + filename);
}
} else {
console.log("skipping: " + path + "/" + filename);
}
});
});
});
}
function store_data() {
console.log("final callback");
console.log(results);
db.open(function(err, db) {
if (err) { throw err; }
db.collection('files', function(err, collection) {
collection.remove(function(err, collection) {
results.forEach(function(item) {
collection.insert(item);
});
//collection.createIndex('track');
//collection.createIndex('album');
//collection.createIndex('artist');
//collection.createIndex('tags');
db.close();
});
});
});
}
read_directory("media");