-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
230 lines (200 loc) · 5.83 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
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
222
223
224
225
226
227
228
229
230
'use strict';
const NodeID3 = require('node-id3');
const jschardet = require("jschardet");
const Iconv = require('iconv');
const fs = require("fs");
const path = require("path");
const minimist = require('minimist');
const { setMaxListeners } = require('process');
function main() {
new CorrectTagsCli();
}
class CorrectTagsCli {
constructor(params) {
console.log("hi, lets start")
this.args = minimist(process.argv.slice(2), {
alias: {
h: "help",
p: "path",
c: "command",
},
});
if (this.args.help != null) {
this.help();
return;
}
if (!this.checks()) {
return;
}
switch (this.args.command) {
case "getList":
new CorrectTags({ initPath: this.args.path }).getList();
break;
case "setList":
new CorrectTags().setList();
break;
default:
console.log("we need command arg -c, see help");
break;
}
}
help() {
console.log("hi, i am help:");
console.log(" we need args:");
console.log(" -h help");
console.log(" -p path to music folder");
console.log(" -c command");
console.log(" list commands:");
console.log(" auto - automatic change encoding");
console.log(" getList - create json file - list media whith tags");
console.log(" you can edit this file");
console.log(" setList - I take json file - list media whith edited tags");
}
checks() {
if (this.args.command == "getList") {
if (this.args.path == null ||
!(typeof (this.args.path) == "string") ||
this.args.path == "") {
console.log("we need path to music folder at -p argument");
return (false);
}
if (!fs.existsSync(this.args.path)) {
console.log(`there no such path ${this.args.path}`);
return (false);
}
}
if (this.args.command == "setList") { }
return (true);
}
}
class CorrectTags {
constructor(params) {
this.initPath = params?.initPath;
this.fileTagList = "tagEditorTMP.json";
}
getList() {
var lst = new GetFiles().run(this.initPath)
.filter(function (item, i, arr) {
return (item.includes(".mp3"));
}.bind(this));
console.log(`we found ${lst.length} tracks`)
var lstToSave = [];
lst.forEach(function (item, i, arr) {
const tags = NodeID3.read(item);
lstToSave.push({ path: item, tags: tags })
}.bind(this));
//var resultFilePath = path.join(this.initPath, "tagEditorTMP.js");
var resultFilePath = this.fileTagList;
if (fs.existsSync(resultFilePath)) {
fs.unlinkSync(resultFilePath);
}
fs.writeFile(
resultFilePath, JSON.stringify({ lst: lstToSave }, null, 2),
function (err) {
if (err) return console.log(err);
}
);
console.log(`You can take file at ${resultFilePath}`)
}
setList() {
console.log("setting list");
var data = JSON.parse(fs.readFileSync(this.fileTagList));
data.lst.forEach(function (item, i, arr) {
this.writeTags({ tags: item.tags, filePath: item.path });
}.bind(this));
console.log(`edited ${data.lst.length}`);
}
auto() {
console.clear();
console.log("getting files");
var lst = new GetFiles().run(this.initPath);
console.log(`got ${lst.length}`);
var lst = lst
.filter(function (item, i, arr) {
return (item.includes(".mp3"));
}.bind(this));
console.log(`${lst.length} mp3 filtered`);
//console.log(lst[0]);
lst
.forEach(function (item, i, arr) {
console.clear();
console.log(`current index:${i} of ${arr.length}, current path ${item}`);
this.processFile(item);
}.bind(this));
console.log(`${lst.length} mp3 done`);
}
processFile(filePath) {
const tags = NodeID3.read(filePath);
//console.log(jschardet.detect(tags.title));
var needEdit = false;
if (tags.title == "" || tags.title == null) {
tags.title = path.basename(filePath);
needEdit = true;
}
try {
Object.keys(tags).forEach(function (tagName, i, arr) {
if (typeof tags[tagName] == "string") {
var enc = jschardet.detect(tags[tagName]).encoding;
if (enc != "ascii") {
//tags[tagName] = conv.convert(buf).toString();
var conv = Iconv.Iconv("WINDOWS-1251", 'utf8');
var buf = Buffer.from(tags[tagName], 'binary');
tags[tagName] = conv.convert(buf).toString();
needEdit = true;
}
}
}.bind(this));
if (needEdit) { this.writeTags(tags, filePath); }
} catch (e) { console.log(e); }
}
writeTags(params) {
//const success = NodeID3.write(params.tags, params.filePath) // Returns true/Error
const success = NodeID3.update(params.tags, params.filePath) // Returns true/Error
}
test1() {
console.clear();
fs.readFile('test.txt', 'utf8', function (err, data) {
console.log("asd");
if (err) {
return console.log(err);
}
var enc = jschardet.detect(data);
console.log(enc);
console.log(data);
});
}
test2() {
var asd = fs.readdirSync(this.initPath);
}
}
class GetFiles {
constructor(dirPath) {
this.getAllFiles = function (dirPath, arrayOfFiles) {
this.files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
this.files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = this.getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(dirPath, "/", file))
}
}.bind(this))
return arrayOfFiles
}
}
run(dirPath) {
return this.getAllFiles(dirPath);
}
}
//todo:
//return list
//get list
//edit one hands
//edit one auto
//set special encoding
//help
//gui
// select encoding from list
//wui
//remove excess libs
main();