mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
use centralized config
This commit is contained in:
@ -25,7 +25,7 @@ const ffmpegMutex = new Mutex();
|
|||||||
|
|
||||||
/** @type {Innertube} */
|
/** @type {Innertube} */
|
||||||
let yt;
|
let yt;
|
||||||
let options;
|
const config = require("./config");
|
||||||
let win;
|
let win;
|
||||||
let playingUrl = undefined;
|
let playingUrl = undefined;
|
||||||
|
|
||||||
@ -45,9 +45,9 @@ const sendError = (error) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = async (win_, options_) => {
|
module.exports = async (win_, options) => {
|
||||||
options = options_;
|
|
||||||
win = win_;
|
win = win_;
|
||||||
|
config.init(options);
|
||||||
injectCSS(win.webContents, join(__dirname, "style.css"));
|
injectCSS(win.webContents, join(__dirname, "style.css"));
|
||||||
|
|
||||||
yt = await Innertube.create({ cache: new UniversalCache(false), generate_session_locally: true });
|
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) => {
|
ipcMain.on("video-src-changed", async (_, data) => {
|
||||||
playingUrl = JSON.parse(data)?.microformat?.microformatDataRenderer?.urlCanonical;
|
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 = () => { }) {
|
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 = '';
|
if (metadata.album === 'N/A') metadata.album = '';
|
||||||
metadata.trackId = trackId;
|
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 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}`, {
|
const filename = filenamify(`${name}.${extension}`, {
|
||||||
replacement: "_",
|
replacement: "_",
|
||||||
@ -88,7 +88,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
|
|||||||
});
|
});
|
||||||
const filePath = join(dir, filename);
|
const filePath = join(dir, filename);
|
||||||
|
|
||||||
if (options.skipExisting && existsSync(filePath)) {
|
if (config.get('skipExisting') && existsSync(filePath)) {
|
||||||
sendFeedback(null, -1);
|
sendFeedback(null, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
|
|||||||
mkdirSync(dir);
|
mkdirSync(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!presets[options.preset]) {
|
if (!presets[config.get('preset')]) {
|
||||||
const fileBuffer = await toMP3(iterableStream, metadata, format.content_length, sendFeedback, increasePlaylistProgress);
|
const fileBuffer = await toMP3(iterableStream, metadata, format.content_length, sendFeedback, increasePlaylistProgress);
|
||||||
writeFileSync(filePath, await writeID3(fileBuffer, metadata, sendFeedback));
|
writeFileSync(filePath, await writeID3(fileBuffer, metadata, sendFeedback));
|
||||||
} else {
|
} else {
|
||||||
@ -126,7 +126,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
|
|||||||
increasePlaylistProgress(ratio);
|
increasePlaylistProgress(ratio);
|
||||||
file.write(chunk);
|
file.write(chunk);
|
||||||
}
|
}
|
||||||
await ffmpegWriteTags(filePath, metadata, presets[options.preset]?.ffmpegArgs);
|
await ffmpegWriteTags(filePath, metadata, presets[config.get('preset')]?.ffmpegArgs);
|
||||||
sendFeedback(null, -1);
|
sendFeedback(null, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,7 +301,7 @@ async function downloadPlaylist(givenUrl) {
|
|||||||
let playlist;
|
let playlist;
|
||||||
try {
|
try {
|
||||||
playlist = await ytpl(playlistId, {
|
playlist = await ytpl(playlistId, {
|
||||||
limit: options.playlistMaxItems || Infinity,
|
limit: config.get('playlistMaxItems') || Infinity,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sendError("Error getting playlist info: make sure it isn't a private or \"Mixed for you\" playlist\n\n" + 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 safePlaylistTitle = filenamify(playlist.title, { replacement: ' ' });
|
||||||
|
|
||||||
const folder = getFolder(options.downloadFolder);
|
const folder = getFolder(config.get('downloadFolder'));
|
||||||
const playlistFolder = join(folder, safePlaylistTitle);
|
const playlistFolder = join(folder, safePlaylistTitle);
|
||||||
if (existsSync(playlistFolder)) {
|
if (existsSync(playlistFolder)) {
|
||||||
if (!options.skipExisting) {
|
if (!config.get('skipExisting')) {
|
||||||
sendError(new Error(`The folder ${playlistFolder} already exists`));
|
sendError(new Error(`The folder ${playlistFolder} already exists`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
23
plugins/downloader/config.js
Normal file
23
plugins/downloader/config.js
Normal 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;
|
||||||
|
};
|
||||||
@ -9,7 +9,6 @@ let progress = null;
|
|||||||
const downloadButton = ElementFromFile(
|
const downloadButton = ElementFromFile(
|
||||||
templatePath(__dirname, "download.html")
|
templatePath(__dirname, "download.html")
|
||||||
);
|
);
|
||||||
let pluginOptions = {};
|
|
||||||
|
|
||||||
let doneFirstLoad = false;
|
let doneFirstLoad = false;
|
||||||
|
|
||||||
@ -57,9 +56,7 @@ global.download = () => {
|
|||||||
ipcRenderer.send('download-song', videoUrl);
|
ipcRenderer.send('download-song', videoUrl);
|
||||||
};
|
};
|
||||||
|
|
||||||
function observeMenu(options) {
|
function observeMenu() {
|
||||||
pluginOptions = { ...pluginOptions, ...options };
|
|
||||||
|
|
||||||
document.addEventListener('apiLoaded', () => {
|
document.addEventListener('apiLoaded', () => {
|
||||||
observer.observe(document.querySelector('ytmusic-popup-container'), {
|
observer.observe(document.querySelector('ytmusic-popup-container'), {
|
||||||
childList: true,
|
childList: true,
|
||||||
|
|||||||
@ -1,27 +1,26 @@
|
|||||||
const { dialog } = require("electron");
|
const { dialog } = require("electron");
|
||||||
|
|
||||||
const { setMenuOptions } = require("../../config/plugins");
|
|
||||||
const { downloadPlaylist } = require("./back");
|
const { downloadPlaylist } = require("./back");
|
||||||
const { defaultMenuDownloadLabel, getFolder, presets } = require("./utils");
|
const { defaultMenuDownloadLabel, getFolder, presets } = require("./utils");
|
||||||
|
const config = require("./config");
|
||||||
|
|
||||||
let downloadLabel = defaultMenuDownloadLabel;
|
let downloadLabel = defaultMenuDownloadLabel;
|
||||||
|
|
||||||
module.exports = (win, options) => {
|
module.exports = () => {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: downloadLabel,
|
label: downloadLabel,
|
||||||
click: () => downloadPlaylist(undefined, win, options),
|
click: () => downloadPlaylist(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Choose download folder",
|
label: "Choose download folder",
|
||||||
click: () => {
|
click: () => {
|
||||||
let result = dialog.showOpenDialogSync({
|
let result = dialog.showOpenDialogSync({
|
||||||
properties: ["openDirectory", "createDirectory"],
|
properties: ["openDirectory", "createDirectory"],
|
||||||
defaultPath: getFolder(options.downloadFolder),
|
defaultPath: getFolder(config.get('downloadFolder')),
|
||||||
});
|
});
|
||||||
if (result) {
|
if (result) {
|
||||||
options.downloadFolder = result[0];
|
config.set("downloadFolder", result[0]);
|
||||||
setMenuOptions("downloader", options);
|
|
||||||
} // else = user pressed cancel
|
} // else = user pressed cancel
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -30,20 +29,18 @@ module.exports = (win, options) => {
|
|||||||
submenu: Object.keys(presets).map((preset) => ({
|
submenu: Object.keys(presets).map((preset) => ({
|
||||||
label: preset,
|
label: preset,
|
||||||
type: "radio",
|
type: "radio",
|
||||||
|
checked: config.get('preset') === preset,
|
||||||
click: () => {
|
click: () => {
|
||||||
options.preset = preset;
|
config.set("preset", preset);
|
||||||
setMenuOptions("downloader", options);
|
|
||||||
},
|
},
|
||||||
checked: options.preset === preset,
|
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Skip existing files",
|
label: "Skip existing files",
|
||||||
type: "checkbox",
|
type: "checkbox",
|
||||||
checked: options.skipExisting,
|
checked: config.get('skipExisting'),
|
||||||
click: () => {
|
click: (item) => {
|
||||||
options.skipExisting = !options.skipExisting;
|
config.set("skipExisting", item.checked);
|
||||||
setMenuOptions("downloader", options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user