use centralized config

This commit is contained in:
Araxeus
2023-03-04 15:48:15 +02:00
parent 7b3280c12b
commit ad484ab745
4 changed files with 46 additions and 29 deletions

View File

@ -25,7 +25,7 @@ const ffmpegMutex = new Mutex();
/** @type {Innertube} */
let yt;
let options;
const config = require("./config");
let win;
let playingUrl = undefined;
@ -45,9 +45,9 @@ const sendError = (error) => {
});
};
module.exports = async (win_, options_) => {
options = options_;
module.exports = async (win_, options) => {
win = win_;
config.init(options);
injectCSS(win.webContents, join(__dirname, "style.css"));
yt = await Innertube.create({ cache: new UniversalCache(false), generate_session_locally: true });
@ -55,7 +55,7 @@ module.exports = async (win_, options_) => {
ipcMain.on("video-src-changed", async (_, data) => {
playingUrl = JSON.parse(data)?.microformat?.microformatDataRenderer?.urlCanonical;
});
ipcMain.on("download-playlist-request", async (_event, url) => downloadPlaylist(url, win, options));
ipcMain.on("download-playlist-request", async (_event, url) => downloadPlaylist(url));
};
async function downloadSong(url, playlistFolder = undefined, trackId = undefined, increasePlaylistProgress = () => { }) {
@ -77,10 +77,10 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
if (metadata.album === 'N/A') metadata.album = '';
metadata.trackId = trackId;
const dir = playlistFolder || options.downloadFolder || app.getPath("downloads");
const dir = playlistFolder || config.get('downloadFolder') || app.getPath("downloads");
const name = `${metadata.artist ? `${metadata.artist} - ` : ""}${metadata.title}`;
const extension = presets[options.preset]?.extension || 'mp3';
const extension = presets[config.get('preset')]?.extension || 'mp3';
const filename = filenamify(`${name}.${extension}`, {
replacement: "_",
@ -88,7 +88,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
});
const filePath = join(dir, filename);
if (options.skipExisting && existsSync(filePath)) {
if (config.get('skipExisting') && existsSync(filePath)) {
sendFeedback(null, -1);
return;
}
@ -110,7 +110,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
mkdirSync(dir);
}
if (!presets[options.preset]) {
if (!presets[config.get('preset')]) {
const fileBuffer = await toMP3(iterableStream, metadata, format.content_length, sendFeedback, increasePlaylistProgress);
writeFileSync(filePath, await writeID3(fileBuffer, metadata, sendFeedback));
} else {
@ -126,7 +126,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
increasePlaylistProgress(ratio);
file.write(chunk);
}
await ffmpegWriteTags(filePath, metadata, presets[options.preset]?.ffmpegArgs);
await ffmpegWriteTags(filePath, metadata, presets[config.get('preset')]?.ffmpegArgs);
sendFeedback(null, -1);
}
@ -301,7 +301,7 @@ async function downloadPlaylist(givenUrl) {
let playlist;
try {
playlist = await ytpl(playlistId, {
limit: options.playlistMaxItems || Infinity,
limit: config.get('playlistMaxItems') || Infinity,
});
} catch (e) {
sendError("Error getting playlist info: make sure it isn't a private or \"Mixed for you\" playlist\n\n" + e);
@ -313,10 +313,10 @@ async function downloadPlaylist(givenUrl) {
}
const safePlaylistTitle = filenamify(playlist.title, { replacement: ' ' });
const folder = getFolder(options.downloadFolder);
const folder = getFolder(config.get('downloadFolder'));
const playlistFolder = join(folder, safePlaylistTitle);
if (existsSync(playlistFolder)) {
if (!options.skipExisting) {
if (!config.get('skipExisting')) {
sendError(new Error(`The folder ${playlistFolder} already exists`));
return;
}

View File

@ -0,0 +1,23 @@
const { setOptions, setMenuOptions } = require("../../config/plugins");
const defaultConfig = require("../../config/defaults");
let config = defaultConfig.plugins["downloader"];
module.exports.init = (options) => {
config = { ...config, ...options };
};
module.exports.setAndMaybeRestart = (option, value) => {
config[option] = value;
setMenuOptions("downloader", config);
};
module.exports.set = (option, value) => {
config[option] = value;
setOptions("downloader", config);
};
module.exports.get = (option) => {
let res = config[option];
return res;
};

View File

@ -9,7 +9,6 @@ let progress = null;
const downloadButton = ElementFromFile(
templatePath(__dirname, "download.html")
);
let pluginOptions = {};
let doneFirstLoad = false;
@ -57,9 +56,7 @@ global.download = () => {
ipcRenderer.send('download-song', videoUrl);
};
function observeMenu(options) {
pluginOptions = { ...pluginOptions, ...options };
function observeMenu() {
document.addEventListener('apiLoaded', () => {
observer.observe(document.querySelector('ytmusic-popup-container'), {
childList: true,

View File

@ -1,27 +1,26 @@
const { dialog } = require("electron");
const { setMenuOptions } = require("../../config/plugins");
const { downloadPlaylist } = require("./back");
const { defaultMenuDownloadLabel, getFolder, presets } = require("./utils");
const config = require("./config");
let downloadLabel = defaultMenuDownloadLabel;
module.exports = (win, options) => {
module.exports = () => {
return [
{
label: downloadLabel,
click: () => downloadPlaylist(undefined, win, options),
click: () => downloadPlaylist(),
},
{
label: "Choose download folder",
click: () => {
let result = dialog.showOpenDialogSync({
properties: ["openDirectory", "createDirectory"],
defaultPath: getFolder(options.downloadFolder),
defaultPath: getFolder(config.get('downloadFolder')),
});
if (result) {
options.downloadFolder = result[0];
setMenuOptions("downloader", options);
config.set("downloadFolder", result[0]);
} // else = user pressed cancel
},
},
@ -30,20 +29,18 @@ module.exports = (win, options) => {
submenu: Object.keys(presets).map((preset) => ({
label: preset,
type: "radio",
checked: config.get('preset') === preset,
click: () => {
options.preset = preset;
setMenuOptions("downloader", options);
config.set("preset", preset);
},
checked: options.preset === preset,
})),
},
{
label: "Skip existing files",
type: "checkbox",
checked: options.skipExisting,
click: () => {
options.skipExisting = !options.skipExisting;
setMenuOptions("downloader", options);
checked: config.get('skipExisting'),
click: (item) => {
config.set("skipExisting", item.checked);
}
}
];