-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·141 lines (117 loc) · 2.87 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
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
#!/usr/bin/env node
'use strict'
const path = require('path')
const userhome = require('userhome')
const fs = require('fs')
const itunes = require('itunes-library-stream')
const async = require('async')
const chalk = require('chalk')
const mdls = require('./mdls')
// Options
const argv = require('yargs')
.alias('d', 'debug')
.boolean('debug')
.argv
const libraryPath = argv.libpath ||
path.resolve(userhome(), 'Music/iTunes/iTunes Music Library.xml')
const inputFiles = argv._
const debugMode = argv.debug ? true : false
const lowestMatch = debugMode ? 1 : 15
if (inputFiles.length === 0) {
console.error('ERROR: No files provided to check')
return
}
function debug(str) {
if (debugMode)
console.log(str);
}
const normalizeMd = (filename, data) => ({
filename: filename,
title: data['Title'],
album: data['Album'],
artist: data['Authors'] ? data['Authors'][0] : null,
size: data['Size'],
milliseconds: Math.floor(data['DurationSeconds'] * 1000)
})
function getFileData(file, done) {
fs.access(file, fs.constants.R_OK, err => {
if (err) {
console.error('ERROR: file "' + file + '" not readable')
done()
} else {
mdls(file, (err, data) => {
if (err) {
console.error('ERROR: ', err)
done()
} else {
done(null, normalizeMd(file, data))
}
})
}
})
}
function processInputFiles(files, done) {
async.map(files, getFileData, (err, list) => {
let anyFiles = list.reduce(
(prev, curr) => prev + (curr ? 1 : 0),
0)
if (!anyFiles) {
console.error('ERROR: no input files readable')
console.log(list)
process.exit(1)
}
done(err, list)
})
}
function getMatch(input, track) {
let matchFactor = 0
if (input.title === track.Name) {
debug('match name')
matchFactor += 10
}
if (input.album === track.Album) {
debug('match album')
matchFactor += 5
}
if (input.artist === track.Artist) {
debug('match artist')
matchFactor += 5
}
if (input.size === track.Size) {
debug('match size')
matchFactor += 20
}
if (input.milliseconds === track['Total Time']) {
debug('match time')
matchFactor += 10
}
return matchFactor
}
function showMatch(factor, input, track) {
console.log(chalk.green('MATCH:') + ' factor ' + factor + '\n' +
chalk.yellow('Input: ') +
input.filename +
chalk.yellow('\niTunes track: ') +
track.Location +
'\n')
// console.log('input:', input)
// console.log('track:', track)
}
function checkForDuplicates(err, files) {
let nFiles = files.length
fs.createReadStream(libraryPath)
.pipe(itunes.createTrackStream())
.on('data', track => {
for (let i = 0; i < nFiles; i++) {
let input = files[i]
let matchFactor = getMatch(input, track)
if (matchFactor >= lowestMatch) {
showMatch(matchFactor, input, track)
}
}
})
.on('end', () => {
console.log('FINISHED.')
})
}
processInputFiles(inputFiles, checkForDuplicates)