From e72915c5d09f9da30e750cf58f635e5d5bcea918 Mon Sep 17 00:00:00 2001 From: TC Date: Wed, 24 Mar 2021 21:52:12 +0100 Subject: [PATCH] Add custom menu in downloader plugin ("download playlist" item) --- plugins/downloader/menu.js | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 plugins/downloader/menu.js diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js new file mode 100644 index 00000000..4eda01a5 --- /dev/null +++ b/plugins/downloader/menu.js @@ -0,0 +1,63 @@ +const { existsSync, mkdirSync } = require("fs"); +const { join } = require("path"); +const { URL } = require("url"); + +const { ipcMain } = require("electron"); +const is = require("electron-is"); +const ytpl = require("ytpl"); + +const { sendError } = require("./back"); +const { defaultMenuDownloadLabel, getFolder } = require("./utils"); + +let downloadLabel = defaultMenuDownloadLabel; + +module.exports = (win, options, refreshMenu) => [ + { + label: downloadLabel, + click: async () => { + const currentURL = win.webContents.getURL(); + const playlistID = new URL(currentURL).searchParams.get("list"); + if (!playlistID) { + sendError(win, new Error("No playlist ID found")); + return; + } + + const playlist = await ytpl(playlistID); + const playlistTitle = playlist.title; + + const folder = getFolder(options.downloadFolder); + const playlistFolder = join(folder, playlistTitle); + if (existsSync(playlistFolder)) { + sendError( + win, + new Error(`The folder ${playlistFolder} already exists`) + ); + return; + } + mkdirSync(playlistFolder, { recursive: true }); + + ipcMain.on("downloader-feedback", (_, feedback) => { + downloadLabel = feedback; + refreshMenu(); + }); + + downloadLabel = `Downloading "${playlistTitle}"`; + refreshMenu(); + + if (is.dev()) { + console.log( + `Downloading playlist "${playlistTitle}" (${playlist.items.length} songs)` + ); + } + + playlist.items.slice(0, options.playlistMaxItems).forEach((song) => { + win.webContents.send( + "downloader-download-playlist", + song, + playlistTitle, + options + ); + }); + }, + }, +];