-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mp3DownTagger.js
417 lines (269 loc) · 16.5 KB
/
Mp3DownTagger.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import {downloadSong} from "ytdl-mp3";
import csv from "csv-parser";
import fs from "fs";
import internetAvailable from "internet-available";
import {assignSongs2Download, checkAndClearInput, getSpotifySearchLinks, getSpotifyHTMLInfo, processSpotifyAutoResults, getYoutubeHTMLInfo, tagFile, checkAccessToken} from "./downTag.js"
import {Playlist} from "./playlist.js"
import {prepareString4Comparison, delay, titleIsPresent, checkAutoTagModePresence, isEmptyObject} from "./utils.js"
import path from "path";
import {fileURLToPath} from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const config = JSON.parse(fs.readFileSync(__dirname + "/config.json")); //__dirname is the parent directory of the current file "./"
let songs2Download = [];
let downloadProgressVar = new Array(50);
let currentSong2Download = null;
init();
const playlist = new Playlist({
playlist2Create: [],
playlistProgressVar: new Array(50),
matrixPath: null,
sortingCriteria: "",
sortingDirection: 0,
globalOrders: "",
globalTree: {},
finalArrayRecursive: [],
elements2Order: 0,
});
function init() {
config.downtag_mode.output_music_directory = config.downtag_mode.output_music_directory.replaceAll("\\", "/");
config.playlist_mode.output_playlist_directory = config.playlist_mode.output_playlist_directory.replaceAll("\\", "/");
for (let i = 0; i < config.playlist_mode.folders2Scan.length; i++) {
config.playlist_mode.folders2Scan[i] = config.playlist_mode.folders2Scan[i].replaceAll("\\", "/");
}
config.playlist_mode.phone_adaptation_settings.output_phone_playlist_directory = config.playlist_mode.phone_adaptation_settings.output_phone_playlist_directory.replaceAll("\\", "/");
downloadProgressVar.fill(" ");
}
function checkMusicDirectory() {
try {
let fd = fs.opendirSync(config.downtag_mode.output_music_directory)
fd.closeSync();
return true;
} catch (exception) {
if (exception.errno === -4058) {
console.log("$ The music directory provided doesn't exist\n");
}
return false;
}
}
async function downTagMode() {
fs.createReadStream(config.downtag_mode.csv_filename)
.on("error", function(error) {
if (error.errno === -4058) {
console.log("$ The input file provided doesn't exist\n");
}
})
.pipe(csv())
.on("data", (data) => songs2Download.push(data))
.on("end", async () => { //Event that is raised when there is no more data to read
assignSongs2Download(songs2Download);
let checkedCleanedSongs = checkAndClearInput(songs2Download);
if (checkedCleanedSongs.bool) {
songs2Download = checkedCleanedSongs.songs2Download;
let autoTagModePresence = checkAutoTagModePresence(songs2Download);
if (autoTagModePresence) {
await checkAccessToken();
}
if (!autoTagModePresence || (autoTagModePresence && config.downtag_mode.client_id.localeCompare("") !== 0 && config.downtag_mode.client_secret.localeCompare("") !== 0)) {
for (let i = 0; i < songs2Download.length; i++) {
if (songs2Download[i].youtube_url.localeCompare("") != 0 && songs2Download[i].spotify_url.localeCompare("") != 0 && songs2Download[i].song.localeCompare("") != 0) { //Downloader and tagger
if (!fs.existsSync(config.downtag_mode.output_music_directory + "/" + songs2Download[i].song + ".mp3")) {
console.log("Downloading " + songs2Download[i].song + ".mp3...");
const filename = await downloadSong(songs2Download[i].youtube_url, {
getTags: false,
outputDir: config.downtag_mode.output_music_directory,
audioFile: songs2Download[i].song
});
console.log("- " + songs2Download[i].song + ".mp3 downloaded");
console.log("Tagging " + songs2Download[i].song + ".mp3...");
let spotifyMetadata = {}
if (songs2Download[i].spotify_url.localeCompare("auto") == 0) { //Get the first five links when auto mode is enabled
let spotifySearchLinks = await getSpotifySearchLinks(songs2Download[i].song, songs2Download[i].extra_information);
let spotifyAutoMetadata = []
for (let j = 0; j < config.downtag_mode.number_of_spotify_searches_auto_mode; j++) {
spotifyAutoMetadata.push(await getSpotifyHTMLInfo(spotifySearchLinks[j]))
await delay(config.downtag_mode.delay_seconds_spotify_searches + "000");
}
spotifyMetadata = processSpotifyAutoResults(songs2Download[i].song, spotifyAutoMetadata, songs2Download[i].extra_information);
}
else {
spotifyMetadata = await getSpotifyHTMLInfo(songs2Download[i].spotify_url);
}
if (Object.keys(spotifyMetadata).length !== 0) {
let coincidence = false;
for (let j = 0; j < spotifyMetadata.tracks.length && !coincidence; j++) {
if (titleIsPresent(prepareString4Comparison(spotifyMetadata.tracks[j].songName), songs2Download[i].song)) {
await tagFile(spotifyMetadata, songs2Download[i].song);
coincidence = true;
}
}
if (!coincidence) {
console.log("$ This song can't be found on the spotify link you provided\n");
}
}
else {
console.log("$ This song can't be tagged in auto mode\n");
}
}
else {
console.log("$ " + songs2Download[i].song + ".mp3 already exists\n"); //The user must provide an alternative name (i = 1, 2, 3...)
}
}
else if (songs2Download[i].youtube_url.localeCompare("") != 0 && songs2Download[i].spotify_url.localeCompare("") == 0) { //Downloader
const videoInfo = await getYoutubeHTMLInfo(songs2Download[i].youtube_url);
let mp3Name;
let fileNameProvided = false;
if (songs2Download[i].song.localeCompare("") != 0) {
fileNameProvided = true;
mp3Name = songs2Download[i].song;
}
else {
mp3Name = videoInfo.title.replace(/\s+/g, " ").trim();
}
if ((fileNameProvided && !fs.existsSync(config.downtag_mode.output_music_directory + "/" + mp3Name + ".mp3")) || (!fileNameProvided && !fs.existsSync(config.downtag_mode.output_music_directory + "/" + mp3Name + ".mp3"))) {
currentSong2Download = mp3Name;
console.log("Downloading " + currentSong2Download + ".mp3...");
const filename = await downloadSong(songs2Download[i].youtube_url, {
getTags: false,
outputDir: config.downtag_mode.output_music_directory,
audioFile: mp3Name
});
console.log("- " + mp3Name + ".mp3 downloaded\n");
}
else {
console.log("$ " + mp3Name + ".mp3 already exists\n"); //The user must provide an alternative name (i = 1, 2, 3...)
}
}
else if (songs2Download[i].youtube_url.localeCompare("") == 0 && songs2Download[i].spotify_url.localeCompare("") != 0) { //Tagger
if (fs.existsSync(config.downtag_mode.output_music_directory + "/" + songs2Download[i].song + ".mp3")) {
console.log("Tagging " + songs2Download[i].song + ".mp3...");
let spotifyMetadata = {}
if (songs2Download[i].spotify_url.localeCompare("auto") == 0) { //Get the first five links when auto mode is enabled
let spotifySearchLinks = await getSpotifySearchLinks(songs2Download[i].song, songs2Download[i].extra_information);
let spotifyAutoMetadata = []
for (let j = 0; j < config.downtag_mode.number_of_spotify_searches_auto_mode; j++) {
spotifyAutoMetadata.push(await getSpotifyHTMLInfo(spotifySearchLinks[j]))
await delay(config.downtag_mode.delay_seconds_spotify_searches + "000");
}
spotifyMetadata = processSpotifyAutoResults(songs2Download[i].song, spotifyAutoMetadata, songs2Download[i].extra_information);
}
else {
spotifyMetadata = await getSpotifyHTMLInfo(songs2Download[i].spotify_url);
}
if (Object.keys(spotifyMetadata).length !== 0) {
let coincidence = false;
for (let j = 0; j < spotifyMetadata.tracks.length && !coincidence; j++) {
if (titleIsPresent(prepareString4Comparison(spotifyMetadata.tracks[j].songName), songs2Download[i].song)) {
await tagFile(spotifyMetadata, songs2Download[i].song);
coincidence = true;
}
}
if (!coincidence) {
console.log("$ This song can't be found on the spotify link you provided\n");
}
}
else {
console.log("$ This song can't be tagged in auto mode\n");
}
}
else {
console.log("$ " + songs2Download[i].song + ".mp3 doesn't exist\n"); //The user must provide an alternative name (i = 1, 2, 3...)
}
}
else {
console.log("$ Csv file format is not correct");
}
}
}
}
else {
console.log("$ Some input value is not correct. Make sure you have filled every field properly and you don't have ilegal characters as filenames");
}
})
}
function playlistMode() {
fs.createReadStream(config.playlist_mode.csv_playlist_filename)
.on("error", function(error) {
if (error.errno === -4058) {
console.log("$ The input file provided doesn't exist\n");
}
})
.pipe(csv())
.on("data", (data) => playlist.playlist2Create.push(data))
.on("end", () => {
let checkedCleanedPlaylist = playlist.checkAndClearInput();
if (checkedCleanedPlaylist) {
playlist.playlistProgressVar.fill(" ");
playlist.matrixPath = new Array(playlist.playlist2Create.length);
for (let i = 0; i < playlist.playlist2Create.length; i++) {
playlist.matrixPath[i] = []; //Each position will be an array
let playlistName = playlist.playlist2Create[i].playlist_name;
if (playlistName === undefined) {
playlistName = playlist.createPlayListName();
}
else {
playlistName = playlistName + ".m3u";
}
if (config.playlist_mode.substitute_playlist_files || !fs.existsSync(config.playlist_mode.output_playlist_directory + "/" + playlistName)) {
config.playlist_mode.folders2Scan.forEach(folder => { //Alphabetic sorting, etc
let files = fs.readdirSync(folder);
console.log("Creating " + playlistName + "...");
for (let j = 0; j < files.length; j++) {
playlist.printPlaylistCreationProgress(j, files.length);
if (playlist.includeFileExtensions(files[j])) { //Filter music files
let path = (folder + "/" + files[j]).replaceAll("\\", "/");
playlist.includeSong(path, i);
playlist.elements2Order = playlist.matrixPath[i].length;
}
}
});
console.log("Sorting " + playlistName);
let sortedArray = playlist.order(i);
playlist.finalArrayRecursive = [];
playlist.globalTree = {};
playlist.sortingCriteria = "";
playlist.sortingDirection = 0;
playlist.createPlaylist(sortedArray, playlistName);
console.log("+ " + playlistName + " was created\n");
if (config.playlist_mode.phone_adaptation_settings.phone_playlist) {
console.log("+ " + playlistName + " - Phone was created\n");
}
}
else {
console.log("$ This playlist already exists\n");
}
}
}
else {
console.log("$ Some input value is not correct. Make sure you have filled every field properly and you don't have ilegal characters as filenames");
}
})
}
async function main() {
if (checkMusicDirectory()) {
if (process.argv.length === 3 && process.argv[2].localeCompare("-downtag") == 0) {
try {
await internetAvailable();
downTagMode();
}
catch (exception) {
console.log("$ No internet");
}
}
else if (process.argv.length === 3 && process.argv[2].localeCompare("-playlist") == 0) {
playlistMode();
}
else if (process.argv.length === 3 && process.argv[2].localeCompare("-help") == 0) {
console.log("Mp3DownTagger 1.0.0. A music downloader and tagger program that is also capable of creating playlists with powerful filters.\n");
console.log("Usage: Mp3DownTagger [OPTION]\n");
console.log("-downtag\tDownload and tag new songs, simply download, or simply tag an existing song.");
console.log("-playlist\tCreate a playlist applying powerful filters.");
console.log("-help\t\tPrint this help.");
}
else {
console.log("Usage: Mp3DownTagger [OPTION]\n");
console.log("Try \"Mp3DownTagger -help\" for more options.");
}
}
}
main();