Add NamingService

This commit is contained in:
magicalunicorn 2019-09-15 16:22:27 +02:00
parent f062ed554a
commit 6e620ae599
3 changed files with 410 additions and 320 deletions

View File

@ -28,7 +28,6 @@ const nodePath = require('path');
const memoryStats = require('./libs/node-memory-stats');
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
const nodeJsonFile = require('jsonfile');
const openUrl = require('openurl');
const packageJson = require('./package.json');
@ -39,9 +38,11 @@ let configService = new ConfigService(configFile);
const EncryptionService = require('./src/service/EncryptionService');
let encryptionService = new EncryptionService();
const NamingService = require('./src/service/NamingService');
let namingService = new NamingService(configService);
const Log = require('log');
let DOWNLOAD_DIR = 'DOWNLOADS/';
let PLAYLIST_DIR = 'PLAYLISTS/';
let PLAYLIST_FILE_ITEMS = {};
@ -91,13 +92,6 @@ const cliOptionDefinitions = [
defaultValue: 'MP3_320',
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',
alias: 'u',
@ -229,7 +223,6 @@ function initRequest() {
fs.writeFileSync(DOWNLOAD_LINKS_FILE, '');
}
nodePath.normalize(DOWNLOAD_DIR).replace(/\/$|\\$/, '');
nodePath.normalize(PLAYLIST_DIR).replace(/\/$|\\$/, '');
if (isCli) {
@ -500,7 +493,6 @@ function selectMusicQuality() {
break;
}
DOWNLOAD_DIR = nodePath.normalize(cliPath).replace(/\/$|\\$/, '');
DOWNLOAD_MODE = cliDownloadMode;
downloadSpinner.warn(chalk.yellow('Do not scroll while downloading! This will mess up the UI!'));
@ -1217,6 +1209,93 @@ function getNumberOfParallelDownloads() {
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.
*
@ -1233,58 +1312,32 @@ function trackListDownload(trackList, albumInfos = {}) {
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;
if (trackInfos.VERSION) {
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';
if (musicQualities.FLAC.id === selectedMusicQuality.id) {
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;
downloadStateInstance.add(trackInfos.SNG_ID, downloadingMessage);
if (fs.existsSync(saveFileDir)) {
let files = Finder.from(saveFileDir).findFiles(saveFileName + '.' + fileExtension);
let files = Finder.from(saveFileDir).findFiles(saveFileName);
if (0 < files.length) {
addTrackToPlaylist(files[0], trackInfos);
@ -1312,7 +1365,6 @@ function trackListDownload(trackList, albumInfos = {}) {
* @param {Number} numberRetry
*/
function downloadSingleTrack(id, trackInfos = {}, albumInfos = {}, isAlternativeTrack = false, numberRetry = 0) {
let dirPath;
let saveFilePath;
let originalTrackInfos;
let fileExtension = 'mp3';
@ -1468,51 +1520,15 @@ function downloadSingleTrack(id, trackInfos = {}, albumInfos = {}, isAlternative
trackInfos = originalTrackInfos;
}
if (trackQuality) {
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';
}
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 (!trackQuality) {
return errorHandling(trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + '\n Deezer doesn\'t provide the song anymore');
}
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;
saveFilePath = getPathForTrackInfos(trackInfos, albumInfos) + "/" + getFileNameForTrackInfos(trackInfos, albumInfos) + '.' + fileExtension;
if (!fs.existsSync(saveFilePath) && !downloadStateInstance.isCurrentlyDownloadingPathUsed(saveFilePath)) {
downloadStateInstance.addCurrentlyDownloadingPath(saveFilePath);
@ -1569,9 +1585,6 @@ function downloadSingleTrack(id, trackInfos = {}, albumInfos = {}, isAlternative
errorHandling(error);
}
} else {
errorHandling(trackInfos.ALB_ART_NAME + ' - ' + trackInfos.SNG_TITLE_VERSION + '\n Deezer doesn\'t provide the song anymore');
}
}
function onTrackDownloadComplete(decryptedTrackBuffer) {

View File

@ -6,8 +6,13 @@ module.exports = class ConfigService {
this.configFile = configFile;
this.config = {
saveLayout: "",
arl: ""
"naming": {
"path": "DOWNLOADS/%artist%/%albumName%",
"discPath": "%path%/Disc %disc%",
"albumName": "%album% (%type%)",
"fileName": "%number% %title%"
},
"arl": ""
};
this.loadConfig();

View File

@ -0,0 +1,72 @@
module.exports = class NamingService {
configService;
/**
* @param {ConfigService} configService
*/
constructor(configService) {
this.configService = configService;
}
getConfig(type) {
return this.configService.get('naming')[type];
}
/**
* @param variables
* @returns {string}
*/
getPath(variables) {
let name = this.getConfig('path');
variables['albumName'] = this.getAlbumName(variables);
for (let variableKey in variables) {
name = name.replace('%' + variableKey + '%', variables[variableKey])
}
return name
}
/**
* @param variables
* @returns {string}
*/
getDiscPath(variables) {
let name = this.getConfig('discPath');
variables['path'] = this.getPath(variables);
for (let variableKey in variables) {
name = name.replace('%' + variableKey + '%', variables[variableKey])
}
return name
}
/**
* @param variables
* @returns {string}
*/
getAlbumName(variables) {
let name = this.getConfig('albumName');
for (let variableKey in variables) {
name = name.replace('%' + variableKey + '%', variables[variableKey])
}
return name
}
/**
* @param variables
* @returns {string}
*/
getFileName(variables) {
let name = this.getConfig('fileName');
for (let variableKey in variables) {
name = name.replace('%' + variableKey + '%', variables[variableKey])
}
return name
}
};