Fixed other errors with settingsRegexCover
[DeezloaderRemix.git] / app / app.js
1 /*
2 *   _____                _                 _             _____                _
3 *  |  __ \              | |               | |           |  __ \              (_)
4 *  | |  | | ___  ___ ___| | ___   __ _  __| | ___ _ __  | |__) |___ _ __ ___  ___  __
5 *  | |  | |/ _ \/ _ \_  / |/ _ \ / _` |/ _` |/ _ \ '__| |  _  // _ \ '_ ` _ \| \ \/ /
6 *  | |__| |  __/  __// /| | (_) | (_| | (_| |  __/ |    | | \ \  __/ | | | | | |>  <
7 *  |_____/ \___|\___/___|_|\___/ \__,_|\__,_|\___|_|    |_|  \_\___|_| |_| |_|_/_/\_\
8 *
9 **/
10
11 // Server stuff
12 const express = require('express')
13 const app = express()
14 const server = require('http').createServer(app)
15 const io = require('socket.io').listen(server, {log: false, wsEngine: 'ws'})
16 // Music tagging stuff
17 const mflac = require('./lib/flac-metadata')
18 const ID3Writer = require('./lib/browser-id3-writer')
19 const deezerApi = require('./lib/deezer-api')
20 const spotifyApi = require('spotify-web-api-node')
21 // App stuff
22 const fs = require('fs-extra')
23 const async = require('async')
24 const request = require('request-promise')
25 const requestOld = require('request')
26 const os = require('os')
27 const path = require('path')
28 const logger = require('./utils/logger.js')
29 const queue = require('queue')
30 const localpaths = require('./utils/localpaths.js')
31 const package = require('./package.json')
32 const stq = require('sequential-task-queue')
33
34 // First run, create config file
35 if(!fs.existsSync(localpaths.user+"config.json")){
36         fs.outputFileSync(localpaths.user+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'))
37 }
38
39 // Main Constants
40 // Files
41 const configFileLocation = localpaths.user+"config.json"
42 // Folders
43 const coverArtFolder = os.tmpdir() + path.sep + 'deezloader-imgs' + path.sep
44 const defaultDownloadFolder = localpaths.user + 'Deezloader Music' + path.sep
45 // Default settings
46 const defaultSettings = require('./default.json').userDefined
47 // Spotify Files
48 const spotifySupport = fs.existsSync(localpaths.user+"authCredentials.js")
49 if (spotifySupport){
50         var authCredentials = require(localpaths.user+'authCredentials.js')
51         var Spotify = new spotifyApi(authCredentials)
52 }
53
54 // Setup the folders START
55 var mainFolder = defaultDownloadFolder
56
57 // See if all settings are there after update
58 var configFile = require(localpaths.user+path.sep+"config.json");
59 for (let x in defaultSettings){
60         if (typeof configFile.userDefined[x] != typeof defaultSettings[x]){
61                 configFile.userDefined[x] = defaultSettings[x]
62         }
63 }
64 // Set default download folder if not userDefined
65 if (configFile.userDefined.downloadLocation != "") {
66         mainFolder = configFile.userDefined.downloadLocation
67 }
68
69 initFolders();
70
71 // Route and create server
72 app.use('/', express.static(__dirname + '/public/'))
73 server.listen(configFile.serverPort)
74 logger.info('Server is running @ localhost:' + configFile.serverPort)
75
76 // START sockets clusterfuck
77 io.sockets.on('connection', function (s) {
78         logger.info("Connection recived!")
79
80         // Check for updates
81         request({
82                 url: "https://notabug.org/RemixDevs/DeezloaderRemix/raw/master/update.json",
83                 json: true
84         })
85         .then(body=>{
86                 logger.info("Checking for updates")
87                 let [currentVersion_MAJOR, currentVersion_MINOR, currentVersion_PATCH] = package.version.split(".")
88                 let [lastVersion_MAJOR, lastVersion_MINOR, lastVersion_PATCH] = body.version.split(".")
89                 if (
90                         parseInt(lastVersion_MAJOR) > parseInt(currentVersion_MAJOR) ||
91                         parseInt(lastVersion_MINOR) > parseInt(currentVersion_MINOR) ||
92                         parseInt(lastVersion_PATCH) > parseInt(currentVersion_PATCH))
93                 {
94                         logger.info("Update Available")
95                         s.emit("message", {title: `Version ${lastVersion_MAJOR}.${lastVersion_MINOR}.${lastVersion_PATCH} is available!`, msg: body.changelog})
96                 }
97         })
98         .catch(error=>{
99                 logger.error(`UpdateCheck failed: ${error.stack ? error.stack : error}`)
100         })
101
102         // Connection dependet variables
103         s.Deezer = new deezerApi()
104         s.dqueue = new stq.SequentialTaskQueue()
105         s.downloadQueue = {}
106         s.trackQueue = queue({
107                 autostart: true
108         })
109         s.trackQueue.concurrency = configFile.userDefined.queueConcurrency
110
111         // Function for logging in
112         s.on("login", async function (username, password, autologin) {
113                 try{
114                         logger.info("Logging in");
115                         await s.Deezer.login(username, password)
116                         s.emit("login", {user: s.Deezer.user})
117                         logger.info("Logged in successfully")
118                         if (autologin){
119                                 // Save session login so next time login is not needed
120                                 // This is the same method used by the official website
121                                 s.emit('getCookies', s.Deezer.getCookies())
122                         }
123                 }catch(err){
124                         s.emit("login", {error: err.message})
125                         logger.error(`Login failed: ${err.message}`)
126                 }
127         });
128
129         // Function for autologin
130         s.on("autologin", async function(jar, email){
131                 try{
132       await s.Deezer.loginViaCookies(jar, email)
133                         s.emit('login', {user: s.Deezer.user})
134     }catch(err){
135       s.emit('login', {error: err.message})
136                         logger.error(`Autologin failed: ${err.message}`)
137       return
138     }
139         })
140
141         // Function for logout
142         s.on("logout", function(){
143                 logger.info("Logged out")
144                 // Creating new object to clear the cookies
145                 s.Deezer = new deezerApi()
146                 return
147         })
148
149         // Returns list of charts available
150         s.on("getChartsCountryList", async function (data) {
151                 try{
152                         let charts = await s.Deezer.legacyGetChartsTopCountry()
153                         charts = charts.data || []
154                         let countries = []
155                         for (let i = 0; i < charts.length; i++) {
156                                 let obj = {
157                                         country: charts[i].title.replace("Top ", ""),
158                                         picture_small: charts[i].picture_small,
159                                         picture_medium: charts[i].picture_medium,
160                                         picture_big: charts[i].picture_big,
161                                         playlistId: charts[i].id
162                                 }
163                                 countries.push(obj)
164                         }
165                         s.emit("getChartsCountryList", {countries: countries, selected: data.selected})
166                 }catch(err){
167                         logger.error(`getChartsCountryList failed: ${err.stack}`)
168                         return
169                 }
170         })
171
172         // Returns chart tracks from Playlist ID
173         async function getChartsTrackListById(playlistId){
174                 if (typeof playlistId === 'undefined') {
175                         s.emit("getChartsTrackListByCountry", {err: "Can't find that playlist"})
176                         return
177                 }
178                 try{
179                         let tracks = await s.Deezer.legacyGetPlaylistTracks(playlistId)
180                         s.emit("getChartsTrackListByCountry", {
181                                 playlistId: playlistId,
182                                 tracks: tracks.data
183                         })
184                 }catch(err){
185                         s.emit("getChartsTrackListByCountry", {err: err})
186                         logger.error(`getChartsTrackListById failed: ${err.stack}`)
187                         return
188                 }
189         }
190
191         // Returns chart tracks from country name
192         async function getChartsTrackListByCountry(country){
193                 if (typeof country === 'undefined') {
194                         s.emit("getChartsTrackListByCountry", {err: "No country passed"})
195                         return
196                 }
197                 try{
198                         let charts = await s.Deezer.legacyGetChartsTopCountry()
199                         charts = charts.data || []
200                         let countries = []
201                         for (let i = 0; i < charts.length; i++) {
202                                 countries.push(charts[i].title.replace("Top ", ""))
203                         }
204                         if (countries.indexOf(country) == -1) {
205                                 s.emit("getChartsTrackListByCountry", {err: "Country not found"})
206                                 return
207                         }
208                         let playlistId = charts[countries.indexOf(country)].id
209                         await getChartsTrackListById(playlistId)
210                 }catch(err){
211                         logger.error(`getChartsTrackListByCountry failed: ${err.stack}`)
212                         return
213                 }
214         }
215         s.on("getChartsTrackListByCountry", function (data) {getChartsTrackListByCountry(data.country)})
216
217         // Returns list of playlists
218         async function getMyPlaylistList(){
219                 try{
220                         logger.info("Loading Personal Playlists")
221                         let data = await s.Deezer.legacyGetUserPlaylists(s.Deezer.user.id)
222                         data = data.data || []
223                         let playlists = []
224                         for (let i = 0; i < data.length; i++) {
225                                 let obj = {
226                                         title: data[i].title,
227                                         image: data[i].picture_small,
228                                         songs: data[i].nb_tracks,
229                                         link: data[i].link
230                                 }
231                                 playlists.push(obj)
232                         }
233                         if (configFile.userDefined.spotifyUser && spotifySupport){
234                                 try{
235                                         let creds = await Spotify.clientCredentialsGrant()
236                                         Spotify.setAccessToken(creds.body['access_token'])
237                                         let first = true
238                                         let offset = 0
239                                         do{
240                                                 let data = await Spotify.getUserPlaylists(configFile.userDefined.spotifyUser, {fields: "items(images,name,owner.id,tracks.total,uri),total", offset: offset*20})
241                                                 if (first){
242                                                         var total = data.body.total
243                                                         var numPages=Math.floor((total-1)/20)
244                                                         var playlistList = new Array(total)
245                                                         first = false
246                                                 }
247                                                 data.body.items.forEach((playlist, i) => {
248                                                         playlistList[(offset*20)+i] = {
249                                                                 title: playlist.name,
250                                                                 image: (playlist.images[0] ? playlist.images[0].url : ""),
251                                                                 songs: playlist.tracks.total,
252                                                                 link: playlist.uri,
253                                                                 spotify: true
254                                                         }
255                                                 })
256                                                 offset++
257                                         }while(offset<=numPages)
258                                         playlists = playlists.concat(playlistList)
259                                 }catch(err){
260                                         logger.error(`Spotify playlist failed loading: ${err}`)
261                                 }
262                         }
263                         logger.info(`Loaded ${playlists.length} Playlist${playlists.length>1 ? "s" : ""}`)
264                         s.emit("getMyPlaylistList", {playlists: playlists})
265                 }catch(err){
266                         logger.error(`getMyPlaylistList failed: ${err}`)
267                         return
268                 }
269         }
270         s.on("getMyPlaylistList", function (d) {getMyPlaylistList()})
271
272         // Returns search results from a query
273         s.on("search", async function (data) {
274                 data.type = data.type || "track"
275                 if (["track", "playlist", "album", "artist"].indexOf(data.type) == -1) data.type = "track"
276
277                 // Remove "feat."  "ft." and "&" (causes only problems)
278                 data.text = data.text
279                         .replace(/ feat[\.]? /g, " ")
280                         .replace(/ ft[\.]? /g, " ")
281                         .replace(/\(feat[\.]? /g, " ")
282                         .replace(/\(ft[\.]? /g, " ")
283                         .replace(/\&/g, "")
284                         .replace(/–/g, "-")
285                         .replace(/—/g, "-")
286
287                 try {
288                         let searchObject = await s.Deezer.legacySearch(encodeURIComponent(data.text), data.type)
289                         s.emit("search", {type: data.type, items: searchObject.data})
290                 } catch (err) {
291                         s.emit("search", {type: data.type, items: []})
292                         logger.error(`search failed: ${err.stack}`)
293                         return
294                 }
295         })
296
297         // Returns list of tracks from an album/playlist or the list of albums from an artist
298         s.on("getTrackList", async function (data) {
299                 if (!data.type || (["playlist", "album", "artist", "spotifyplaylist"].indexOf(data.type) == -1) || !data.id) {
300                         s.emit("getTrackList", {err: -1, response: {}, id: data.id, reqType: data.type})
301                         return
302                 }
303                 if (data.type == 'artist') {
304                         try{
305                                 let response = await s.Deezer.legacyGetArtistAlbums(data.id)
306                                 s.emit("getTrackList", {response: response, id: data.id, reqType: data.type})
307                         }catch(err){
308                                 s.emit("getTrackList", {err: "wrong artist id", response: {}, id: data.id, reqType: data.type})
309                                 logger.error(`getTrackList failed: ${err.stack}`)
310                                 return
311                         }
312                 }else if(data.type == "spotifyplaylist" && spotifySupport){
313                         try{
314                                 let creds = await Spotify.clientCredentialsGrant()
315                                 Spotify.setAccessToken(creds.body.access_token)
316                                 let first = true
317                                 let offset = 0
318                                 do{
319                                         let resp = await Spotify.getPlaylistTracks(data.id, {fields: "items(track(artists,name,duration_ms,preview_url,explicit)),total", offset: offset*100})
320                                         if (first){
321                                                 var numPages=Math.floor((resp.body.total-1)/100)
322                                                 var response = new Array(resp.body.total)
323                                                 first = false
324                                         }
325                                         resp.body.items.forEach((t, index) => {
326                                                 response[index+offset*100]={
327                                                         explicit_lyrics: t.track.explicit,
328                                                         preview: t.track.preview_url,
329                                                         title: t.track.name,
330                                                         artist: {
331                                                                 name: t.track.artists[0].name
332                                                         },
333                                                         duration: Math.floor(t.track.duration_ms/1000)
334                                                 }
335                                         })
336                                         offset++
337                                 }while(offset<=numPages)
338                                 s.emit("getTrackList", {response: {'data': response}, id: data.id, reqType: data.type})
339                         }catch(err){
340                                 logger.error(`getTrackList failed: ${err.stack}`)
341                         }
342                 }else{
343                         let reqType = data.type.charAt(0).toUpperCase() + data.type.slice(1)
344                         try{
345                                 let response = await s.Deezer["legacyGet" + reqType + "Tracks"](data.id)
346                                 s.emit("getTrackList", {response: response, id: data.id, reqType: data.type})
347                         }catch(err){
348                                 s.emit("getTrackList", {err: "wrong id "+reqType, response: {}, id: data.id, reqType: data.type})
349                                 logger.error(`getTrackList failed: ${err.stack}`)
350                                 return
351                         }
352                 }
353         })
354
355         // Sends settings saved by the user to the frontend
356         s.on("getUserSettings", function () {
357                 let settings = configFile.userDefined
358                 if (!settings.downloadLocation) {
359                         settings.downloadLocation = mainFolder
360                 }
361                 s.emit('getUserSettings', {settings: settings})
362         });
363
364         // Saves locally the settings comming from the frontend
365         s.on("saveSettings", function (settings) {
366                 if (settings.userDefined.downloadLocation == defaultDownloadFolder) {
367                         settings.userDefined.downloadLocation = ""
368                 } else {
369                         settings.userDefined.downloadLocation = path.resolve(settings.userDefined.downloadLocation + path.sep) + path.sep
370                         mainFolder = settings.userDefined.downloadLocation
371                 }
372
373                 if (settings.userDefined.queueConcurrency < 1) settings.userDefined.queueConcurrency = 1
374
375                 if (settings.userDefined.queueConcurrency != s.trackQueue.concurrency){
376                         s.trackQueue.concurrency = settings.userDefined.queueConcurrency
377                 }
378
379                 if (settings.userDefined.chartsCountry != configFile.userDefined.chartsCountry){
380                         s.emit("setChartsCountry", {selected: settings.userDefined.chartsCountry})
381                         getChartsTrackListByCountry(settings.userDefined.chartsCountry)
382                 }
383
384                 if (settings.userDefined.spotifyUser != configFile.userDefined.spotifyUser){
385                         getMyPlaylistList(settings.userDefined.spotifyUser)
386                 }
387
388                 configFile.userDefined = settings.userDefined;
389                 fs.outputFile(configFileLocation, JSON.stringify(configFile, null, 2), function (err) {
390                         if (err) return
391                         logger.info("Settings updated")
392                         initFolders()
393                 });
394         });
395
396         /*
397          * Downloading section of the app
398         */
399
400         // Gets data from the frontend and creates the track object
401         async function downloadTrack(data){
402                 logger.info(`Added to Queue ${data.id}`)
403                 try{
404                         var track = await s.Deezer.getTrack(data.id)
405                         data.settings.filename = data.settings.trackNameTemplate
406                         data.settings.foldername = data.settings.albumNameTemplate
407                         let _track = {
408                                 name: track.title,
409                                 artist: track.artist.name,
410                                 size: 1,
411                                 downloaded: 0,
412                                 failed: 0,
413                                 queueId: `id${Math.random().toString(36).substring(2)}`,
414                                 id: `${track.id}:${data.settings.maxBitrate}`,
415                                 type: 'track',
416                                 settings: data.settings || {},
417                                 obj: track,
418                         }
419                         addToQueue(_track)
420                 }catch(err){
421                         logger.error(`downloadTrack failed: ${err.stack ? err.stack : err}`)
422                         return
423                 }
424         }
425         s.on("downloadtrack", async data=>{await downloadTrack(data)})
426
427         // Gets data from the frontend and creates the album object
428         async function downloadAlbum(data){
429                 logger.info(`Added to Queue ${data.id}`)
430                 try{
431                         var album = await s.Deezer.legacyGetAlbum(data.id)
432                         if (data.settings.tags.discTotal || data.settings.createCDFolder){
433                                 var discTotal = await s.Deezer.getAlbum(data.id)
434                                 album.discTotal = discTotal.discTotal
435                         }
436                         if (album.nb_tracks == 1){
437                                 var track = await s.Deezer.getTrack(album.tracks.data[0].id)
438                                 data.settings.filename = data.settings.trackNameTemplate
439                                 data.settings.foldername = data.settings.albumNameTemplate
440                                 let _track = {
441                                         name: track.title,
442                                         artist: track.artist.name,
443                                         size: 1,
444                                         downloaded: 0,
445                                         failed: 0,
446                                         queueId: `id${Math.random().toString(36).substring(2)}`,
447                                         id: `${track.id}:${data.settings.maxBitrate}`,
448                                         type: 'track',
449                                         settings: data.settings || {},
450                                         obj: track,
451                                 }
452                                 addToQueue(_track)
453                         }else{
454                                 album.tracks = await s.Deezer.getAlbumTracks(data.id)
455                                 data.settings.filename = data.settings.albumTrackNameTemplate
456                                 data.settings.foldername = data.settings.albumNameTemplate
457                                 let _album = {
458                                         name: album.title,
459                                         artist: album.artist.name,
460                                         size: album.tracks.length,
461                                         downloaded: 0,
462                                         failed: 0,
463                                         queueId: `id${Math.random().toString(36).substring(2)}`,
464                                         id: `${album.id}:${data.settings.maxBitrate}`,
465                                         type: 'album',
466                                         settings: data.settings || {},
467                                         obj: album,
468                                 }
469                                 addToQueue(_album)
470                         }
471                         return
472                 }catch(err){
473                         logger.error(`downloadAlbum failed: ${err.stack ? err.stack : err}`)
474                         return
475                 }
476         }
477         s.on("downloadalbum", async data=>{await downloadAlbum(data)});
478
479         // Gets data from the frontend and creates for each album an album object
480         async function downloadArtist(data){
481                 logger.info(`Added to Queue ${data.id}`)
482                 try{
483                         var albums = await s.Deezer.legacyGetArtistAlbums(data.id);
484                         (function sendAllAlbums(i) {
485                                 setTimeout(function () {
486                                         data.id = albums.data[albums.data.length-1-i].id
487                                         downloadAlbum(JSON.parse(JSON.stringify(data)))
488                                         if (--i+1) sendAllAlbums(i)
489                                 }, 100)
490                         })(albums.data.length-1)
491                 }catch(err){
492                         logger.error(`downloadArtist failed: ${err.stack ? err.stack : err}`)
493                         return
494                 }
495         }
496         s.on("downloadartist", async data=>{ await downloadArtist(data)});
497
498         // Gets data from the frontend and creates the playlist object
499         async function downloadPlaylist(data){
500                 logger.info(`Added to Queue ${data.id}`)
501                 try{
502                         var playlist = await s.Deezer.legacyGetPlaylist(data.id)
503                         data.settings.filename = data.settings.playlistTrackNameTemplate
504                         data.settings.foldername = data.settings.albumNameTemplate
505                         playlist.tracks = await s.Deezer.getPlaylistTracks(data.id)
506                         let _playlist = {
507                                 name: playlist.title,
508                                 artist: playlist.creator.name,
509                                 size: playlist.tracks.length,
510                                 downloaded: 0,
511                                 failed: 0,
512                                 queueId: `id${Math.random().toString(36).substring(2)}`,
513                                 id: `${playlist.id}:${data.settings.maxBitrate}`,
514                                 type: "playlist",
515                                 settings: data.settings || {},
516                                 obj: playlist,
517                         }
518                         addToQueue(_playlist)
519                 }catch(err){
520                         logger.error(`downloadPlaylist failed: ${err.stack ? err.stack : err}`)
521                         return
522                 }
523         }
524         s.on("downloadplaylist", data=>{downloadPlaylist(data)});
525
526         // Gets data from the frontend and creates the object fot the artist top tracks
527         async function downloadArtistTop(data){
528                 logger.info(`Added to Queue ${data.id}`)
529                 try{
530                         var artist = await s.Deezer.legacyGetArtist(data.id)
531                         data.settings.filename = data.settings.playlistTrackNameTemplate
532                         data.settings.foldername = data.settings.albumNameTemplate
533                         artist.tracks = await s.Deezer.getArtistTopTracks(data.id)
534                         let _playlist = {
535                                 name: artist.name + " Most played tracks",
536                                 artist: artist.name,
537                                 size: artist.tracks.length,
538                                 downloaded: 0,
539                                 failed: 0,
540                                 queueId: `id${Math.random().toString(36).substring(2)}`,
541                                 id: `${artist.id}:${data.settings.maxBitrate}`,
542                                 type: "playlist",
543                                 settings: data.settings || {},
544                                 obj: artist,
545                         }
546                         addToQueue(_playlist)
547                 }catch(err){
548                         logger.error(`downloadArtistTop failed: ${err.stack ? err.stack : err}`)
549                         return
550                 }
551         }
552         s.on("downloadartisttop", data=>{downloadArtistTop(data)});
553
554         // Gets data from the frontend and creates the spotify playlist object
555         async function downloadSpotifyPlaylist(data){
556                 logger.info(`Added to Queue ${data.id}`)
557                 if (spotifySupport){
558                         try{
559                                 let creds = await Spotify.clientCredentialsGrant()
560                                 Spotify.setAccessToken(creds.body['access_token'])
561                                 let first = true
562                                 let offset = 0
563                                 data.settings.filename = data.settings.playlistTrackNameTemplate
564                                 data.settings.foldername = data.settings.albumNameTemplate
565                                 do{
566                                         var resp = await Spotify.getPlaylist(data.id, {fields: "id,name,owner,images,tracks(total,items(track(artists,name,album,external_ids)))", offset: offset*100})
567                                         if (first){
568                                                 var _playlist = {
569                                                         name: resp.body.name,
570                                                         artist: (resp.body.owner.display_name ? resp.body.owner.display_name : resp.body.owner.id),
571                                                         size: resp.body.tracks.total,
572                                                         downloaded: 0,
573                                                         failed: 0,
574                                                         queueId: `id${Math.random().toString(36).substring(2)}`,
575                                                         settings: data.settings || {},
576                                                         id: `${resp.body.id}:${data.settings.maxBitrate}`,
577                                                         type: "spotifyplaylist",
578                                                         obj: resp.body
579                                                 }
580                                                 var numPages=Math.floor((_playlist.size-1)/100)
581                                                 var trackList = new Array(_playlist.size)
582                                                 first = false
583                                         }
584                                         resp.body.tracks.items.forEach((track, i) => {
585                                                 trackList[(offset*100)+i] = track.track
586                                         })
587                                         offset++
588                                 }while(offset<=numPages)
589                                 _playlist.obj.tracks = trackList
590                                 addToQueue(_playlist)
591                         }catch(err){
592                                 logger.error(`downloadSpotifyPlaylist failed: ${err.stack ? err.stack : err}`)
593                                 return
594                         }
595                 }else{
596                         s.emit("message", {title: "Spotify Support is not enabled", msg: "You should add authCredentials.js in your config files to use this feature<br>You can see how to do that in <a href=\"https://notabug.org/RemixDevs/DeezloaderRemix/wiki/Spotify+Features\">this guide</a>"})
597                 }
598         }
599         s.on("downloadspotifyplaylist", data=>{downloadSpotifyPlaylist(data)})
600
601         // Converts the spotify track to a deezer one
602         // It tries first with the isrc (best way of conversion)
603         // Fallbacks to the old way, using search
604         async function convertSpotify2Deezer(track){
605                 try{
606                         if (track.external_ids.isrc){
607                                 let resp = await s.Deezer.legacyGetTrackByISRC(track.external_ids.isrc)
608                                 return resp.id
609                         }
610                 }catch(err){
611                         logger.warn("ISRC not found, falling back to old method")
612                 }
613                 let resp
614                 track.artists[0].name = track.artists[0].name.replace(/–/g,"-").replace(/’/g, "'")
615                 track.name = track.name.replace(/–/g,"-").replace(/’/g, "'")
616                 track.album.name = track.album.name.replace(/–/g,"-").replace(/’/g, "'")
617                 try{
618                         resp = await s.Deezer.legacySearch(`artist:"${track.artists[0].name}" track:"${track.name}" album:"${track.album.name}"`, "track", 1)
619                 }catch(err){logger.err(`Convert2Spotify: ${err.stack ? err.stack : err}`)}
620                 if (resp.data[0]) return resp.data[0].id
621                 try{
622                         resp = await s.Deezer.legacySearch(`artist:"${track.artists[0].name}" track:"${track.name}"`, "track", 1)
623                 }catch(err){logger.err(`Convert2Spotify: ${err.stack ? err.stack : err}`)}
624                 if (resp.data[0]) return resp.data[0].id
625                 if (track.name.indexOf("(") < track.name.indexOf(")")){
626                         try{
627                                 resp = await s.Deezer.legacySearch(`artist:"${track.artists[0].name}" track:"${track.name.split("(")[0]}"`, "track", 1)
628                         }catch(err){logger.err(`Convert2Spotify: ${err.stack ? err.stack : err}`)}
629                         if (resp.data[0]) return resp.data[0].id
630                 }else if (track.name.indexOf(" - ")>0){
631                         try{
632                                 resp = await s.Deezer.legacySearch(`artist:"${track.artists[0].name}" track:"${track.name.split(" - ")[0]}"`, "track", 1)
633                         }catch(err){logger.err(`Convert2Spotify: ${err.stack ? err.stack : err}`)}
634                         if (resp.data[0]) return resp.data[0].id
635                 }else{
636                         return 0
637                 }
638                 return 0
639         }
640
641         // All the above functions call this function
642         // It adds the object to an array and adds the promise for the download to the object itself
643         function addToQueue(object) {
644                 s.downloadQueue[object.queueId] = object
645                 s.emit('addToQueue', object)
646                 s.downloadQueue[object.queueId].downloadQueuePromise = s.dqueue.push(addNextDownload, { args: object })
647         }
648
649         // Wrapper for queue download
650         function addNextDownload(obj, token){
651                 return new Promise(async (resolve, reject) => {
652       await queueDownload(obj)
653                         resolve()
654     }).then(() => new Promise((resolve, reject) => {
655       if (token.cancelled)
656         reject()
657       else
658         resolve()
659     }))
660         }
661
662         // Cancels download
663         // TODO: Might check this one, could be a little buggy
664         function cancelDownload(queueId, cleanAll=false){
665                 if (!queueId) return
666                 let cancel = false
667                 let cancelSuccess
668                 if (s.downloadQueue[queueId]){
669                         cancel = true;
670                         if (s.downloadQueue[queueId].downloadQueuePromise) s.downloadQueue[queueId].downloadQueuePromise.cancel()
671                         if (s.downloadQueue[Object.keys(s.downloadQueue)[0]].queueId == queueId) {
672                                 s.trackQueue = queue({
673                                         autostart: true,
674                                         concurrency: s.trackQueue.concurrency
675                                 })
676                         }
677                         delete s.downloadQueue[queueId]
678                 }
679
680                 if (cancel) {
681                         s.emit("cancelDownload", {queueId: queueId, cleanAll: cleanAll});
682                 }
683         }
684         s.on("cancelDownload", function (data) {cancelDownload(data.queueId)});
685
686         s.on("cancelAllDownloads", function(data){
687                 data.queueList.forEach(x=>{
688                         cancelDownload(x, true);
689                 })
690                 s.emit("cancelAllDownloads")
691         })
692
693         /*function getNextDownload() {
694                 if (s.currentItem != null || Object.keys(s.downloadQueue).length == 0) {
695                         if (Object.keys(s.downloadQueue).length == 0 && s.currentItem == null) {
696                                 s.emit("emptyDownloadQueue", {})
697                         }
698                         return null
699                 }
700                 s.currentItem = s.downloadQueue[Object.keys(s.downloadQueue)[0]]
701                 return s.currentItem
702         }*/
703
704         //downloadQueue: the tracks in the queue to be downloaded
705         //queueId: random number generated when user clicks download on something
706         async function queueDownload(downloading) {
707                 if (!downloading) return
708
709                 if (downloading.type != "spotifyplaylist"){
710                         s.emit("downloadStarted", {queueId: downloading.queueId})
711                 }
712
713                 downloading.errorLog = "";
714                 downloading.searchedLog = "";
715
716                 let filePath;
717                 logger.info(`Registered ${downloading.type}: ${downloading.id} | ${downloading.artist} - ${downloading.name}`);
718                 switch(downloading.type){
719                         /*
720                         *  TRACK DOWNLOAD
721                         */
722                         case "track":
723                                 var downloadPromise = new Promise(async (resolve,reject)=>{
724                                         try{
725                                                 await downloadTrackObject(downloading.obj, downloading.queueId, downloading.settings)
726                                                 downloading.downloaded++
727                                         }catch(err){
728                                                 downloading.failed++
729                                         }
730                                         s.emit("updateQueue", {
731                                                 name: downloading.name,
732                                                 artist: downloading.artist,
733                                                 size: downloading.size,
734                                                 downloaded: downloading.downloaded,
735                                                 failed: downloading.failed,
736                                                 queueId: downloading.queueId,
737                                                 id: downloading.id,
738                                                 type: downloading.type,
739                                         })
740                                         s.emit("downloadProgress", {
741                                                 queueId: downloading.queueId,
742                                                 percentage: 100
743                                         })
744                                         resolve()
745                                 })
746                                 try{
747                                         await downloadPromise
748                                 }catch(err){
749                                         if (err) logger.error(`queueDownload:track failed: ${err.stack ? err.stack : err}`)
750                                         logger.info("Downloading Stopped")
751                                 }
752                         break
753                         /*
754                         *  ALBUM DOWNLOAD
755                         */
756                         case "album":
757                                 downloading.settings.albName = downloading.name;
758                                 downloading.settings.artName = downloading.artist;
759                                 downloading.playlistArr = Array(downloading.size);
760                                 filePath = mainFolder;
761                                 if (downloading.settings.createArtistFolder || downloading.settings.createAlbumFolder) {
762                                         if (downloading.settings.createArtistFolder) {
763                                                 filePath += antiDot(fixName(downloading.settings.artName)) + path.sep;
764                                         }
765                                         if (downloading.settings.createAlbumFolder) {
766                                                 filePath += antiDot(settingsRegexAlbum(downloading.settings.foldername,downloading.settings.artName,downloading.settings.albName,downloading.obj.release_date.slice(0, 4),downloading.obj.record_type,downloading.obj.explicit_lyrics,downloading.obj.label, downloading.obj.genres)) + path.sep;
767                                         }
768                                 } else if (downloading.settings.artName) {
769                                         filePath += antiDot(settingsRegexAlbum(downloading.settings.foldername,downloading.settings.artName,downloading.settings.albName,downloading.obj.release_date.slice(0, 4),downloading.obj.record_type,downloading.obj.explicit_lyrics,downloading.obj.label, downloading.obj.genres)) + path.sep;
770                                 }
771                                 let ajson = {
772                                         artist : downloading.obj.artist,
773                                         nb_tracks : downloading.obj.nb_tracks,
774                                         upc : downloading.obj.upc,
775                                         record_type : downloading.obj.record_type,
776                                         explicit_lyrics : downloading.obj.explicit_lyrics,
777                                         label : downloading.obj.label,
778                                         release_date : downloading.obj.release_date,
779                                         genres : downloading.obj.genres,
780                                         discTotal: downloading.obj.discTotal ? downloading.obj.discTotal : null
781                                 }
782                                 downloading.downloadPromise = new Promise((resolve,reject)=>{
783                                         downloading.obj.tracks.every(function (t) {
784                                                 s.trackQueue.push(async cb=>{
785                                                         if (!s.downloadQueue[downloading.queueId]) {
786                                                                 reject()
787                                                                 return false
788                                                         }
789                                                         t.ajson = ajson
790                                                         logger.info(`Now downloading: ${t.artist.name} - ${t.title}`)
791                                                         try{
792                                                                 await downloadTrackObject(t, downloading.queueId, downloading.settings)
793                                                                 downloading.downloaded++
794                                                                 downloading.playlistArr[t.playlistData[0]] = t.playlistData[1].split(filePath)[1]
795                                                                 if (t.searched) downloading.searchedLog += `${t.artist.name} - ${t.name}\r\n`
796                                                         }catch(err){
797                                                                 downloading.failed++
798                                                                 downloading.errorLog += `${t.id} | ${t.artist.name} - ${t.title} | ${err}\r\n`
799                                                         }
800                                                         s.emit("downloadProgress", {
801                                                                 queueId: downloading.queueId,
802                                                                 percentage: ((downloading.downloaded+downloading.failed) / downloading.size) * 100
803                                                         });
804                                                         s.emit("updateQueue", {
805                                                                 name: downloading.name,
806                                                                 artist: downloading.artist,
807                                                                 size: downloading.size,
808                                                                 downloaded: downloading.downloaded,
809                                                                 failed: downloading.failed,
810                                                                 queueId: downloading.queueId,
811                                                                 id: downloading.id,
812                                                                 type: downloading.type,
813                                                         })
814                                                         if (downloading.downloaded + downloading.failed >= downloading.size) resolve()
815                                                         cb()
816                                                 })
817                                                 return true
818                                         })
819                                 })
820                                 try{
821                                         await downloading.downloadPromise
822                                         logger.info("Album finished downloading: "+downloading.name);
823                                         s.emit("downloadProgress", {
824                                                 queueId: downloading.queueId,
825                                                 percentage: 100
826                                         });
827                                         if (downloading.settings.logErrors){
828                                                 if (downloading.errorLog != ""){
829                                                         if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
830                                                         fs.writeFileSync(filePath+"notFound.txt",downloading.errorLog)
831                                                 }else{
832                                                         if (fs.existsSync(filePath+"notFound.txt")) fs.unlinkSync(filePath+"notFound.txt");
833                                                 }
834                                         }
835                                         if (downloading.settings.logSearched){
836                                                 if (downloading.searchedLog != ""){
837                                                         if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
838                                                         fs.writeFileSync(filePath+"alternativeSongs.txt",downloading.searchedLog)
839                                                 }else{
840                                                         if (fs.existsSync(filePath+"alternativeSongs.txt")) fs.unlinkSync(filePath+"alternativeSongs.txt");
841                                                 }
842                                         }
843                                         if (downloading.settings.createM3UFile){
844                                                 fs.writeFileSync(filePath+"playlist.m3u", downloading.playlistArr.join("\r\n"));
845                                         }
846                                 }catch(err){
847                                         if (err) logger.error(`queueDownload:album failed: ${err.stack ? err.stack : err}`)
848                                         logger.info("Stopping the album queue");
849                                 }
850                         break
851                         /*
852                         *  PLAYLIST DOWNLOAD
853                         */
854                         case "playlist":
855                                 downloading.settings.plName = downloading.name;
856                                 downloading.playlistArr = Array(downloading.size);
857                                 downloading.settings.playlist = {
858                                         fullSize: downloading.obj.tracks.length
859                                 };
860                                 filePath = mainFolder+antiDot(fixName(downloading.settings.plName)) + path.sep
861                                 downloading.downloadPromise = new Promise((resolve,reject)=>{
862                                         downloading.obj.tracks.every(function (t, index) {
863                                                 s.trackQueue.push(async cb=>{
864                                                         if (!s.downloadQueue[downloading.queueId]) {
865                                                                 reject()
866                                                                 return false
867                                                         }
868                                                         try{
869                                                                 await downloadTrackObject(t, downloading.queueId, downloading.settings)
870                                                                 downloading.downloaded++
871                                                                 downloading.playlistArr[t.playlistData[0]] = t.playlistData[1].split(filePath)[1]
872                                                                 if (t.searched) downloading.searchedLog += `${t.artist.name} - ${t.name}\r\n`
873                                                         }catch(err){
874                                                                 logger.debug(err.stack ? err.stack : err)
875                                                                 downloading.failed++
876                                                                 downloading.errorLog += `${t.id} | ${t.artist.name} - ${t.title} | ${err}\r\n`
877                                                         }
878                                                         s.emit("downloadProgress", {
879                                                                 queueId: downloading.queueId,
880                                                                 percentage: ((downloading.downloaded+downloading.failed) / downloading.size) * 100
881                                                         });
882                                                         s.emit("updateQueue", {
883                                                                 name: downloading.name,
884                                                                 artist: downloading.artist,
885                                                                 size: downloading.size,
886                                                                 downloaded: downloading.downloaded,
887                                                                 failed: downloading.failed,
888                                                                 queueId: downloading.queueId,
889                                                                 id: downloading.id,
890                                                                 type: downloading.type,
891                                                         })
892                                                         if (downloading.downloaded + downloading.failed >= downloading.size) resolve()
893                                                         cb()
894                                                 })
895                                                 return true
896                                         })
897                                 })
898                                 try{
899                                         await downloading.downloadPromise
900                                         logger.info("Playlist finished "+downloading.name);
901                                         s.emit("downloadProgress", {
902                                                 queueId: downloading.queueId,
903                                                 percentage: 100
904                                         });
905                                         if (downloading.settings.logErrors){
906                                                 if (downloading.errorLog != ""){
907                                                         if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
908                                                         fs.writeFileSync(filePath+"notFound.txt",downloading.errorLog)
909                                                 }else{
910                                                         if (fs.existsSync(filePath+"notFound.txt")) fs.unlinkSync(filePath+"notFound.txt");
911                                                 }
912                                         }
913                                         if (downloading.settings.logSearched){
914                                                 if (downloading.searchedLog != ""){
915                                                         if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
916                                                         fs.writeFileSync(filePath+"alternativeSongs.txt",downloading.searchedLog)
917                                                 }else{
918                                                         if (fs.existsSync(filePath+"alternativeSongs.txt")) fs.unlinkSync(filePath+"alternativeSongs.txt");
919                                                 }
920                                         }
921                                         if (downloading.settings.createM3UFile){
922                                                 fs.writeFileSync(filePath + "playlist.m3u", downloading.playlistArr.join("\r\n"));
923                                         }
924                                         if (downloading.settings.saveArtwork){
925                                                 if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
926                                                 let imgPath = filePath + antiDot(settingsRegexCover(downloading.settings.coverImageTemplate,downloading.artist,downloading.name))+(downloading.settings.PNGcovers ? ".png" : ".jpg");
927                                                 if (downloading.cover){
928                                                         downloading.cover = downloading.cover.replace("56x56",`${downloading.settings.artworkSize}x${downloading.settings.artworkSize}`)
929                                                         request.get(downloading.cover, {strictSSL: false,encoding: 'binary'}, function(error,response,body){
930                                                                 if(error){
931                                                                         logger.error(error.stack);
932                                                                         return;
933                                                                 }
934                                                                 fs.outputFile(imgPath,body,'binary',function(err){
935                                                                         if(err){
936                                                                                 logger.error(err.stack);
937                                                                                 return;
938                                                                         }
939                                                                         logger.info(`Cover downloaded for: ${downloading.settings.plName}`)
940                                                                 })
941                                                         });
942                                                 }
943                                         }
944                                 }catch(err){
945                                         if (err) logger.error(`queueDownload:playlist failed: ${err.stack ? err.stack : err}`)
946                                         logger.info("Stopping the playlist queue")
947                                 }
948                         break
949                         /*
950                         *  SPOTIFY PLAYLIST DOWNLOAD
951                         */
952                         case "spotifyplaylist":
953                                 downloading.settings.plName = downloading.name
954                                 downloading.playlistArr = Array(downloading.size)
955                                 downloading.playlistContent = new Array(downloading.size);
956                                 logger.info("Waiting for all tracks to be converted");
957                                 const convert = async () =>{
958                                         await asyncForEach(downloading.obj.tracks, async (t,i)=>{
959                                                 try{
960                                                         downloading.playlistContent[i] = await convertSpotify2Deezer(t)
961                                                 }catch(err){
962                                                         logger.error(`queueDownload:spotifyplaylist failed during conversion: ${err.stack ? err.stack : err}`)
963                                                 }
964                                         })
965                                 }
966                                 await convert()
967                                 if (!s.downloadQueue[downloading.queueId]) {
968                                         logger.info("Stopping the playlist queue")
969                                         break
970                                 }
971                                 downloading.trackList = await s.Deezer.getTracks(downloading.playlistContent)
972                                 logger.info("All tracks converted, starting download")
973                                 s.emit("downloadStarted", {queueId: downloading.queueId})
974                                 downloading.settings.playlist = {
975                                         fullSize: downloading.trackList.length
976                                 }
977                                 filePath = `${mainFolder}${antiDot(fixName(downloading.settings.plName))}${path.sep}`
978                                 downloading.downloadPromise = new Promise((resolve,reject)=>{
979                                         downloading.trackList.every(function (t, index) {
980                                                 s.trackQueue.push(async cb=>{
981                                                         if (!s.downloadQueue[downloading.queueId]) {
982                                                                 reject()
983                                                                 return false
984                                                         }
985                                                         t.position = index
986                                                         try{
987                                                                 await downloadTrackObject(t, downloading.queueId, downloading.settings)
988                                                                 downloading.downloaded++
989                                                                 downloading.playlistArr[t.playlistData[0]] = t.playlistData[1].split(filePath)[1]
990                                                                 if (t.searched) downloading.searchedLog += `${t.artist.name} - ${t.name}\r\n`
991                                                         }catch(err){
992                                                                 downloading.failed++
993                                                                 downloading.errorLog += `${t.id} | ${t.artist.name} - ${t.title} | ${err}\r\n`
994                                                         }
995                                                         s.emit("downloadProgress", {
996                                                                 queueId: downloading.queueId,
997                                                                 percentage: ((downloading.downloaded+downloading.failed) / downloading.size) * 100
998                                                         });
999                                                         s.emit("updateQueue", {
1000                                                                 name: downloading.name,
1001                                                                 artist: downloading.artist,
1002                                                                 size: downloading.size,
1003                                                                 downloaded: downloading.downloaded,
1004                                                                 failed: downloading.failed,
1005                                                                 queueId: downloading.queueId,
1006                                                                 id: downloading.id,
1007                                                                 type: downloading.type,
1008                                                         })
1009                                                         if (downloading.downloaded + downloading.failed >= downloading.size) resolve()
1010                                                         cb()
1011                                                 })
1012                                                 return true
1013                                         })
1014                                 })
1015                                 try{
1016                                         await downloading.downloadPromise
1017                                         logger.info("Playlist finished "+downloading.name);
1018                                         s.emit("downloadProgress", {
1019                                                 queueId: downloading.queueId,
1020                                                 percentage: 100
1021                                         });
1022                                         if (downloading.settings.logErrors){
1023                                                 if (downloading.errorLog != ""){
1024                                                         if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
1025                                                         fs.writeFileSync(filePath+"notFound.txt",downloading.errorLog)
1026                                                 }else{
1027                                                         if (fs.existsSync(filePath+"notFound.txt")) fs.unlinkSync(filePath+"notFound.txt");
1028                                                 }
1029                                         }
1030                                         if (downloading.settings.logSearched){
1031                                                 if (downloading.searchedLog != ""){
1032                                                         if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
1033                                                         fs.writeFileSync(filePath+"alternativeSongs.txt",downloading.searchedLog)
1034                                                 }else{
1035                                                         if (fs.existsSync(filePath+"alternativeSongs.txt")) fs.unlinkSync(filePath+"alternativeSongs.txt");
1036                                                 }
1037                                         }
1038                                         if (downloading.settings.createM3UFile){
1039                                                 fs.writeFileSync(filePath + "playlist.m3u", downloading.playlistArr.join("\r\n"));
1040                                         }
1041                                         if (downloading.settings.saveArtwork){
1042                                                 if (!fs.existsSync(filePath)) fs.mkdirSync(filePath);
1043                                                 let imgPath = filePath + antiDot(settingsRegexCover(downloading.settings.coverImageTemplate,downloading.artist,downloading.name))+(downloading.settings.PNGcovers ? ".png" : ".jpg");
1044                                                 if (downloading.obj.images){
1045                                                         downloading.cover = downloading.obj.images[0].url.replace("56x56",`${downloading.settings.artworkSize}x${downloading.settings.artworkSize}`)
1046                                                         request.get(downloading.cover, {strictSSL: false,encoding: 'binary'}, function(error,response,body){
1047                                                                 if(error){
1048                                                                         logger.error(error.stack);
1049                                                                         return;
1050                                                                 }
1051                                                                 fs.outputFile(imgPath,body,'binary',function(err){
1052                                                                         if(err){
1053                                                                                 logger.error(err.stack);
1054                                                                                 return;
1055                                                                         }
1056                                                                         logger.info(`Cover downloaded for: ${downloading.settings.plName}`)
1057                                                                 })
1058                                                         });
1059                                                 }
1060                                         }
1061                                 }catch(err){
1062                                         if (err) logger.error(`queueDownload:spotifyplaylist failed: ${err.stack ? err.stack : err}`)
1063                                         logger.info("Stopping the playlist queue")
1064                                 }
1065                         break
1066                 }
1067                 if (downloading && s.downloadQueue[Object.keys(s.downloadQueue)[0]] && (Object.keys(s.downloadQueue)[0] == downloading.queueId)) delete s.downloadQueue[Object.keys(s.downloadQueue)[0]]
1068                 if (Object.keys(s.downloadQueue).length == 0) {
1069                         s.emit("emptyDownloadQueue", {})
1070                 }
1071         }
1072
1073         // This function takes the track object and does all the stuff to download it
1074         async function downloadTrackObject(track, queueId, settings) {
1075                 if (!s.downloadQueue[queueId]) {
1076                         logger.error(`[${track.artist.name} - ${track.title}] Failed to download: Not in queue`)
1077                         throw new Error(`Failed to download: Not in queue`)
1078                         return
1079                 }
1080                 if (track.id == 0){
1081                         logger.error(`[${track.artist.name} - ${track.title}] Failed to download: Wrong ID`)
1082                         throw new Error(`Failed to download: Wrong ID`)
1083                         return
1084                 }
1085
1086                 /* Album information is necessary for the following tags:
1087                  * album_artist
1088                  * album_artist_picture
1089                  * trackTotal
1090                  * recordType
1091                  * barcode
1092                  * explicit
1093                  * label
1094                  * genres
1095                  * date
1096                 */
1097                 if (parseInt(track.id)>0){
1098                         var ajson
1099                         if (!track.ajson){
1100                                 try{
1101                                         logger.info(`[${track.artist.name} - ${track.title}] Getting album info`)
1102                                         ajson = await s.Deezer.legacyGetAlbum(track.album.id)
1103                                 }catch(err){
1104                                         logger.warn(`[${track.artist.name} - ${track.title}] Album not found, trying to reach deeper`)
1105                                         try{
1106                                                 ajson = await s.Deezer.getAlbum(track.album.id)
1107                                                 ajson.fromNewAPI = true
1108                                         } catch(err){
1109                                                 if(track.fallbackId){
1110                                                         logger.warn(`[${track.artist.name} - ${track.title}] Failed to download track, falling on alternative`)
1111                                                         track = await s.Deezer.getTrack(track.fallbackId)
1112                                                         return downloadTrackObject(track, queueId, settings)
1113                                                 }else{
1114                                                         logger.error(`[${track.artist.name} - ${track.title}] Failed to download: ${err}`)
1115                                                         return
1116                                                 }
1117                                         }
1118                                 }
1119                         }else{
1120                                 ajson = track.ajson
1121                         }
1122                         if (!ajson.fromNewAPI){
1123                                 // Aquiring discTotal (only if necessary)
1124                                 if ((settings.tags.discTotal || settings.createCDFolder) && parseInt(track.id)>0){
1125                                         if (!ajson.discTotal){
1126                                                 logger.info(`[${track.artist.name} - ${track.title}] Getting total disc number`);
1127                                                 var discTotal = await s.Deezer.getAlbum(ajson.id)
1128                                                 track.discTotal = discTotal.discTotal
1129                                         }else{
1130                                                 track.discTotal = ajson.discTotal
1131                                         }
1132                                 }
1133                                 track.album.artist = {
1134                                         id: ajson.artist.id,
1135                                         name: ajson.artist.name,
1136                                         picture: ajson.artist.picture_small.split("/56x56-000000-80-0-0.jpg")[0].split(s.Deezer.artistPicturesHost)[1],
1137                                 }
1138                                 track.trackTotal = ajson.nb_tracks
1139                                 track.album.barcode = ajson.upc
1140                                 if (!ajson.record_type){
1141                                         track.recordType = swichReleaseType(track.recordType)
1142                                 }else{
1143                                         track.recordType = ajson.record_type
1144                                 }
1145                                 if (ajson.explicit_lyrics){
1146                                         track.album.explicit = ajson.explicit_lyrics;
1147                                 }
1148                                 if(ajson.label){
1149                                         track.publisher = ajson.label;
1150                                 }
1151                                 if (ajson.release_date) {
1152                                         track.date = {
1153                                                 day: ajson.release_date.slice(8,10),
1154                                                 month: ajson.release_date.slice(5,7),
1155                                                 year: ajson.release_date.slice(0, 4),
1156                                                 slicedYear: (settings.dateFormatYear == "2" ? ajson.release_date.slice(2, 4) : ajson.release_date.slice(0, 4))
1157                                         }
1158                                 }else if(!track.date){
1159                                         track.date = {
1160                                                 day: 0,
1161                                                 month: 0,
1162                                                 year: 0,
1163                                                 slicedYear: 0
1164                                         }
1165                                 }
1166                                 if(ajson.genres && ajson.genres.data[0] && ajson.genres.data[0].name){
1167                                         track.genre = [];
1168                                         genreArray = [];
1169                                         ajson.genres.data.forEach(function(genre){
1170                                                 genreArray.push(genre.name);
1171                                         });
1172                                         uniqueArray(genreArray, track.genre, false)
1173                                 }
1174                         }else{
1175                                 // Missing barcode, genre, recordType
1176                                 track.album = ajson
1177                                 track.date = track.album.date
1178                                 // TODO: Make a loop for each artist
1179
1180                         }
1181
1182                         // Acquiring bpm (only if necessary)
1183                         if (settings.tags.bpm){
1184                                 logger.info(`[${track.artist.name} - ${track.title}] Getting BPM`);
1185                                 try{
1186                                         var bpm = await s.Deezer.legacyGetTrack(track.id)
1187                                         track.bpm = bpm.bpm
1188                                 }catch(err){
1189                                         track.bpm = 0
1190                                 }
1191                         }else{
1192                                 track.bpm = 0
1193                         }
1194
1195                         // Acquiring ReplayGain value (only if necessary)
1196                         if (settings.tags.replayGain){
1197                                 logger.info(`[${track.artist.name} - ${track.title}] Getting track gain`);
1198                                 try{
1199                                         var gain = await s.Deezer.legacyGetTrack(track.id)
1200                                         track.replayGain = gain.gain
1201                                 }catch(err){
1202                                         track.replayGain = 0
1203                                 }
1204                         }else{
1205                                 track.replayGain = 0
1206                         }
1207
1208                         let separator = settings.multitagSeparator
1209                         if (separator == "null") separator = String.fromCharCode(parseInt("\u0000",16))
1210
1211                         // Autoremoves (Album Version) from the title
1212                         if (settings.removeAlbumVersion){
1213                                 if(track.title.indexOf("Album Version")>-1){
1214                                         track.title = track.title.replace(/\(Album Version\)/g,"")
1215                                         track.title.trim()
1216                                 }
1217                         }
1218
1219                         track.album.artist.pictureUrl = `${s.Deezer.artistPicturesHost}${track.album.artist.picture}/${settings.artworkSize}x${settings.artworkSize}-000000-80-0-0${(settings.PNGcovers ? ".png" : ".jpg")}`
1220                         track.album.pictureUrl = `${s.Deezer.albumPicturesHost}${track.album.picture}/${settings.artworkSize}x${settings.artworkSize}-000000-80-0-0${(settings.PNGcovers ? ".png" : ".jpg")}`
1221                         if(track.contributor){
1222                                 if(track.contributor.composer){
1223                                         track.composerString = []
1224                                         uniqueArray(track.contributor.composer, track.composerString, settings.removeDupedTags)
1225                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.composerString = track.composerString.join(separator)
1226                                 }
1227                                 if(track.contributor.musicpublisher){
1228                                         track.musicpublisherString = []
1229                                         uniqueArray(track.contributor.musicpublisher, track.musicpublisherString, settings.removeDupedTags)
1230                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.musicpublisherString = track.musicpublisherString.join(separator)
1231                                 }
1232                                 if(track.contributor.producer){
1233                                         track.producerString = []
1234                                         uniqueArray(track.contributor.producer, track.producerString, settings.removeDupedTags)
1235                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.producerString = track.producerString.join(separator)
1236                                 }
1237                                 if(track.contributor.engineer){
1238                                         track.engineerString = []
1239                                         uniqueArray(track.contributor.engineer, track.engineerString, settings.removeDupedTags)
1240                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.engineerString = track.engineerString.join(separator)
1241                                 }
1242                                 if(track.contributor.writer){
1243                                         track.writerString = []
1244                                         uniqueArray(track.contributor.writer, track.writerString, settings.removeDupedTags)
1245                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.writerString = track.writerString.join(separator)
1246                                 }
1247                                 if(track.contributor.author){
1248                                         track.authorString = []
1249                                         uniqueArray(track.contributor.author, track.authorString, settings.removeDupedTags)
1250                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.authorString = track.authorString.join(separator)
1251                                 }
1252                                 if(track.contributor.mixer){
1253                                         track.mixerString = [];
1254                                         uniqueArray(track.contributor.mixer, track.mixerString, settings.removeDupedTags)
1255                                         if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.mixerString = track.mixerString.join(separator)
1256                                 }
1257                         }
1258
1259                         if(track.artists || track.artistsString){
1260                                 if (!track.artistsString){
1261                                         track.artistsString = []
1262                                         artistArray = []
1263                                         track.artists.forEach(function(artist){
1264                                                 artistArray.push(artist.name)
1265                                         })
1266                                 }else{
1267                                         artistArray = track.artistsString
1268                                 }
1269                                 if (! Array.isArray(track.artistsString)){
1270                                         track.artistsString = [track.artistsString]
1271                                 }
1272                                 uniqueArray(artistArray, track.artistsString, settings.removeDupedTags)
1273                                 let posMainArtist = track.artistsString.indexOf(track.album.artist.name)
1274                                 if (posMainArtist !== -1 && posMainArtist !== 0 && settings.removeDupedTags){
1275                                         let element = track.artistsString[posMainArtist]
1276                                 track.artistsString.splice(posMainArtist, 1)
1277                                 track.artistsString.splice(0, 0, element)
1278                                 }
1279                                 if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.artistsString = track.artistsString.join(separator)
1280                         }
1281                         if (track.genre){
1282                                 if (!(track.selectedFormat == 9 && separator==String.fromCharCode(parseInt("\u0000",16)))) track.genreString = track.genre.join(separator)
1283                         }
1284
1285                         if (track.date){
1286                                 let date
1287                                 switch (settings.dateFormat){
1288                                         case "0": date = `${track.date.slicedYear}-${track.date.month}-${track.date.day}`; break;
1289                                         case "1": date = `${track.date.day}-${track.date.month}-${track.date.slicedYear}`; break;
1290                                         case "2": date = `${track.date.month}-${track.date.day}-${track.date.slicedYear}`; break;
1291                                         case "3": date = `${track.date.slicedYear}-${track.date.day}-${track.date.month}`; break;
1292                                         case "4": date = `${track.date.day}${track.date.month}`; break;
1293                                         default: date = `${track.date.day}${track.date.month}`; break;
1294                                 }
1295                                 track.dateString = date;
1296                         }
1297                 }else{
1298                         track.date = {year: 0,day: 0,month: 0}
1299                 }
1300
1301                 if(settings.plName && !(settings.createArtistFolder || settings.createAlbumFolder) && !settings.numplaylistbyalbum){
1302                         track.trackNumber = (track.position+1).toString();
1303                         track.trackTotal = settings.playlist.fullSize;
1304                         track.discNumber = "1";
1305                         track.discTotal = "1";
1306                 }
1307
1308                 // Auto detect aviable track format from settings
1309                 if (parseInt(track.id)>0){
1310                         switch(settings.maxBitrate){
1311                                 case "9":
1312                                         track.selectedFormat = 9
1313                                         track.selectedFilesize = track.filesize.flac
1314                                         if (track.filesize.flac>0) break
1315                                         if (!settings.fallbackBitrate) throw new Error("Song not found at desired bitrate.")
1316                                 case "3":
1317                                         track.selectedFormat = 3
1318                                         track.selectedFilesize = track.filesize.mp3_320
1319                                         if (track.filesize.mp3_320>0) break
1320                                         if (!settings.fallbackBitrate) throw new Error("Song not found at desired bitrate.")
1321                                 case "1":
1322                                         track.selectedFormat = 1
1323                                         track.selectedFilesize = track.filesize.mp3_128
1324                                         if (track.filesize.mp3_128>0) break
1325                                         if (!settings.fallbackBitrate) throw new Error("Song not found at desired bitrate.")
1326                                 default:
1327                                         track.selectedFormat = 8
1328                                         track.selectedFilesize = track.filesize.default
1329                         }
1330                 }else{
1331                         track.selectedFilesize = track.filesize
1332                         track.selectedFormat = 3
1333                 }
1334
1335                 // TODO: Move to a separate function
1336                 // Generating file name
1337                 if (settings.saveFullArtists && settings.multitagSeparator != null){
1338                         let filename = fixName(`${track.artistsString} - ${track.title}`);
1339                 }else{
1340                         let filename = fixName(`${track.artist.name} - ${track.title}`);
1341                 }
1342                 if (settings.filename) {
1343                         filename = settingsRegex(track, settings.filename, settings.playlist, settings.saveFullArtists && settings.multitagSeparator != null, settings.paddingSize);
1344                 }
1345
1346                 // TODO: Move to a separate function
1347                 // Generating file path
1348                 let filepath = mainFolder;
1349                 let artistPath;
1350                 if (settings.createArtistFolder || settings.createAlbumFolder) {
1351
1352                         if(settings.plName){
1353                                 filepath += antiDot(fixName(settings.plName)) + path.sep;
1354                         }
1355
1356                         if (settings.createArtistFolder) {
1357                                 if(settings.artName){
1358                                         filepath += antiDot(fixName(settings.artName)) + path.sep;
1359                                 }else{
1360                                         filepath += antiDot(fixName(track.album.artist.name)) + path.sep;
1361                                 }
1362                                 artistPath = filepath;
1363                         }
1364
1365                         if (settings.createAlbumFolder) {
1366                                 if(settings.artName){
1367                                         filepath += antiDot(settingsRegexAlbum(settings.foldername,settings.artName,settings.albName,track.date.year,track.recordType,track.album.explicit,track.publisher,track.genre)) + path.sep;
1368                                 }else{
1369                                         filepath += antiDot(settingsRegexAlbum(settings.foldername,track.album.artist.name,track.album.title,track.date.year,track.recordType,track.album.explicit,track.publisher,track.genre)) + path.sep;
1370                                 }
1371                         }
1372                 } else if (settings.plName) {
1373                         filepath += antiDot(fixName(settings.plName)) + path.sep;
1374                 } else if (settings.artName) {
1375                         filepath += antiDot(settingsRegexAlbum(settings.foldername,settings.artName,settings.albName,track.date.year,track.recordType,track.album.explicit,track.publisher,track.genre)) + path.sep;
1376                 }
1377                 let coverpath = filepath;
1378                 if (track.discTotal > 1 && (settings.artName || settings.createAlbumFolder) && settings.createCDFolder){
1379                         filepath += `CD${track.discNumber +  path.sep}`
1380                 }
1381                 let writePath;
1382
1383                 if(track.selectedFormat == 9){
1384                         writePath = filepath + filename + '.flac';
1385                 }else{
1386                         writePath = filepath + filename + '.mp3';
1387                 }
1388
1389                 if ((settings.syncedlyrics || settings.tags.unsynchronisedLyrics) && track.lyricsId>0){
1390                         let lyr = await s.Deezer.getLyrics(track.id)
1391                         track.syncLyrics = lyr.syncLyrics
1392                         track.unsyncLyrics = lyr.unsyncLyrics
1393                 }
1394
1395                 if(track.syncLyrics && settings.syncedlyrics){
1396                         fs.outputFile(writePath.substring(0,writePath.lastIndexOf('.'))+".lrc",track.syncLyrics,function(){});
1397                 }
1398
1399                 track.playlistData = [0,""]
1400                 if (settings.createM3UFile && (settings.plName || settings.albName)) {
1401                         if (track.position){
1402                                 track.playlistData = [parseInt(track.position), writePath];
1403                         }else{
1404                                 track.playlistData = [track.trackNumber-1, writePath];
1405                         }
1406                 }
1407                 if (fs.existsSync(writePath)) {
1408                         logger.info(`[${track.artist.name} - ${track.title}] Already downloaded`);
1409                         return;
1410                 }else{
1411                         logger.info(`[${track.artist.name} - ${track.title}] Downloading file to ${writePath}`);
1412                 }
1413                 // Get cover image
1414                 if (track.album.pictureUrl) {
1415                         let imgPath;
1416                         //If its not from an album but a playlist.
1417                         if(!(settings.albName || settings.createAlbumFolder)){
1418                                 imgPath = coverArtFolder + (track.album.barcode ? fixName(track.album.barcode) : fixName(`${track.album.artist.name} - ${track.album.title}`))+(settings.PNGcovers ? ".png" : ".jpg")
1419                         }else{
1420                                 if (settings.saveArtwork)
1421                                         imgPath = coverpath + settingsRegexCover(settings.coverImageTemplate,track.album.artist.name,track.album.title)+(settings.PNGcovers ? ".png" : ".jpg")
1422                                 else
1423                                         imgPath = coverArtFolder + fixName(track.album.barcode ? fixName(track.album.barcode) : fixName(`${track.album.artist.name} - ${track.album.title}`))+(settings.PNGcovers ? ".png" : ".jpg")
1424                         }
1425                         if(fs.existsSync(imgPath)){
1426                                 track.album.picturePath = (imgPath).replace(/\\/g, "/")
1427                                 logger.info(`[${track.artist.name} - ${track.title}] Starting the download process CODE:1`)
1428                         }else{
1429                                 try{
1430                                         var body = await request.get(track.album.pictureUrl, {strictSSL: false,encoding: 'binary'})
1431                                         fs.outputFileSync(imgPath,body,'binary')
1432                                         track.album.picturePath = (imgPath).replace(/\\/g, "/")
1433                                         logger.info(`[${track.artist.name} - ${track.title}] Starting the download process CODE:2`)
1434                                 }catch(error){
1435                                         logger.error(`[${track.artist.name} - ${track.title}] Cannot download Album Image: ${error}`)
1436                                         logger.error(`Album art link: ${track.album.pictureUrl}`)
1437                                         track.album.pictureUrl = undefined
1438                                         track.album.picturePath = undefined
1439                                 }
1440                         }
1441                 }else{
1442                         track.album.pictureUrl = undefined
1443                         logger.info(`[${track.artist.name} - ${track.title}] Starting the download process CODE:3`)
1444                 }
1445
1446                 // Get Artist Image
1447                 if (parseInt(this.id)>0 && track.album.artist.picture && settings.saveArtworkArtist) {
1448                         let imgPath;
1449                         if(settings.createArtistFolder){
1450                                 imgPath = artistPath + antiDot(settingsRegexArtistCover(settings.artistImageTemplate,track.album.artist.name))+(settings.PNGcovers ? ".png" : ".jpg");
1451                                 if(!fs.existsSync(imgPath)){
1452                                         try{
1453                                                 var body = await request.get(track.album.artist.pictureUrl, {strictSSL: false,encoding: 'binary'})
1454                                                 if (body.indexOf("unauthorized")>-1) throw new Error("Unauthorized")
1455                                                 fs.outputFileSync(imgPath,body,'binary')
1456                                                 logger.info(`[${track.artist.name} - ${track.title}] Saved Artist Image`)
1457                                         }catch(err){
1458                                                 logger.error(`[${track.artist.name} - ${track.title}] Cannot download Artist Image: ${err}`)
1459                                         }
1460                                 }
1461                         }
1462                 }
1463
1464                 let tempPath
1465                 if(parseInt(track.id)>0)
1466                         tempPath = `${filepath}${track.id}_${track.selectedFormat}.temp`
1467                 else
1468                         tempPath = writePath
1469
1470                 logger.info(`[${track.artist.name} - ${track.title}] Downloading track`)
1471                 var downloadingPromise = new Promise((resolve, reject)=>{
1472                         let req = requestOld.get({url: track.getDownloadUrl(track.selectedFormat), strictSSL: false, headers: s.Deezer.httpHeaders, encoding: 'binary'}, function (error, response, body) {
1473                                 if (error){
1474                                         logger.error(`[${track.artist.name} - ${track.title}] Downloading error: ${error}`)
1475                                         reject("Downloading error: "+error)
1476                                         return false
1477                                 }
1478                                 if (!s.downloadQueue[queueId]){
1479                                         fs.remove(tempPath)
1480                                         reject("Not in Queue")
1481                                         return false
1482                                 }
1483                                 if (body.length == 0){
1484                                         logger.error(`[${track.artist.name} - ${track.title}] Downloading error: Track is Empty`)
1485                                         fs.remove(tempPath)
1486                                         reject("Track is Empty")
1487                                         return false
1488                                 }
1489                                 logger.info(`[${track.artist.name} - ${track.title}] Decrypting track`)
1490                                 var decryptedSource = s.Deezer.decryptDownload(Buffer.from(body, 'binary'), track.id)
1491                                 try{
1492                                         fs.outputFileSync(tempPath,decryptedSource)
1493                                         resolve()
1494                                 }catch(err){
1495                                         logger.error(`[${track.artist.name} - ${track.title}] Decryption error: ${err}`)
1496                                         reject(err)
1497                                         return false
1498                                 }
1499                         }).on("data", function(data) {
1500                                 if (!s.downloadQueue[queueId]){
1501                                         reject("Not in Queue")
1502                                         return false
1503                                 }
1504                         })
1505                         if((s.downloadQueue[queueId]) && s.downloadQueue[queueId].type == "track"){
1506                                 let chunkLength = 0
1507                                 req.on("data", function(data) {
1508                                         if (!s.downloadQueue[queueId]){
1509                                                 reject("Not in Queue")
1510                                         }
1511                                         chunkLength += data.length
1512                                         try{
1513                                                 if (!s.downloadQueue[queueId].percentage) {
1514                                                         s.downloadQueue[queueId].percentage = 0
1515                                                 }
1516                                                 let complete = track.selectedFilesize
1517                                                 let percentage = (chunkLength / complete) * 100;
1518                                                 if ((percentage - s.downloadQueue[queueId].percentage > 1) || (chunkLength == complete)) {
1519                                                         s.downloadQueue[queueId].percentage = percentage
1520                                                         s.emit("downloadProgress", {
1521                                                                 queueId: queueId,
1522                                                                 percentage: s.downloadQueue[queueId].percentage-5
1523                                                         })
1524                                                 }
1525                                         }catch(err){}
1526                                 })
1527                         }
1528                 })
1529
1530
1531                 await downloadingPromise
1532
1533                 logger.info(`[${track.artist.name} - ${track.title}] Adding Tags`)
1534                 if (parseInt(track.id)>0){
1535                         if(track.selectedFormat == 9){
1536                                 let flacComments = [];
1537                                 if (settings.tags.title)
1538                                         flacComments.push('TITLE=' + track.title);
1539                                 if (settings.tags.album)
1540                                         flacComments.push('ALBUM=' + track.album.title);
1541                                 if (settings.tags.albumArtist)
1542                                         flacComments.push('ALBUMARTIST=' + track.album.artist.name);
1543                                 if (settings.tags.trackNumber)
1544                                         flacComments.push('TRACKNUMBER=' + track.trackNumber);
1545                                 if (settings.tags.discNumber)
1546                                         flacComments.push('DISCNUMBER=' + track.discNumber);
1547                                 if (settings.tags.trackTotal)
1548                                         flacComments.push('TRACKTOTAL=' + track.trackTotal);
1549                                 if (settings.tags.explicit)
1550                                         flacComments.push('ITUNESADVISORY=' + track.explicit);
1551                                 if (settings.tags.isrc)
1552                                         flacComments.push('ISRC=' + track.ISRC);
1553                                 if (settings.tags.artist && track.artistsString)
1554                                         if (Array.isArray(track.artistsString)){
1555                                                 track.artistsString.forEach(x=>{
1556                                                         flacComments.push('ARTIST=' + x);
1557                                                 });
1558                                         }else{
1559                                                 flacComments.push('ARTIST=' + track.artistsString);
1560                                         }
1561                                 if (settings.tags.discTotal)
1562                                         flacComments.push('DISCTOTAL='+track.discTotal);
1563                                 if (settings.tags.length)
1564                                         flacComments.push('LENGTH=' + track.duration);
1565                                 if (settings.tags.barcode && track.album.barcode)
1566                                         flacComments.push('BARCODE=' + track.album.barcode);
1567                                 if (track.unsyncLyrics && settings.tags.unsynchronisedLyrics)
1568                                         flacComments.push('LYRICS='+track.unsyncLyrics.lyrics);
1569                                 if (track.genreString && settings.tags.genre)
1570                                         if (Array.isArray(track.genreString)){
1571                                                 track.genreString.forEach(x=>{
1572                                                         flacComments.push('GENRE=' + x);
1573                                                 });
1574                                         }else{
1575                                                 flacComments.push('GENRE=' + track.genreString);
1576                                         }
1577                                 if (track.copyright && settings.tags.copyright)
1578                                         flacComments.push('COPYRIGHT=' + track.copyright);
1579                                 if (0 < parseInt(track.date.year)){
1580                                         if (settings.tags.year)
1581                                                 flacComments.push('YEAR=' + track.date.year);
1582                                         if (settings.tags.date)
1583                                         flacComments.push('DATE=' + track.dateString);
1584                                 }
1585                                 if (0 < parseInt(track.bpm) && settings.tags.bpm)
1586                                         flacComments.push('BPM=' + track.bpm);
1587                                 if(track.publisher && settings.tags.publisher)
1588                                         flacComments.push('PUBLISHER=' + track.publisher);
1589                                 if(track.composerString && settings.tags.composer)
1590                                         if (Array.isArray(track.composerString)){
1591                                                 track.composerString.forEach(x=>{
1592                                                         flacComments.push('COMPOSER=' + x);
1593                                                 });
1594                                         }else{
1595                                                 flacComments.push('COMPOSER=' + track.composerString);
1596                                         }
1597                                 if(track.musicpublisherString && settings.tags.musicpublisher)
1598                                         if (Array.isArray(track.musicpublisherString)){
1599                                                 track.musicpublisherString.forEach(x=>{
1600                                                         flacComments.push('ORGANIZATION=' + x);
1601                                                 });
1602                                         }else{
1603                                                 flacComments.push('ORGANIZATION=' + track.musicpublisherString);
1604                                         }
1605                                 if(track.mixerString && settings.tags.mixer)
1606                                         if (Array.isArray(track.mixerString)){
1607                                                 track.mixerString.forEach(x=>{
1608                                                         flacComments.push('MIXER=' + x);
1609                                                 });
1610                                         }else{
1611                                                 flacComments.push('MIXER=' + track.mixerString);
1612                                         }
1613                                 if(track.authorString && settings.tags.author)
1614                                         if (Array.isArray(track.authorString)){
1615                                                 track.authorString.forEach(x=>{
1616                                                         flacComments.push('AUTHOR=' + x);
1617                                                 });
1618                                         }else{
1619                                                 flacComments.push('AUTHOR=' + track.authorString);
1620                                         }
1621                                 if(track.writerString && settings.tags.writer)
1622                                         if (Array.isArray(track.writerString)){
1623                                                 track.writerString.forEach(x=>{
1624                                                         flacComments.push('WRITER=' + x);
1625                                                 });
1626                                         }else{
1627                                                 flacComments.push('WRITER=' + track.writerString);
1628                                         }
1629                                 if(track.engineerString && settings.tags.engineer)
1630                                         if (Array.isArray(track.engineerString)){
1631                                                 track.engineerString.forEach(x=>{
1632                                                         flacComments.push('ENGINEER=' + x);
1633                                                 });
1634                                         }else{
1635                                                 flacComments.push('ENGINEER=' + track.engineerString);
1636                                         }
1637                                 if(track.producerString && settings.tags.producer)
1638                                         if (Array.isArray(track.producerString)){
1639                                                 track.producerString.forEach(x=>{
1640                                                         flacComments.push('PRODUCER=' + x);
1641                                                 });
1642                                         }else{
1643                                                 flacComments.push('PRODUCER=' + track.producerString);
1644                                         }
1645                                 if(track.replayGain && settings.tags.replayGain)
1646                                         flacComments.push('REPLAYGAIN_TRACK_GAIN=' + track.replayGain);
1647
1648                                 const reader = fs.createReadStream(tempPath);
1649                                 const writer = fs.createWriteStream(writePath);
1650                                 let processor = new mflac.Processor({parseMetaDataBlocks: true});
1651                                 let vendor = 'reference libFLAC 1.2.1 20070917';
1652                                 let cover = null;
1653                                 if(track.album.picturePath && settings.tags.cover){
1654                                         cover = fs.readFileSync(track.album.picturePath)
1655                                 }
1656                                 let mdbVorbisPicture
1657                                 let mdbVorbisComment
1658                                 processor.on('preprocess', (mdb) => {
1659                                         // Remove existing VORBIS_COMMENT and PICTURE blocks, if any.
1660                                         if (mflac.Processor.MDB_TYPE_VORBIS_COMMENT === mdb.type) {
1661                                                 mdb.remove();
1662                                         } else if (mflac.Processor.MDB_TYPE_PICTURE === mdb.type) {
1663                                                 mdb.remove();
1664                                         }
1665                                         if (mdb.isLast) {
1666                                                 if(cover){
1667                                                         mdbVorbisPicture = mflac.data.MetaDataBlockPicture.create(true, 3, `image/${(settings.PNGcovers ? "png" : "jpeg")}`, '', settings.artworkSize, settings.artworkSize, 24, 0, cover);
1668                                                 }
1669                                                 mdbVorbisComment = mflac.data.MetaDataBlockVorbisComment.create(!cover, vendor, flacComments);
1670                                                 mdb.isLast = false;
1671                                         }
1672                                 });
1673                                 processor.on('postprocess', (mdb) => {
1674                                         if (mflac.Processor.MDB_TYPE_VORBIS_COMMENT === mdb.type && null !== mdb.vendor) {
1675                                                 vendor = mdb.vendor;
1676                                         }
1677                                         if (mdbVorbisPicture && mdbVorbisComment) {
1678                                                         processor.push(mdbVorbisComment.publish());
1679                                                         processor.push(mdbVorbisPicture.publish());
1680                                                 }else if(mdbVorbisComment){
1681                                                         processor.push(mdbVorbisComment.publish());
1682                                         }
1683                                 });
1684                                 reader.on('end', () => {
1685                                         fs.remove(tempPath);
1686                                 });
1687                                 await reader.pipe(processor).pipe(writer);
1688                         }else{
1689                                 const songBuffer = fs.readFileSync(tempPath);
1690                                 const writer = new ID3Writer(songBuffer);
1691                                 if (settings.tags.title)
1692                                         writer.setFrame('TIT2', track.title)
1693                                 if (settings.tags.artist)
1694                                         writer.setFrame('TPE1', [track.artistsString])
1695                                 if (settings.tags.album)
1696                                         writer.setFrame('TALB', track.album.title)
1697                                 if (settings.tags.albumArtist && track.album.artist)
1698                                         writer.setFrame('TPE2', track.album.artist.name)
1699                                 if (settings.tags.trackNumber)
1700                                         writer.setFrame('TRCK', (settings.tags.trackTotal ? track.trackNumber+"/"+track.trackTotal : track.trackNumber))
1701                                 if (settings.tags.discNumber)
1702                                         writer.setFrame('TPOS', (settings.tags.discTotal ? track.discNumber+"/"+track.discTotal : track.discNumber))
1703                                 if (settings.tags.isrc)
1704                                         writer.setFrame('TSRC', track.ISRC);
1705
1706                                 if (settings.tags.length)
1707                                         writer.setFrame('TLEN', track.duration);
1708                                 if (settings.tags.barcode && track.album.barcode)
1709                                         writer.setFrame('TXXX', {
1710                                                 description: 'BARCODE',
1711                                                 value: track.album.barcode
1712                                         });
1713                                 if(track.album.picturePath && settings.tags.cover){
1714                                         const coverBuffer = fs.readFileSync(track.album.picturePath);
1715                                         writer.setFrame('APIC', {
1716                                                 type: 3,
1717                                                 data: coverBuffer,
1718                                                 description: ''
1719                                         });
1720                                 }
1721                                 if(track.unsyncLyrics && settings.tags.unsynchronisedLyrics)
1722                                         writer.setFrame('USLT', track.unsyncLyrics);
1723                                 if(track.publisher && settings.tags.publisher)
1724                                         writer.setFrame('TPUB', track.publisher);
1725                                 if(track.genreString && settings.tags.genre)
1726                                         writer.setFrame('TCON', [track.genreString]);
1727                                 if(track.copyright && settings.tags.copyright)
1728                                         writer.setFrame('TCOP', track.copyright);
1729                                 if (0 < parseInt(track.date.year)) {
1730                                         if (settings.tags.date)
1731                                                 writer.setFrame('TDAT', track.dateString);
1732                                         if (settings.tags.year)
1733                                                 writer.setFrame('TYER', track.date.year);
1734                                 }
1735                                 if (0 < parseInt(track.bpm) && settings.tags.bpm)
1736                                         writer.setFrame('TBPM', track.bpm);
1737                                 if(track.composerString && settings.tags.composer)
1738                                         writer.setFrame('TCOM', [track.composerString]);
1739                                 if(track.replayGain && settings.tags.replayGain)
1740                                         writer.setFrame('TXXX', {
1741                                                 description: 'REPLAYGAIN_TRACK_GAIN',
1742                                                 value: track.replayGain
1743                                         });
1744                                 writer.addTag();
1745                                 const taggedSongBuffer = Buffer.from(writer.arrayBuffer);
1746                                 fs.writeFileSync(writePath, taggedSongBuffer);
1747                                 fs.remove(tempPath);
1748                         }
1749                 }
1750                 logger.info(`[${track.artist.name} - ${track.title}] Downloaded`)
1751         }
1752 })
1753
1754 // Helper functions
1755
1756 /**
1757  * Updates individual parameters in the settings file
1758  * @param config
1759  * @param value
1760  */
1761 function updateSettingsFile(config, value) {
1762         configFile.userDefined[config] = value;
1763
1764         fs.outputFile(configFileLocation, JSON.stringify(configFile, null, 2), function (err) {
1765                 if (err) return;
1766                 logger.info("Settings updated");
1767
1768                 // FIXME: Endless Loop, due to call from initFolders()...crashes soon after startup
1769                 // initFolders();
1770         });
1771 }
1772
1773 function fixName (txt) {
1774         txt = txt+""
1775   const regEx = /[\0\/\\:*?"<>|]/g;
1776   txt = txt.replace(regEx, '_');
1777   txt = txt.slice(0,200);
1778   return txt;
1779 }
1780
1781 function antiDot(str){
1782         while(str[str.length-1] == "." || str[str.length-1] == " " || str[str.length-1] == "\n"){
1783                 str = str.substring(0,str.length-1);
1784         }
1785         if(str.length < 1){
1786                 str = "dot";
1787         }
1788         return str;
1789 }
1790
1791 /**
1792  * Initialize the temp folder for covers and main folder for downloads
1793  */
1794 function initFolders() {
1795         // Check if main folder exists
1796         if (!fs.existsSync(mainFolder)) {
1797                 mainFolder = defaultDownloadFolder;
1798                 updateSettingsFile('downloadLocation', defaultDownloadFolder);
1799         }
1800         //fs.removeSync(coverArtFolder);
1801         //fs.ensureFolderSync(coverArtFolder);
1802 }
1803
1804 /**
1805  * Creates the name of the tracks replacing wildcards to correct metadata
1806  * @param track
1807  * @param filename
1808  * @param playlist
1809  * @returns {XML|string|*}
1810  */
1811 function settingsRegex(track, filename, playlist, saveFullArtists, paddingSize) {
1812         try{
1813                 filename = filename.replace(/%title%/g, fixName(track.title));
1814                 filename = filename.replace(/%album%/g, fixName(track.album.title));
1815                 filename = filename.replace(/%artist%/g, fixName((saveFullArtists ? track.artistsString : track.artist.name)));
1816                 filename = filename.replace(/%year%/g, fixName(track.date.year));
1817                 filename = filename.replace(/%label%/g, fixName(track.publisher));
1818                 if(typeof track.trackNumber != 'undefined'){
1819                         if(configFile.userDefined.padtrck){
1820                                  filename = filename.replace(/%number%/g, fixName(pad(track.trackNumber, (parseInt(paddingSize)>0 ? parseInt(paddingSize) : track.trackTotal))));
1821                         }else{
1822                                 filename = filename.replace(/%number%/g, fixName(track.trackNumber));
1823                         }
1824                 } else {
1825                         filename = filename.replace(/%number%/g, '');
1826                 }
1827                 filename = filename.replace(/%explicit%/g, fixName((track.explicit==="1" ? (filename.indexOf(/[^%]explicit/g)>-1 ? "" : "(Explicit Version)") : "")));
1828                 filename = filename.replace(/%label%/g, fixName(track.genre ? (Array.isArray(track.genre) ? track.genre[0] : track.genre) : "Unknown"));
1829                 return filename.trim();
1830         }catch(e){
1831                 logger.error("settingsRegex failed: "+e)
1832         }
1833 }
1834
1835 /**
1836  * Creates the name of the albums folder replacing wildcards to correct metadata
1837  * @param metadata
1838  * @param foldername
1839  * @returns {XML|string|*}
1840  */
1841 function settingsRegexAlbum(foldername, artist, album, year, rtype, explicit, publisher, genres) {
1842         try{
1843                 foldername = foldername.replace(/%album%/g, fixName(album))
1844                 foldername = foldername.replace(/%artist%/g, fixName(artist))
1845                 foldername = foldername.replace(/%year%/g, fixName(year))
1846                 if (rtype){
1847                         foldername = foldername.replace(/%type%/g, fixName(rtype[0].toUpperCase() + rtype.substring(1)))
1848                 }else{
1849                         foldername = foldername.replace(/%type%/g, "")
1850                 }
1851                 foldername = foldername.replace(/%label%/g, fixName(publisher))
1852                 foldername = foldername.replace(/%explicit%/g, fixName((explicit ? (foldername.indexOf(/[^%]explicit/g)>-1 ? "" : "(Explicit)") : "")))
1853                 foldername = foldername.replace(/%genre%/g, fixName(genres ? (Array.isArray(genres) ? genres[0] : genres) : "Unknown"))
1854                 return foldername.trim();
1855         }catch(e){
1856                 logger.error("settingsRegexAlbum failed: "+e)
1857         }
1858
1859 }
1860
1861 function settingsRegexCover(foldername, artist, name) {
1862         foldername = foldername.replace(/%name%/g, fixName(name));
1863         foldername = foldername.replace(/%artist%/g, fixName(artist));
1864         return foldername;
1865 }
1866
1867 function settingsRegexArtistCover(foldername, artist) {
1868         foldername = foldername.replace(/%artist%/g, fixName(artist));
1869         return foldername;
1870 }
1871
1872 /**
1873  * Pad number with 0s so max and str have the same nuber of characters
1874  * @param str
1875  * @param max
1876  * @returns {String|string|*}
1877  */
1878 function pad(str, max) {
1879         str = str.toString();
1880         max = max.toString();
1881         return str.length < max.length || str.length == 1 ? pad("0" + str, max) : str;
1882 }
1883
1884 /**
1885  * Splits the %number%
1886  * @param string str
1887  * @return string
1888  */
1889 function splitNumber(str,total){
1890         str = str.toString();
1891         let i = str.indexOf("/");
1892         if(total && i > 0){
1893                 return str.slice(i+1, str.length);
1894         }else if(i > 0){
1895                 return str.slice(0, i);
1896         }else{
1897                 return str;
1898         }
1899         return i > 0 ? str.slice(0, i) : str;
1900 }
1901
1902 function swichReleaseType(id){
1903         switch (id) {
1904                 case "0":
1905                         return "Album";
1906                 case "1":
1907                         return "Single";
1908                 case "3":
1909                         return "EP";
1910                 default:
1911                         return id;
1912         }
1913 }
1914
1915 function uniqueArray(origin, destination, removeDupes=true){
1916         Array.from(new Set(origin)).forEach(function(x){
1917                 if(destination.indexOf(x) == -1)
1918                         destination.push(x);
1919         });
1920         if (removeDupes){
1921                 destination.forEach((name,index)=>{
1922                         destination.forEach((name2,index2)=>{
1923                                 if(!(index===index2) && (name.indexOf(name2)!== -1)){
1924                                         destination.splice(index, 1);
1925                                 }
1926                         })
1927                 })
1928         }
1929 }
1930
1931 async function asyncForEach(array, callback) {
1932   for (let index = 0; index < array.length; index++) {
1933     await callback(array[index], index, array);
1934   }
1935 }
1936
1937 // Show crash error in console for debugging
1938 process.on('unhandledRejection', function (err) {
1939         if (err) logger.error(err.stack ? err.stack : err)
1940
1941 })
1942 process.on('uncaughtException', function (err) {
1943         if (err) logger.error(err.stack ? err.stack : err)
1944 })
1945
1946 // Exporting vars
1947 module.exports.mainFolder = mainFolder
1948 module.exports.defaultSettings = defaultSettings
1949 module.exports.defaultDownloadFolder = defaultDownloadFolder