From 31c1fb8a11f93df8e1aa373667432a36e1e9baf5 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Thu, 21 Oct 2021 19:27:00 +0300 Subject: [PATCH] create config --- .gitignore | 3 ++- README.md | 2 +- config.example.toml | 36 ++++++++++++++++++++++++++++++++++++ config.toml | 33 +++++++++++++++++++++++++++++++++ package-lock.json | 11 +++++++++++ package.json | 1 + src/index.js | 44 +++++++++++++++++++++++++++++++++----------- 7 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 config.example.toml create mode 100644 config.toml diff --git a/.gitignore b/.gitignore index b229ec8..2cda530 100644 --- a/.gitignore +++ b/.gitignore @@ -144,4 +144,5 @@ dist # .nfs files are created when an open file is removed but is still being accessed .nfs* -data/** \ No newline at end of file +data/** +config.json \ No newline at end of file diff --git a/README.md b/README.md index 190bfd3..71ab65e 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ it's intended use is for small groups of people to self-host, and as such there' your arl can be found in the local storage for deezer, as a value conviniently named `arl` -2. (optionally) set a `PORT` value in your `.env` file. (if unset, this will fall back to 4500) +2. (optionally) copy the config.example.json to config.json in the same folder, and modify it 3. `npm install` diff --git a/config.example.toml b/config.example.toml new file mode 100644 index 0000000..53e8ad1 --- /dev/null +++ b/config.example.toml @@ -0,0 +1,36 @@ +[timer] +# the time before a downloaded file is cleaned off the data folder +deleteTimer = 1500000 # 25 minutes + +[server] +port = 4500 +# location of the zip utility binary +zipBinaryLocation = "zip" +zipArguments = "-0rD" + +[limits] +# the max amount of search results to send to the client +searchLimit = 15 + +[deemix] +# the format of the song to download +# can be "FLAC", "MP3_320", "MP3_128" or "DEFAULT" +trackFormat = "FLAC" +# templates for the folder and file names +trackNameTemplate = "%artist% - %title%" +albumTrackNameTemplate = "%tracknumber%. %artist% - %title%" +albumNameTemplate = "%artist% - %album%" +# create a m3u8 playlist file or not +createM3U8File = false +# cover art settings +# embed cover art in the music files themselves +embeddedArtworkPNG = true +embeddedArtworkSize = 800 +# put the cover art file inside of the folder +saveArtwork = true +localArtworkSize = 1200 +localArtworkFormat = "jpg" + +jpegImageQuality = 80 + +removeDuplicateArtists = true \ No newline at end of file diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..91eddb9 --- /dev/null +++ b/config.toml @@ -0,0 +1,33 @@ +[timer] +# the time before a downloaded file is cleaned off the data folder +deleteTimer = 1500000 # 25 minutes + +[server] +port = 4500 + +[limits] +# the max amount of search results to send to the client +searchLimit = 15 + +[deemix] +# the format of the song to download +# can be "FLAC", "MP3_320", "MP3_128" or "DEFAULT" +trackFormat = "FLAC" +# templates for the folder and file names +trackNameTemplate = "%artist% - %title%" +albumTrackNameTemplate = "%tracknumber%. %artist% - %title%" +albumNameTemplate = "%artist% - %album%" +# create a m3u8 playlist file or not +createM3U8File = false +# cover art settings +# embed cover art in the music files themselves +embeddedArtworkPNG = true +embeddedArtworkSize = 800 +# put the cover art file inside of the folder +saveArtwork = true +localArtworkSize = 1200 +localArtworkFormat = "jpg" + +jpegImageQuality = 80 + +removeDuplicateArtists = true \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 391a09c..c4f847d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "express": "^4.17.1", "express-ws": "^5.0.2", "timeago.js": "^4.0.2", + "toml": "^3.0.0", "ws": "^8.2.3" }, "optionalDependencies": { @@ -1275,6 +1276,11 @@ "node": ">=0.6" } }, + "node_modules/toml": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", + "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==" + }, "node_modules/tough-cookie": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.0.0.tgz", @@ -2358,6 +2364,11 @@ "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" }, + "toml": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", + "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==" + }, "tough-cookie": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.0.0.tgz", diff --git a/package.json b/package.json index a07a9c3..800983a 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "express": "^4.17.1", "express-ws": "^5.0.2", "timeago.js": "^4.0.2", + "toml": "^3.0.0", "ws": "^8.2.3" }, "optionalDependencies": { diff --git a/src/index.js b/src/index.js index 446ae5d..f3d2d2a 100644 --- a/src/index.js +++ b/src/index.js @@ -2,15 +2,25 @@ const express = require('express'); const deezer = require('deezer-js'); const deemix = require('deemix'); const path = require('path'); -const { inspect, promisify } = require('util'); +const { promisify } = require('util'); const fs = require('fs'); const { exec } = require('child_process'); const timeago = require('timeago.js'); +const toml = require('toml'); -const port = process.env.PORT || 4500; -// const deleteTimer = 1000 * 60 * 60; // 1 hour -const deleteTimer = 1000 * 60 * 25; // 25 minutes -// const deleteTimer = 16000; +if (!fs.existsSync('./config.toml')) { + if (!fs.existsSync('./config.example.toml')) { + console.error('!! no config.toml OR config.example.toml found!!! what the hell are you up to!!!'); + process.exit(1); + } + console.log('! copying config.example.toml to config.toml as it was not found. the default config may not be preferable!'); + fs.copyFileSync('./config.example.toml', './config.toml'); +} +const config = toml.parse(fs.readFileSync('./config.toml')); +console.log('loaded config'); + +const port = config.server.port || 4500; +const deleteTimer = config.timer.deleteTimer || 1000 * 60 * 25; require('dotenv').config(); @@ -21,8 +31,20 @@ let deemixDownloader; let deemixSettings = deemix.settings.DEFAULTS deemixSettings.downloadLocation = path.join(process.cwd(), 'data/'); -deemixSettings.maxBitrate = String(deezer.TrackFormats.FLAC); -deemixSettings.overwriteFile = deemix.settings.OverwriteOption.OVERWRITE; +deemixSettings.overwriteFile = deemix.settings.OverwriteOption.ONLY_TAGS; + +deemixSettings.maxBitrate = String(deezer.TrackFormats[config.deemix.trackFormat]); +deemixSettings.tracknameTemplate = config.deemix.trackNameTemplate || '%artist% - %title%'; +deemixSettings.albumTracknameTemplate = config.deemix.albumTrackNameTemplate || '%tracknumber%. %artist% - %title%'; +deemixSettings.albumNameTemplate = config.deemix.albumNameTemplate || '%artist% - %album%' +deemixSettings.createM3U8File = config.deemix.createM3U8File !== undefined ? config.deemix.createM3U8File : false; +deemixSettings.embeddedArtworkPNG = config.deemix.embeddedArtworkPNG !== undefined ? config.deemix.embeddedArtworkPNG : true; +deemixSettings.embeddedArtworkSize = config.deemix.embeddedArtworkSize || 800; +deemixSettings.saveArtwork = config.deemix.saveArtwork !== undefined ? config.deemix.saveArtwork : true; +deemixSettings.localArtworkSize = deemixSettings.localArtworkSize || 1200; +deemixSettings.localArtworkFormat = deemixSettings.localArtworkFormat || 'jpg'; +deemixSettings.jpegImageQuality = deemixSettings.jpegImageQuality || 80; +deemixSettings.removeDuplicateArtists = config.deemix.removeDuplicateArtists !== undefined ? config.deemix.removeDuplicateArtists : true; const toDeleteLocation = './data/toDelete.json'; @@ -90,7 +112,7 @@ app.get('/api/search', async (req, res) => { if (!req.query.search) return res.sendStatus(400); let s = await deezerInstance.api.search_album(req.query.search, { - limit: 15, + limit: config.limits.searchLimit || 15, }); let format = s.data.map(s => { @@ -170,13 +192,13 @@ app.ws('/api/album', async (ws, req) => { const folderName = trackpaths[0].split('/').slice(-2)[0]; try { - await promisify(exec)(`zip -0rD "data/${folderName}.zip" "data/${folderName}"`); + await promisify(exec)(`${config.server.zipBinaryLocation} ${config.server.zipArguments} "data/${folderName}.zip" "data/${folderName}"`); } catch(err) { return ws.close(1011, 'Zipping album failed'); } - + await ws.send(JSON.stringify({key: 'download', data: `data/${folderName}.zip`})); - + queueDeletion('./data/' + folderName + '.zip'); } else { await ws.send(JSON.stringify({key: 'download', data: trackpaths[0].replace(process.cwd(), '')}));