-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
110 lines (89 loc) · 2.52 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
var spawn = require("child_process").spawn
function EyeD3(options) {
if(!options) options = {}
if(!options.eyed3_path) options.eyed3_path = "eyeD3"
this.options = options
}
/**
* Reads the meta data of the given file
*
* @param {String} file
* @param {Function} callback
*/
EyeD3.prototype.readMeta = function(file, callback) {
var args = ['--no-color', '--rfc822', file]
, p = spawn(this.options.eyed3_path, args)
, allData = ''
p.stdout.on('data', function (data) {
allData += data
})
p.on('exit', function (exitCode) {
if(exitCode !== 0)
return callback(new Error('eyeD3 exit code: ' + exitCode))
var response = {}
, lines = allData.split('\n')
, line, match
for(var i = 0; i < lines.length; i++) {
line = lines[i]
if(match = line.match(/^(.*): (.*)$/i)) {
response[match[1].toLowerCase()] = match[2]
}
}
callback(null, response)
})
}
/**
* Reads the lyrics of the given file
*
* @param {String} file
* @param {Function} callback
*/
EyeD3.prototype.readLyrics = function(file, callback) {
var args = ['--no-color', '--verbose', file]
, p = spawn(this.options.eyed3_path, args)
, allData = ''
p.stdout.on('data', function (data) {
allData += data
})
p.on('exit', function (exitCode) {
if(exitCode !== 0)
return callback(new Error('eyeD3 exit code: ' + exitCode))
var response = '';
if(match = allData.match(/<.*lyric\/text.*:\s(.*)\s\[Lang:[^\]]*\]\s*\[Desc:[^\]]*\]>/im)) {
response = match[1]
}
callback(null, response)
})
}
/**
* Updates the meta data of the given file
*
* @param {String} file
* @param {Object} meta
* @param {Function} callback
*/
EyeD3.prototype.updateMeta = function(file, meta, callback) {
var args = this.buildArgs(meta).concat([file])
, p = spawn(this.options.eyed3_path, args)
p.on('exit', function (exitCode) {
if(exitCode !== 0)
return callback(new Error('eyeD3 exit code:' + exitCode))
if(callback) callback()
})
}
/**
* Builds an argument error out of the given meta data
*
* @param {Object} meta
* @return {Array} The arguments for our spawn() call
*/
EyeD3.prototype.buildArgs = function(meta) {
var args = []
if(meta.artist) args.push('-a', meta.artist)
if(meta.title) args.push('-t', meta.title)
if(meta.album) args.push('-A', meta.album)
if(meta.comment) args.push('-c', '::' + meta.comment)
if(meta.lyrics) args.push('-L', '::' + meta.lyrics)
return args
}
module.exports = EyeD3