Removed customization from config and brought back -p(ath) argument

This commit is contained in:
FatKiwi 2020-01-01 10:20:25 +00:00
parent a1d22d0ad3
commit 02364e373c
1 changed files with 139 additions and 208 deletions

View File

@ -25,17 +25,7 @@ let configService = new ConfigService(configFile);
const EncryptionService = require('./src/service/EncryptionService'); const EncryptionService = require('./src/service/EncryptionService');
let encryptionService = new EncryptionService(); let encryptionService = new EncryptionService();
const NamingService = require('./src/service/NamingService'); let DOWNLOAD_DIR = 'DOWNLOADS/';
let namingService = new NamingService(configService);
const log = winston.createLogger({
level: 'debug',
transports: [
new winston.transports.File({filename: 'SMLoadr.log'})
]
});
let PLAYLIST_DIR = 'PLAYLISTS/'; let PLAYLIST_DIR = 'PLAYLISTS/';
let PLAYLIST_FILE_ITEMS = {}; let PLAYLIST_FILE_ITEMS = {};
@ -79,6 +69,13 @@ const cliOptionDefinitions = [
defaultValue: 'MP3_320', defaultValue: 'MP3_320',
description: 'The quality of the files to download: MP3_128/MP3_320/FLAC' description: 'The quality of the files to download: MP3_128/MP3_320/FLAC'
}, },
{
name: 'path',
alias: 'p',
type: String,
defaultValue: DOWNLOAD_DIR,
description: 'The path to download the files to: path with / in the end'
},
{ {
name: 'url', name: 'url',
alias: 'u', alias: 'u',
@ -125,7 +122,7 @@ let requestWithCache;
function initRequest() { function initRequest() {
httpHeaders = { httpHeaders = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
'cache-control': 'max-age=0', 'cache-control': 'max-age=0',
'accept-language': 'en-US,en;q=0.9,en-US;q=0.8,en;q=0.7', 'accept-language': 'en-US,en;q=0.9,en-US;q=0.8,en;q=0.7',
'accept-charset': 'utf-8,ISO-8859-1;q=0.8,*;q=0.7', 'accept-charset': 'utf-8,ISO-8859-1;q=0.8,*;q=0.7',
@ -175,24 +172,19 @@ function initRequest() {
*/ */
(function initApp() { (function initApp() {
process.on('unhandledRejection', (reason, p) => { process.on('unhandledRejection', (reason, p) => {
log.debug(reason + 'Unhandled Rejection at Promise' + p);
console.error('\n' + reason + '\nUnhandled Rejection at Promise' + JSON.stringify(p) + '\n'); console.error('\n' + reason + '\nUnhandled Rejection at Promise' + JSON.stringify(p) + '\n');
}); });
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
log.debug(err + 'Uncaught Exception thrown');
console.error('\n' + err + '\nUncaught Exception thrown' + '\n'); console.error('\n' + err + '\nUncaught Exception thrown' + '\n');
process.exit(1); process.exit(1);
}); });
// App info // App info
console.log(chalk.cyan('╔══════════════════════════════════════════════════════════════════╗')); console.log(chalk.cyan('╔══════════════════════════════════════════════════════════════════╗'));
console.log(chalk.cyan('║') + chalk.bold.yellow(' SMLoadr v' + packageJson.version + ' ') + chalk.cyan('║')); console.log(chalk.cyan('║') + chalk.bold.yellow(' SMLoadr v' + packageJson.version + ' ') + chalk.cyan('║'));
console.log(chalk.cyan('╠══════════════════════════════════════════════════════════════════╣')); console.log(chalk.cyan('╠══════════════════════════════════════════════════════════════════╣'));
console.log(chalk.cyan('║') + ' DOWNLOADS: https://git.fuwafuwa.moe/SMLoadrDev/SMLoadr/releases' + chalk.cyan('║')); console.log(chalk.cyan('║') + ' GIT: https://git.fuwafuwa.moe/SMLoadrDev/SMLoadr ' + chalk.cyan('║'));
console.log(chalk.cyan('║') + ' MANUAL: https://git.fuwafuwa.moe/SMLoadrDev/SMLoadr ' + chalk.cyan('║'));
console.log(chalk.cyan('║') + ' NEWS: https://t.me/SMLoadrNews ' + chalk.cyan('║'));
console.log(chalk.cyan('╚══════════════════════════════════════════════════════════════════╝')); console.log(chalk.cyan('╚══════════════════════════════════════════════════════════════════╝'));
@ -201,6 +193,7 @@ function initRequest() {
fs.writeFileSync(DOWNLOAD_LINKS_FILE, ''); fs.writeFileSync(DOWNLOAD_LINKS_FILE, '');
} }
nodePath.normalize(DOWNLOAD_DIR).replace(/\/$|\\$/, '');
nodePath.normalize(PLAYLIST_DIR).replace(/\/$|\\$/, ''); nodePath.normalize(PLAYLIST_DIR).replace(/\/$|\\$/, '');
if (isCli) { if (isCli) {
@ -230,7 +223,7 @@ function startApp() {
selectMusicQuality(); selectMusicQuality();
}).catch((err) => { }).catch((err) => {
if ('Wrong Deezer credentials!' === err) { if ('Wrong Deezer credentials!' === err) {
downloadSpinner.fail('Wrong Deezer credentials!\n'); downloadSpinner.fail('Wrong Deezer credentials!');
configService.set('arl', null); configService.set('arl', null);
configService.saveConfig(); configService.saveConfig();
@ -264,7 +257,6 @@ function ensureDir(filePath) {
*/ */
function initDeezerApi() { function initDeezerApi() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
log.debug('Init Deezer API');
requestWithoutCacheAndRetry({ requestWithoutCacheAndRetry({
method: 'POST', method: 'POST',
@ -294,7 +286,6 @@ function initDeezerApi() {
throw 'Unable to initialize Deezer API.'; throw 'Unable to initialize Deezer API.';
} else { } else {
if (response.results && response.results.checkForm) { if (response.results && response.results.checkForm) {
log.debug('Successfully initiated Deezer API. Checkform: "' + response.results.checkForm + '"');
unofficialApiQueries.api_token = response.results.checkForm; unofficialApiQueries.api_token = response.results.checkForm;
@ -400,6 +391,7 @@ function selectMusicQuality() {
break; break;
} }
DOWNLOAD_DIR = nodePath.normalize(cliPath).replace(/\/$|\\$/, '');
DOWNLOAD_MODE = cliDownloadMode; DOWNLOAD_MODE = cliDownloadMode;
downloadSpinner.warn(chalk.yellow('Do not scroll while downloading! This will mess up the UI!')); downloadSpinner.warn(chalk.yellow('Do not scroll while downloading! This will mess up the UI!'));
@ -783,8 +775,6 @@ let downloadStateInstance = new downloadState();
* @param {Boolean} downloadFromFile * @param {Boolean} downloadFromFile
*/ */
function startDownload(deezerUrl, downloadFromFile = false) { function startDownload(deezerUrl, downloadFromFile = false) {
log.debug('------------------------------------------');
log.debug('Started download task: "' + deezerUrl + '"');
const deezerUrlParts = getDeezerUrlParts(deezerUrl); const deezerUrlParts = getDeezerUrlParts(deezerUrl);
@ -868,7 +858,6 @@ function downloadArtist(id) {
throw 'Could not fetch the artist!'; throw 'Could not fetch the artist!';
} }
} else { } else {
log.debug('Got artist infos for "artist/' + id + '"');
const artistName = response.results.ART_NAME; const artistName = response.results.ART_NAME;
downloadStateInstance.setDownloadTypeName(artistName); downloadStateInstance.setDownloadTypeName(artistName);
@ -900,7 +889,6 @@ function downloadArtist(id) {
throw 'Could not fetch "' + artistName + '" albums!'; throw 'Could not fetch "' + artistName + '" albums!';
} }
} else { } else {
log.debug('Got all albums for "artist/' + id + '"');
if (0 < response.results.data.length) { if (0 < response.results.data.length) {
let trackList = []; let trackList = [];
@ -1012,7 +1000,6 @@ function downloadMultiple(type, id) {
throw 'Could not fetch the ' + type + '!'; throw 'Could not fetch the ' + type + '!';
} }
} else { } else {
log.debug('Got track list for "' + type + '/' + id + '"');
let trackList = []; let trackList = [];
let albumList = {}; let albumList = {};
@ -1116,93 +1103,6 @@ function getNumberOfParallelDownloads() {
return numberOfParallel; return numberOfParallel;
} }
function getPathForTrackInfos(trackInfos, albumInfos) {
let artistName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_ART_NAME));
if ('' === artistName.trim()) {
artistName = 'Unknown artist';
}
let albumType = 'Album';
if (albumInfos.TYPE) {
albumType = albumInfos.TYPE.toLowerCase();
if ('ep' === albumType) {
albumType = 'EP';
} else {
albumType = capitalizeFirstLetter(albumType);
}
}
let albumName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_TITLE));
if ('' === albumName.trim()) {
albumName = 'Unknown album';
}
let variableData = {
"title": multipleWhitespacesToSingle(sanitizeFilename(trackInfos.SNG_TITLE_VERSION)),
"artist": artistName,
"album": albumName,
"type": albumType,
'disc': toTwoDigits(trackInfos.DISK_NUMBER)
};
if (trackInfos.TRACK_NUMBER) {
variableData['number'] = toTwoDigits(trackInfos.TRACK_NUMBER);
}
let dirPath;
if (trackInfos.ALB_NUM_DISCS > 1) {
dirPath = namingService.getDiscPath(variableData);
} else {
dirPath = namingService.getPath(variableData);
}
return dirPath
}
function getFileNameForTrackInfos(trackInfos, albumInfos) {
let artistName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_ART_NAME));
if ('' === artistName.trim()) {
artistName = 'Unknown artist';
}
let albumType = 'Album';
if (albumInfos.TYPE) {
albumType = albumInfos.TYPE.toLowerCase();
if ('ep' === albumType) {
albumType = 'EP';
} else {
albumType = capitalizeFirstLetter(albumType);
}
}
let albumName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_TITLE));
if ('' === albumName.trim()) {
albumName = 'Unknown album';
}
let variableData = {
"title": multipleWhitespacesToSingle(sanitizeFilename(trackInfos.SNG_TITLE_VERSION)),
"artist": artistName,
"album": albumName,
"type": albumType,
'disc': toTwoDigits(trackInfos.DISK_NUMBER)
};
if (trackInfos.TRACK_NUMBER) {
variableData['number'] = toTwoDigits(trackInfos.TRACK_NUMBER);
}
return namingService.getFileName(variableData);
}
/** /**
* Map through a track list and download it. * Map through a track list and download it.
* *
@ -1219,27 +1119,53 @@ function trackListDownload(trackList, albumInfos = {}) {
trackAlbumInfos = albumInfos[trackInfos.ALB_ID]; trackAlbumInfos = albumInfos[trackInfos.ALB_ID];
} }
trackInfos.ALB_ART_NAME = trackInfos.ART_NAME;
if (albumInfos.ART_NAME) {
trackInfos.ALB_ART_NAME = albumInfos.ART_NAME;
}
trackInfos.SNG_TITLE_VERSION = trackInfos.SNG_TITLE; trackInfos.SNG_TITLE_VERSION = trackInfos.SNG_TITLE;
if (trackInfos.VERSION) { if (trackInfos.VERSION) {
trackInfos.SNG_TITLE_VERSION = (trackInfos.SNG_TITLE + ' ' + trackInfos.VERSION).trim(); trackInfos.SNG_TITLE_VERSION = (trackInfos.SNG_TITLE + ' ' + trackInfos.VERSION).trim();
} }
let artistName = trackInfos.ART_NAME;
if (trackAlbumInfos && '' !== trackAlbumInfos.ART_NAME) {
artistName = trackAlbumInfos.ART_NAME;
}
artistName = multipleWhitespacesToSingle(sanitizeFilename(artistName));
if ('' === artistName.trim()) {
artistName = 'Unknown artist';
}
if ('various' === artistName.trim().toLowerCase()) {
artistName = 'Various Artists';
}
let albumName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_TITLE));
if ('' === albumName.trim()) {
albumName = 'Unknown album';
}
albumName += ' (Album)';
let saveFileDir = nodePath.join(DOWNLOAD_DIR, artistName, albumName);
if (trackAlbumInfos && trackAlbumInfos.SONGS && trackAlbumInfos.SONGS.data && 0 < trackAlbumInfos.SONGS.data.length && '' !== trackAlbumInfos.SONGS.data[trackAlbumInfos.SONGS.data.length - 1].DISK_NUMBER) {
const albumNumberOfDisks = trackAlbumInfos.SONGS.data[trackAlbumInfos.SONGS.data.length - 1].DISK_NUMBER;
if (albumNumberOfDisks > 1) {
saveFileDir += nodePath.join(saveFileDir, 'Disc ' + toTwoDigits(trackInfos.DISK_NUMBER));
}
}
let saveFileName = multipleWhitespacesToSingle(sanitizeFilename(toTwoDigits(trackInfos.TRACK_NUMBER) + ' ' + trackInfos.SNG_TITLE_VERSION));
let fileExtension = 'mp3'; let fileExtension = 'mp3';
if (musicQualities.FLAC.id === selectedMusicQuality.id) { if (musicQualities.FLAC.id === selectedMusicQuality.id) {
fileExtension = 'flac'; fileExtension = 'flac';
} }
let saveFileName = getFileNameForTrackInfos(trackInfos, albumInfos) + '.' + fileExtension;
let saveFileDir = getPathForTrackInfos(trackInfos, albumInfos) + "/" + saveFileName;
let artistName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_ART_NAME));
const downloadingMessage = artistName + ' - ' + trackInfos.SNG_TITLE_VERSION; const downloadingMessage = artistName + ' - ' + trackInfos.SNG_TITLE_VERSION;
downloadStateInstance.add(trackInfos.SNG_ID, downloadingMessage); downloadStateInstance.add(trackInfos.SNG_ID, downloadingMessage);
@ -1259,20 +1185,17 @@ function trackListDownload(trackList, albumInfos = {}) {
* @param {Number} numberRetry * @param {Number} numberRetry
*/ */
function downloadSingleTrack(id, trackInfos = {}, albumInfos = {}, isAlternativeTrack = false, numberRetry = 0) { function downloadSingleTrack(id, trackInfos = {}, albumInfos = {}, isAlternativeTrack = false, numberRetry = 0) {
let dirPath;
let saveFilePath; let saveFilePath;
let originalTrackInfos; let originalTrackInfos;
let fileExtension = 'mp3'; let fileExtension = 'mp3';
let trackQuality; let trackQuality;
log.debug('Start downloading "track/' + id + '"');
return new Promise((resolve) => { return new Promise((resolve) => {
if ('-' === id.toString().charAt(0) && 0 < Object.keys(trackInfos).length) { if ('-' === id.toString().charAt(0) && 0 < Object.keys(trackInfos).length) {
getTrackAlternative(trackInfos).then((alternativeTrackInfos) => { getTrackAlternative(trackInfos).then((alternativeTrackInfos) => {
downloadStateInstance.remove(id); downloadStateInstance.remove(id);
log.debug('Using alternative "track/' + alternativeTrackInfos.SNG_ID + '" for "track/' + trackInfos.SNG_ID + '"');
downloadSingleTrack(alternativeTrackInfos.SNG_ID, {}, {}, true).then(() => { downloadSingleTrack(alternativeTrackInfos.SNG_ID, {}, {}, true).then(() => {
resolve(); resolve();
}); });
@ -1414,70 +1337,104 @@ function downloadSingleTrack(id, trackInfos = {}, albumInfos = {}, isAlternative
trackInfos = originalTrackInfos; trackInfos = originalTrackInfos;
} }
if (!trackQuality) { if (trackQuality) {
return errorHandling(trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + '\n Deezer doesn\'t provide the song anymore'); let artistName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_ART_NAME));
}
if (musicQualities.FLAC.id === trackQuality.id) { if ('' === artistName.trim()) {
fileExtension = 'flac'; artistName = 'Unknown artist';
} }
saveFilePath = getPathForTrackInfos(trackInfos, albumInfos) + "/" + getFileNameForTrackInfos(trackInfos, albumInfos) + '.' + fileExtension; let albumType = 'Album';
if (!fs.existsSync(saveFilePath) && !downloadStateInstance.isCurrentlyDownloadingPathUsed(saveFilePath)) { if (albumInfos.TYPE) {
downloadStateInstance.addCurrentlyDownloadingPath(saveFilePath); albumType = albumInfos.TYPE.toLowerCase();
return downloadTrack(originalTrackInfos, trackQuality.id, saveFilePath).then((decryptedTrackBuffer) => { if ('ep' === albumType) {
onTrackDownloadComplete(decryptedTrackBuffer); albumType = 'EP';
}).catch((error) => {
log.debug('Failed downloading "track/' + trackInfos.SNG_ID + '". Error: "' + error + '"');
if (originalTrackInfos.FALLBACK && originalTrackInfos.FALLBACK.SNG_ID && trackInfos.SNG_ID !== originalTrackInfos.FALLBACK.SNG_ID && originalTrackInfos.SNG_ID !== originalTrackInfos.FALLBACK.SNG_ID) {
downloadStateInstance.removeCurrentlyDownloadingPath(saveFilePath);
downloadStateInstance.remove(originalTrackInfos.SNG_ID);
log.debug('Using alternative "track/' + originalTrackInfos.FALLBACK.SNG_ID + '" for "track/' + trackInfos.SNG_ID + '"');
downloadSingleTrack(originalTrackInfos.FALLBACK.SNG_ID, trackInfos, albumInfos, true).then(() => {
resolve();
});
const error = {
message: '-',
name: 'notAvailableButAlternative'
};
errorHandling(error);
} else { } else {
getTrackAlternative(trackInfos).then((alternativeTrackInfos) => { albumType = capitalizeFirstLetter(albumType);
}
}
let albumName = multipleWhitespacesToSingle(sanitizeFilename(trackInfos.ALB_TITLE));
if ('' === albumName.trim()) {
albumName = 'Unknown album';
}
albumName += ' (' + albumType + ')';
if (trackInfos.ALB_NUM_DISCS > 1) {
dirPath = nodePath.join(DOWNLOAD_DIR, artistName, albumName, 'Disc ' + toTwoDigits(trackInfos.DISK_NUMBER));
} else {
dirPath = nodePath.join(DOWNLOAD_DIR, artistName, albumName);
}
if (musicQualities.FLAC.id === trackQuality.id) {
fileExtension = 'flac';
}
saveFilePath = dirPath + nodePath.sep;
if (trackInfos.TRACK_NUMBER) {
saveFilePath += toTwoDigits(trackInfos.TRACK_NUMBER) + ' ';
}
saveFilePath += multipleWhitespacesToSingle(sanitizeFilename(trackInfos.SNG_TITLE_VERSION));
saveFilePath += '.' + fileExtension;
if (!fs.existsSync(saveFilePath) && !downloadStateInstance.isCurrentlyDownloadingPathUsed(saveFilePath)) {
downloadStateInstance.addCurrentlyDownloadingPath(saveFilePath);
return downloadTrack(originalTrackInfos, trackQuality.id, saveFilePath).then((decryptedTrackBuffer) => {
onTrackDownloadComplete(decryptedTrackBuffer);
}).catch((error) => {
if (originalTrackInfos.FALLBACK && originalTrackInfos.FALLBACK.SNG_ID && trackInfos.SNG_ID !== originalTrackInfos.FALLBACK.SNG_ID && originalTrackInfos.SNG_ID !== originalTrackInfos.FALLBACK.SNG_ID) {
downloadStateInstance.removeCurrentlyDownloadingPath(saveFilePath); downloadStateInstance.removeCurrentlyDownloadingPath(saveFilePath);
downloadStateInstance.remove(originalTrackInfos.SNG_ID); downloadStateInstance.remove(originalTrackInfos.SNG_ID);
log.debug('Using alternative "track/' + alternativeTrackInfos.SNG_ID + '" for "track/' + trackInfos.SNG_ID + '"'); downloadSingleTrack(originalTrackInfos.FALLBACK.SNG_ID, trackInfos, albumInfos, true).then(() => {
if (albumInfos.ALB_TITLE) {
albumInfos = {};
}
downloadSingleTrack(alternativeTrackInfos.SNG_ID, trackInfos, albumInfos, true).then(() => {
resolve(); resolve();
}); });
}).catch(() => {
const errorMessage = trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + '\n Deezer doesn\'t provide the song anymore';
errorHandling(errorMessage); const error = {
}); message: '-',
} name: 'notAvailableButAlternative'
}); };
errorHandling(error);
} else {
getTrackAlternative(trackInfos).then((alternativeTrackInfos) => {
downloadStateInstance.removeCurrentlyDownloadingPath(saveFilePath);
downloadStateInstance.remove(originalTrackInfos.SNG_ID);
if (albumInfos.ALB_TITLE) {
albumInfos = {};
}
downloadSingleTrack(alternativeTrackInfos.SNG_ID, trackInfos, albumInfos, true).then(() => {
resolve();
});
}).catch(() => {
const errorMessage = trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + '\n Deezer doesn\'t provide the song anymore';
errorHandling(errorMessage);
});
}
});
} else {
addTrackToPlaylist(saveFilePath, trackInfos);
const error = {
message: trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + ' \n Song already exists',
name: 'songAlreadyExists'
};
errorHandling(error);
}
} else { } else {
addTrackToPlaylist(saveFilePath, trackInfos); errorHandling(trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + '\n Deezer doesn\'t provide the song anymore');
const error = {
message: trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + ' \n Song already exists',
name: 'songAlreadyExists'
};
errorHandling(error);
} }
} }
@ -1559,7 +1516,6 @@ function getTrackInfos(id) {
json: true, json: true,
jar: true jar: true
}).then((response) => { }).then((response) => {
log.debug('Got track infos for "track/' + id + '"');
if (response && 0 === Object.keys(response.error).length && response.results && response.results.DATA) { if (response && 0 === Object.keys(response.error).length && response.results && response.results.DATA) {
let trackInfos = response.results.DATA; let trackInfos = response.results.DATA;
@ -1611,7 +1567,7 @@ function getTrackAlternative(trackInfos) {
json: true, json: true,
jar: true jar: true
}).then((response) => { }).then((response) => {
log.debug('Got alternative track for "track/' + trackInfos.SNG_ID + '"');
if (response && 0 === Object.keys(response.error).length && response.results && response.results.data && 0 > response.results.data.length) { if (response && 0 === Object.keys(response.error).length && response.results && response.results.data && 0 > response.results.data.length) {
const foundTracks = response.results.data; const foundTracks = response.results.data;
let matchingTracks = []; let matchingTracks = [];
@ -1706,7 +1662,6 @@ function getAlbumInfos(id) {
json: true, json: true,
jar: true jar: true
}).then((response) => { }).then((response) => {
log.debug('Got album infos for "album/' + id + '"');
if (response && 0 === Object.keys(response.error).length && response.results && response.results.DATA && response.results.SONGS) { if (response && 0 === Object.keys(response.error).length && response.results && response.results.DATA && response.results.SONGS) {
let albumInfos = response.results.DATA; let albumInfos = response.results.DATA;
@ -1743,7 +1698,6 @@ function getAlbumInfosOfficialApi(id) {
url: 'https://api.deezer.com/album/' + id, url: 'https://api.deezer.com/album/' + id,
json: true json: true
}).then((albumInfos) => { }).then((albumInfos) => {
log.debug('Got album infos (official api) for "album/' + id + '"');
if (albumInfos && !albumInfos.error) { if (albumInfos && !albumInfos.error) {
resolve(albumInfos); resolve(albumInfos);
@ -1776,7 +1730,6 @@ function getTrackLyrics(id) {
json: true, json: true,
jar: true jar: true
}).then((response) => { }).then((response) => {
log.debug('Got lyrics for "track/' + id + '"');
if (response && 0 === Object.keys(response.error).length && response.results && response.results.LYRICS_ID) { if (response && 0 === Object.keys(response.error).length && response.results && response.results.LYRICS_ID) {
let trackLyrics = response.results; let trackLyrics = response.results;
@ -1966,15 +1919,12 @@ function downloadTrack(trackInfos, trackQualityId, saveFilePath, numberRetry = 0
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const trackDownloadUrl = getTrackDownloadUrl(trackInfos, trackQualityId); const trackDownloadUrl = getTrackDownloadUrl(trackInfos, trackQualityId);
log.debug('Started downloading "track/' + trackInfos.SNG_ID + '" in "' + trackQualityId + '". Download url: "' + trackDownloadUrl + '"');
requestWithoutCache({ requestWithoutCache({
url: trackDownloadUrl, url: trackDownloadUrl,
headers: httpHeaders, headers: httpHeaders,
jar: true, jar: true,
encoding: null encoding: null
}).then((response) => { }).then((response) => {
log.debug('Got download response for "track/' + trackInfos.SNG_ID + '"');
const decryptedTrackBuffer = encryptionService.decryptTrack(response, trackInfos); const decryptedTrackBuffer = encryptionService.decryptTrack(response, trackInfos);
@ -2025,7 +1975,6 @@ function downloadAlbumCover(trackInfos, saveFilePath, numberRetry = 0) {
reject(); reject();
} else { } else {
if (!fs.existsSync(albumCoverSavePath)) { if (!fs.existsSync(albumCoverSavePath)) {
log.debug('Started downloading album cover for "track/' + trackInfos.SNG_ID + '". Album cover url: "' + albumCoverUrl + '"');
requestWithoutCache({ requestWithoutCache({
url: albumCoverUrl, url: albumCoverUrl,
@ -2033,17 +1982,12 @@ function downloadAlbumCover(trackInfos, saveFilePath, numberRetry = 0) {
jar: true, jar: true,
encoding: null encoding: null
}).then((response) => { }).then((response) => {
log.debug('Got album cover download response for "track/' + trackInfos.SNG_ID + '"');
ensureDir(albumCoverSavePath); ensureDir(albumCoverSavePath);
fs.writeFile(albumCoverSavePath, response, (err) => { fs.writeFile(albumCoverSavePath, response, (err) => {
if (err) { if (err) {
log.debug('Error downloading album cover for "track/' + trackInfos.SNG_ID + '"');
log.debug(err);
reject(); reject();
} else { } else {
log.debug('Finished downloading album cover for "track/' + trackInfos.SNG_ID + '"');
resolve(albumCoverSavePath); resolve(albumCoverSavePath);
} }
}); });
@ -2067,8 +2011,6 @@ function downloadAlbumCover(trackInfos, saveFilePath, numberRetry = 0) {
} }
}); });
} else { } else {
log.debug('Album cover for "track/' + trackInfos.SNG_ID + '" already exists');
resolve(albumCoverSavePath); resolve(albumCoverSavePath);
} }
} }
@ -2085,10 +2027,8 @@ function downloadAlbumCover(trackInfos, saveFilePath, numberRetry = 0) {
*/ */
function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetry = 0) { function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetry = 0) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
log.debug('Started tagging "track/' + trackInfos.SNG_ID + '"');
downloadAlbumCover(trackInfos, saveFilePath).then((albumCoverSavePath) => { downloadAlbumCover(trackInfos, saveFilePath).then((albumCoverSavePath) => {
log.debug('Got album cover and started tagging "track/' + trackInfos.SNG_ID + '"');
startTagging(albumCoverSavePath); startTagging(albumCoverSavePath);
}).catch(() => { }).catch(() => {
@ -2310,8 +2250,6 @@ function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetr
fs.writeFileSync(lyricsFile, trackMetadata.synchronisedLyrics); fs.writeFileSync(lyricsFile, trackMetadata.synchronisedLyrics);
} }
log.debug('Started MP3 tagging "track/' + trackInfos.SNG_ID + '"');
const writer = new id3Writer(decryptedTrackBuffer); const writer = new id3Writer(decryptedTrackBuffer);
let coverBuffer; let coverBuffer;
@ -2414,8 +2352,6 @@ function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetr
ensureDir(saveFilePath); ensureDir(saveFilePath);
fs.writeFileSync(saveFilePath, taggedTrackBuffer); fs.writeFileSync(saveFilePath, taggedTrackBuffer);
log.debug('Finished MP3 tagging "track/' + trackInfos.SNG_ID + '"');
resolve(); resolve();
} else if ('.flac' === saveFilePathExtension) { } else if ('.flac' === saveFilePathExtension) {
if ('' !== trackMetadata.synchronisedLyrics.trim()) { if ('' !== trackMetadata.synchronisedLyrics.trim()) {
@ -2425,8 +2361,6 @@ function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetr
fs.writeFileSync(lyricsFile, trackMetadata.synchronisedLyrics); fs.writeFileSync(lyricsFile, trackMetadata.synchronisedLyrics);
} }
log.debug('Started FLAC tagging "track/' + trackInfos.SNG_ID + '"');
let flacComments = [ let flacComments = [
'SOURCE=Deezer', 'SOURCE=Deezer',
'SOURCEID=' + trackInfos.SNG_ID 'SOURCEID=' + trackInfos.SNG_ID
@ -2605,8 +2539,6 @@ function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetr
}); });
reader.on('end', () => { reader.on('end', () => {
log.debug('Finished FLAC tagging "track/' + trackInfos.SNG_ID + '"');
resolve(); resolve();
}); });
@ -2614,7 +2546,6 @@ function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetr
} }
} }
} catch (err) { } catch (err) {
log.debug('Error tagging "track/' + trackInfos.SNG_ID + '". Number retries: "' + numberRetry + '". Error: ' + err);
if (10 > numberRetry) { if (10 > numberRetry) {
numberRetry += 1; numberRetry += 1;
@ -2635,4 +2566,4 @@ function addTrackTags(decryptedTrackBuffer, trackInfos, saveFilePath, numberRetr
} }
} }
}); });
} }