From 560e323893650b7df53182e48d6ba9cdd9d101d2 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sat, 4 Mar 2023 13:40:03 +0200 Subject: [PATCH] lint --- plugins/downloader/back.js | 62 ++++++++++++++++++------------------- plugins/downloader/front.js | 3 +- plugins/downloader/utils.js | 12 ------- 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 5adde56a..a316de7c 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -30,19 +30,19 @@ let win; let playingUrl = undefined; const sendError = (error) => { - win.setProgressBar(-1); // close progress bar - setBadge(0); // close badge - sendFeedback_(win); // reset feedback + win.setProgressBar(-1); // close progress bar + setBadge(0); // close badge + sendFeedback_(win); // reset feedback - console.error(error); - dialog.showMessageBox({ - type: "info", - buttons: ["OK"], - title: "Error in download!", - message: "Argh! Apologies, download failed…", - detail: error.toString(), - }); + console.error(error); + dialog.showMessageBox({ + type: "info", + buttons: ["OK"], + title: "Error in download!", + message: "Argh! Apologies, download failed…", + detail: error.toString(), + }); }; module.exports = async (win_, options_) => { @@ -51,14 +51,14 @@ module.exports = async (win_, options_) => { injectCSS(win.webContents, join(__dirname, "style.css")); yt = await Innertube.create({ cache: new UniversalCache(false), generate_session_locally: true }); - ipcMain.handle("download-song", (_, url) => downloadSong(url)); + ipcMain.on("download-song", (_, url) => downloadSong(url)); 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)); }; -async function downloadSong(url, playlistFolder = undefined, trackId = undefined, increasePlaylistProgress = ()=>{}) { +async function downloadSong(url, playlistFolder = undefined, trackId = undefined, increasePlaylistProgress = () => { }) { const sendFeedback = (message, progress) => { if (!playlistFolder) { sendFeedback_(win, message); @@ -69,7 +69,11 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined }; sendFeedback(`Downloading...`, 2); - const metadata = await getMetadata(url); + + const id = url.match(/v=([^&]+)/)?.[1]; + const info = await yt.music.getInfo(id); + + const metadata = getMetadata(info); metadata.trackId = trackId; const download_options = { @@ -78,8 +82,8 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined format: 'any' // media container format }; - const format = metadata.info.chooseFormat(download_options); - const stream = await metadata.info.download(download_options); + const format = info.chooseFormat(download_options); + const stream = await info.download(download_options); console.info(`Downloading ${metadata.artist} - ${metadata.title} [${metadata.id}]`); @@ -121,23 +125,17 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined } sendFeedback(null, -1); - console.info(`Saved download to ${filePath}`); + console.info(`Done: "${filePath}"`); } module.exports.downloadSong = downloadSong; -async function getMetadata(url) { - const id = url.match(/v=([^&]+)/)?.[1]; - const info = await yt.music.getInfo(id); - - return { - id: info.basic_info.id, - title: info.basic_info.title, - artist: info.basic_info.author, - album: info.player_overlays?.browser_media_session?.album?.text, - image: info.basic_info.thumbnail[0].url, - info - }; -} +const getMetadata = (info) => ({ + id: info.basic_info.id, + title: info.basic_info.title, + artist: info.basic_info.author, + album: info.player_overlays?.browser_media_session?.album?.text, + image: info.basic_info.thumbnail[0].url, +}); async function writeID3(buffer, metadata, sendFeedback) { try { @@ -349,7 +347,9 @@ async function downloadPlaylist(givenUrl) { for (const song of playlist.items) { sendFeedback(`Downloading ${counter}/${playlist.items.length}...`); const trackId = isAlbum ? counter : undefined; - await downloadSong(song.url, playlistFolder, trackId, increaseProgress).catch((e) => sendError(e)); + await downloadSong(song.url, playlistFolder, trackId, increaseProgress) + .catch((e) => sendError(`Error downloading "${song.author} - ${song.title}":\n ${e}`)); + win.setProgressBar(counter / playlist.items.length); setBadge(playlist.items.length - counter); counter++; diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index d5cc19f2..3113f029 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -49,8 +49,7 @@ global.download = () => { videoUrl = metadata.url || window.location.href; } - ipcRenderer.invoke('download-song', videoUrl); - return; + ipcRenderer.send('download-song', videoUrl); }; function observeMenu(options) { diff --git a/plugins/downloader/utils.js b/plugins/downloader/utils.js index def066cb..6b9c449b 100644 --- a/plugins/downloader/utils.js +++ b/plugins/downloader/utils.js @@ -8,18 +8,6 @@ module.exports.sendFeedback = (win, message) => { win.webContents.send("downloader-feedback", message); }; -const orderedQualityList = ["maxresdefault", "hqdefault", "mqdefault", "sdddefault"]; -module.exports.urlToJPG = (imgUrl, videoId) => { - if (!imgUrl || imgUrl.includes(".jpg")) return imgUrl; - //it will almost never get further than hqdefault - for (const quality of orderedQualityList) { - if (imgUrl.includes(quality)) { - return `https://img.youtube.com/vi/${videoId}/${quality}.jpg`; - } - } - return `https://img.youtube.com/vi/${videoId}/default.jpg`; -} - module.exports.cropMaxWidth = (image) => { const imageSize = image.getSize(); // standart youtube artwork width with margins from both sides is 280 + 720 + 280