From cceb45319ae82ff7c23c7accce64132aeb51b703 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 04:03:34 +0300 Subject: [PATCH 01/14] debug videoUrl from `start Radio` button in menu --- plugins/downloader/front.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index 200a7e15d6..23cd1448a4 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -40,9 +40,9 @@ const baseUrl = defaultConfig.url; global.download = () => { let metadata; let videoUrl = getSongMenu() - .querySelector("ytmusic-menu-navigation-item-renderer") - .querySelector("#navigation-endpoint") - .getAttribute("href"); + ?.querySelector('ytmusic-menu-navigation-item-renderer.iron-selected[tabindex="0"]') + ?.querySelector("#navigation-endpoint") + ?.getAttribute("href"); if (videoUrl) { videoUrl = baseUrl + "/" + videoUrl; metadata = null; From 2168cbca30b3dcecd265552926fac83683da8f78 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 04:04:35 +0300 Subject: [PATCH 02/14] use image from imageSrc if transfered --- plugins/downloader/back.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index ac32623da2..ddf3dc464c 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -44,12 +44,13 @@ function handle(win) { ipcMain.on("add-metadata", async (event, filePath, songBuffer, currentMetadata) => { let fileBuffer = songBuffer; - const songMetadata = { ...metadata, ...currentMetadata }; - if (!songMetadata.image && songMetadata.imageSrc) { - songMetadata.image = await getImage(songMetadata.imageSrc); + if (currentMetadata.imageSrc) { + currentMetadata.image = await getImage(currentMetadata.imageSrc); } + const songMetadata = { ...metadata, ...currentMetadata }; + try { const coverBuffer = songMetadata.image ? songMetadata.image.toPNG() : null; const writer = new ID3Writer(songBuffer); From a8ac2c3af988f299be85010e7fea541096b7e261 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 07:11:54 +0300 Subject: [PATCH 03/14] download progress bar on taskbar + Get the best possible artwork --- plugins/downloader/actions.js | 1 + plugins/downloader/back.js | 17 +++++++++++------ plugins/downloader/front.js | 7 ++++++- plugins/downloader/menu.js | 3 +-- plugins/downloader/utils.js | 30 ++++++++++++++++++++++++++++++ plugins/downloader/youtube-dl.js | 23 +++++++++++++---------- 6 files changed, 62 insertions(+), 19 deletions(-) diff --git a/plugins/downloader/actions.js b/plugins/downloader/actions.js index da75f181e9..0d6c342648 100644 --- a/plugins/downloader/actions.js +++ b/plugins/downloader/actions.js @@ -2,6 +2,7 @@ const CHANNEL = "downloader"; const ACTIONS = { ERROR: "error", METADATA: "metadata", + PROGRESS: "progress", }; module.exports = { diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index ddf3dc464c..4590525e2b 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -6,10 +6,11 @@ const { dialog, ipcMain } = require("electron"); const getSongInfo = require("../../providers/song-info"); const { injectCSS, listenAction } = require("../utils"); +const { cropMaxWidth } = require("./utils"); const { ACTIONS, CHANNEL } = require("./actions.js"); const { getImage } = require("../../providers/song-info"); -const sendError = (win, err) => { +const sendError = (err) => { const dialogOpts = { type: "info", buttons: ["OK"], @@ -17,6 +18,7 @@ const sendError = (win, err) => { message: "Argh! Apologies, download failed…", detail: err.toString(), }; + win.setProgressBar(-1); // close progress bar dialog.showMessageBox(dialogOpts); }; @@ -29,14 +31,17 @@ function handle(win) { metadata = info; }); - listenAction(CHANNEL, (event, action, error) => { + listenAction(CHANNEL, (event, action, arg) => { switch (action) { - case ACTIONS.ERROR: - sendError(win, error); + case ACTIONS.ERROR: //arg = error + sendError(arg); break; case ACTIONS.METADATA: event.returnValue = JSON.stringify(metadata); break; + case ACTIONS.PROGRESS: //arg = progress + win.setProgressBar(arg); + break; default: console.log("Unknown action: " + action); } @@ -46,7 +51,7 @@ function handle(win) { let fileBuffer = songBuffer; if (currentMetadata.imageSrc) { - currentMetadata.image = await getImage(currentMetadata.imageSrc); + currentMetadata.image = cropMaxWidth(await getImage(currentMetadata.imageSrc)); } const songMetadata = { ...metadata, ...currentMetadata }; @@ -69,7 +74,7 @@ function handle(win) { writer.addTag(); fileBuffer = Buffer.from(writer.arrayBuffer); } catch (error) { - sendError(win, error); + sendError(error); } writeFileSync(filePath, fileBuffer); diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index 23cd1448a4..f9a0e9b97d 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -25,6 +25,7 @@ const observer = new MutationObserver((mutations, observer) => { }); const reinit = () => { + triggerAction(CHANNEL, ACTIONS.PROGRESS, -1); // closes progress bar if (!progress) { console.warn("Cannot update progress"); } else { @@ -38,6 +39,7 @@ const baseUrl = defaultConfig.url; // contextBridge.exposeInMainWorld("downloader", { // download: () => { global.download = () => { + triggerAction(CHANNEL, ACTIONS.PROGRESS, 2); // starts with indefinite progress bar let metadata; let videoUrl = getSongMenu() ?.querySelector('ytmusic-menu-navigation-item-renderer.iron-selected[tabindex="0"]') @@ -53,12 +55,15 @@ global.download = () => { downloadVideoToMP3( videoUrl, - (feedback) => { + (feedback, ratio = undefined) => { if (!progress) { console.warn("Cannot update progress"); } else { progress.innerHTML = feedback; } + if (ratio) { + triggerAction(CHANNEL, ACTIONS.PROGRESS, ratio); + } }, (error) => { triggerAction(CHANNEL, ACTIONS.ERROR, error); diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js index f5b99d7390..b2dd5f257b 100644 --- a/plugins/downloader/menu.js +++ b/plugins/downloader/menu.js @@ -32,7 +32,7 @@ module.exports = (win, options) => { const currentURL = metadataURL || win.webContents.getURL(); const playlistID = new URL(currentURL).searchParams.get("list"); if (!playlistID) { - sendError(win, new Error("No playlist ID found")); + sendError(new Error("No playlist ID found")); return; } @@ -46,7 +46,6 @@ module.exports = (win, options) => { const playlistFolder = join(folder, playlistTitle); if (existsSync(playlistFolder)) { sendError( - win, new Error(`The folder ${playlistFolder} already exists`) ); return; diff --git a/plugins/downloader/utils.js b/plugins/downloader/utils.js index e2763541ab..7c5b9b965b 100644 --- a/plugins/downloader/utils.js +++ b/plugins/downloader/utils.js @@ -3,3 +3,33 @@ const electron = require("electron"); module.exports.getFolder = (customFolder) => customFolder || (electron.app || electron.remote.app).getPath("downloads"); module.exports.defaultMenuDownloadLabel = "Download playlist"; + +module.exports.UrlToJPG = (imgUrl, videoId) => { + if (!imgUrl || imgUrl.includes(".jpg")) return imgUrl; + if (imgUrl.includes("maxresdefault")) { + return "https://img.youtube.com/vi/"+videoId+"/maxresdefault.jpg"; + } + if (imgUrl.includes("hqdefault")) { + return "https://img.youtube.com/vi/"+videoId+"/hqdefault.jpg"; + } //it will almost never get further than hq + if (imgUrl.includes("mqdefault")) { + return "https://img.youtube.com/vi/"+videoId+"/mqdefault.jpg"; + } + if (imgUrl.includes("sdddefault")) { + return "https://img.youtube.com/vi/"+videoId+"/sdddefault.jpg"; + } + return "https://img.youtube.com/vi/"+videoId+"/default.jpg"; +} + +module.exports.cropMaxWidth = (image) => { + const imageSize = image.getSize(); + if (imageSize.width === 1280 && imageSize.height === 720) { + return image.crop({ + x: 280, + y: 0, + width: 720, + height: 720 + }); + } + return image; +} diff --git a/plugins/downloader/youtube-dl.js b/plugins/downloader/youtube-dl.js index 814d992499..7edf8143fd 100644 --- a/plugins/downloader/youtube-dl.js +++ b/plugins/downloader/youtube-dl.js @@ -14,7 +14,7 @@ const ytdl = require("ytdl-core"); const { triggerAction, triggerActionSync } = require("../utils"); const { ACTIONS, CHANNEL } = require("./actions.js"); -const { getFolder } = require("./utils"); +const { getFolder, UrlToJPG } = require("./utils"); const { cleanupArtistName } = require("../../providers/song-info"); const { createFFmpeg } = FFmpeg; @@ -37,12 +37,14 @@ const downloadVideoToMP3 = async ( sendFeedback("Downloading…"); if (metadata === null) { - const info = await ytdl.getInfo(videoUrl); - const thumbnails = info.videoDetails?.author?.thumbnails; + const { videoDetails } = await ytdl.getInfo(videoUrl); + const thumbnails = videoDetails?.thumbnails; metadata = { - artist: info.videoDetails?.media?.artist || cleanupArtistName(info.videoDetails?.author?.name) || "", - title: info.videoDetails?.media?.song || info.videoDetails?.title || "", - imageSrc: thumbnails ? thumbnails[thumbnails.length - 1].url : "" + artist: videoDetails?.media?.artist || cleanupArtistName(videoDetails?.author?.name) || "", + title: videoDetails?.media?.song || videoDetails?.title || "", + imageSrc: thumbnails ? + UrlToJPG(thumbnails[thumbnails.length - 1].url, videoDetails?.videoId) + : "" } } @@ -65,9 +67,10 @@ const downloadVideoToMP3 = async ( .on("data", (chunk) => { chunks.push(chunk); }) - .on("progress", (chunkLength, downloaded, total) => { - const progress = Math.floor((downloaded / total) * 100); - sendFeedback("Download: " + progress + "%"); + .on("progress", (_chunkLength, downloaded, total) => { + const ratio = downloaded / total; + const progress = Math.floor(ratio * 100); + sendFeedback("Download: " + progress + "%", ratio); }) .on("info", (info, format) => { videoName = info.videoDetails.title.replace("|", "").toString("ascii"); @@ -112,7 +115,7 @@ const toMP3 = async ( try { if (!ffmpeg.isLoaded()) { - sendFeedback("Loading…"); + sendFeedback("Loading…", 2); // indefinite progress bar after download await ffmpeg.load(); } From 3ea17e6f468c77fb4ec20b474dfb9170016c2879 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 08:05:38 +0300 Subject: [PATCH 04/14] refactor --- plugins/downloader/utils.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/plugins/downloader/utils.js b/plugins/downloader/utils.js index 7c5b9b965b..8408456dbc 100644 --- a/plugins/downloader/utils.js +++ b/plugins/downloader/utils.js @@ -4,25 +4,21 @@ module.exports.getFolder = (customFolder) => customFolder || (electron.app || electron.remote.app).getPath("downloads"); module.exports.defaultMenuDownloadLabel = "Download playlist"; +const orderedQualityList = ["maxresdefault", "hqdefault", "mqdefault", "sdddefault"]; module.exports.UrlToJPG = (imgUrl, videoId) => { if (!imgUrl || imgUrl.includes(".jpg")) return imgUrl; - if (imgUrl.includes("maxresdefault")) { - return "https://img.youtube.com/vi/"+videoId+"/maxresdefault.jpg"; + //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`; + } } - if (imgUrl.includes("hqdefault")) { - return "https://img.youtube.com/vi/"+videoId+"/hqdefault.jpg"; - } //it will almost never get further than hq - if (imgUrl.includes("mqdefault")) { - return "https://img.youtube.com/vi/"+videoId+"/mqdefault.jpg"; - } - if (imgUrl.includes("sdddefault")) { - return "https://img.youtube.com/vi/"+videoId+"/sdddefault.jpg"; - } - return "https://img.youtube.com/vi/"+videoId+"/default.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 if (imageSize.width === 1280 && imageSize.height === 720) { return image.crop({ x: 280, From 96a74f8955e7b7653fe4763e1023092a9913ec3d Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sat, 8 May 2021 19:19:11 +0300 Subject: [PATCH 05/14] use original metadata only if not already captured from ytpl.getInfo() --- plugins/downloader/back.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 4590525e2b..7064aada0e 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -49,13 +49,14 @@ function handle(win) { ipcMain.on("add-metadata", async (event, filePath, songBuffer, currentMetadata) => { let fileBuffer = songBuffer; - - if (currentMetadata.imageSrc) { + let songMetadata; + if (currentMetadata.imageSrc) { // means metadata come from ytpl.getInfo(); currentMetadata.image = cropMaxWidth(await getImage(currentMetadata.imageSrc)); + songMetadata = { ...currentMetadata }; + } else { + songMetadata = { ...metadata, ...currentMetadata }; } - const songMetadata = { ...metadata, ...currentMetadata }; - try { const coverBuffer = songMetadata.image ? songMetadata.image.toPNG() : null; const writer = new ID3Writer(songBuffer); From b3da77a6bc31473826cfd517b4427f67dd1b7778 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sat, 8 May 2021 19:24:25 +0300 Subject: [PATCH 06/14] small refactor --- plugins/downloader/back.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 7064aada0e..aae5344d04 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -51,8 +51,10 @@ function handle(win) { let fileBuffer = songBuffer; let songMetadata; if (currentMetadata.imageSrc) { // means metadata come from ytpl.getInfo(); - currentMetadata.image = cropMaxWidth(await getImage(currentMetadata.imageSrc)); - songMetadata = { ...currentMetadata }; + songMetadata = { + ...currentMetadata, + image: cropMaxWidth(await getImage(currentMetadata.imageSrc)) + }; } else { songMetadata = { ...metadata, ...currentMetadata }; } From e46e7b74e2c7c75592c0cc6df9bcc3221461df42 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 19:34:57 +0300 Subject: [PATCH 07/14] fix sendError() --- plugins/downloader/back.js | 6 +++--- plugins/downloader/menu.js | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index aae5344d04..43e385bb05 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -10,7 +10,7 @@ const { cropMaxWidth } = require("./utils"); const { ACTIONS, CHANNEL } = require("./actions.js"); const { getImage } = require("../../providers/song-info"); -const sendError = (err) => { +const sendError = (win, err) => { const dialogOpts = { type: "info", buttons: ["OK"], @@ -34,7 +34,7 @@ function handle(win) { listenAction(CHANNEL, (event, action, arg) => { switch (action) { case ACTIONS.ERROR: //arg = error - sendError(arg); + sendError(win, arg); break; case ACTIONS.METADATA: event.returnValue = JSON.stringify(metadata); @@ -77,7 +77,7 @@ function handle(win) { writer.addTag(); fileBuffer = Buffer.from(writer.arrayBuffer); } catch (error) { - sendError(error); + sendError(win, error); } writeFileSync(filePath, fileBuffer); diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js index b2dd5f257b..f5b99d7390 100644 --- a/plugins/downloader/menu.js +++ b/plugins/downloader/menu.js @@ -32,7 +32,7 @@ module.exports = (win, options) => { const currentURL = metadataURL || win.webContents.getURL(); const playlistID = new URL(currentURL).searchParams.get("list"); if (!playlistID) { - sendError(new Error("No playlist ID found")); + sendError(win, new Error("No playlist ID found")); return; } @@ -46,6 +46,7 @@ module.exports = (win, options) => { const playlistFolder = join(folder, playlistTitle); if (existsSync(playlistFolder)) { sendError( + win, new Error(`The folder ${playlistFolder} already exists`) ); return; From 3831e61d106b5f9cfa61971ba3ca011a70b7bf3b Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 19:45:33 +0300 Subject: [PATCH 08/14] differentiate names of different metadata sources --- plugins/downloader/back.js | 12 ++++++------ plugins/downloader/youtube-dl.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 43e385bb05..9d7b95b682 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -22,13 +22,13 @@ const sendError = (win, err) => { dialog.showMessageBox(dialogOpts); }; -let metadata = {}; +let nowPlayingMetadata = {}; function handle(win) { injectCSS(win.webContents, join(__dirname, "style.css")); const registerCallback = getSongInfo(win); registerCallback((info) => { - metadata = info; + nowPlayingMetadata = info; }); listenAction(CHANNEL, (event, action, arg) => { @@ -37,7 +37,7 @@ function handle(win) { sendError(win, arg); break; case ACTIONS.METADATA: - event.returnValue = JSON.stringify(metadata); + event.returnValue = JSON.stringify(nowPlayingMetadata); break; case ACTIONS.PROGRESS: //arg = progress win.setProgressBar(arg); @@ -50,13 +50,13 @@ function handle(win) { ipcMain.on("add-metadata", async (event, filePath, songBuffer, currentMetadata) => { let fileBuffer = songBuffer; let songMetadata; - if (currentMetadata.imageSrc) { // means metadata come from ytpl.getInfo(); + if (currentMetadata.imageSrcYTPL) { // means metadata come from ytpl.getInfo(); songMetadata = { ...currentMetadata, - image: cropMaxWidth(await getImage(currentMetadata.imageSrc)) + image: cropMaxWidth(await getImage(currentMetadata.imageSrcYTPL)) }; } else { - songMetadata = { ...metadata, ...currentMetadata }; + songMetadata = { ...nowPlayingMetadata, ...currentMetadata }; } try { diff --git a/plugins/downloader/youtube-dl.js b/plugins/downloader/youtube-dl.js index 7edf8143fd..7e54195ae1 100644 --- a/plugins/downloader/youtube-dl.js +++ b/plugins/downloader/youtube-dl.js @@ -42,7 +42,7 @@ const downloadVideoToMP3 = async ( metadata = { artist: videoDetails?.media?.artist || cleanupArtistName(videoDetails?.author?.name) || "", title: videoDetails?.media?.song || videoDetails?.title || "", - imageSrc: thumbnails ? + imageSrcYTPL: thumbnails ? UrlToJPG(thumbnails[thumbnails.length - 1].url, videoDetails?.videoId) : "" } @@ -149,7 +149,7 @@ const toMP3 = async ( ipcRenderer.send("add-metadata", filePath, fileBuffer, { artist: metadata.artist, title: metadata.title, - imageSrc: metadata.imageSrc + imageSrcYTPL: metadata.imageSrcYTPL }); ipcRenderer.once("add-metadata-done", reinit); } catch (e) { From ccd320d8ff27d91633f425fd55f1c5f16978cbf9 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 20:40:44 +0300 Subject: [PATCH 09/14] minimize getArtist() --- providers/song-info.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/providers/song-info.js b/providers/song-info.js index 15b5145906..fc0c14c581 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -30,15 +30,11 @@ const getPausedStatus = async (win) => { }; const getArtist = async (win) => { - return await win.webContents.executeJavaScript( - ` - var bar = document.getElementsByClassName('subtitle ytmusic-player-bar')[0]; - var artistName = (bar.getElementsByClassName('yt-formatted-string')[0]) || (bar.getElementsByClassName('byline ytmusic-player-bar')[0]); - if (artistName) { - artistName.textContent; - } - ` - ); + return await win.webContents.executeJavaScript(` + document.querySelector(".subtitle.ytmusic-player-bar") + ?.querySelector(".yt-formatted-string") + ?.textContent + `); } // Fill songInfo with empty values @@ -66,6 +62,8 @@ const handleData = async (responseText, win) => { songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical; + console.log("updating song-info"); + win.webContents.send("update-song-info", JSON.stringify(songInfo)); }; From da3c709ff0bbecf6d85813eee22ad408f8ef266a Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 20:46:15 +0300 Subject: [PATCH 10/14] remove videoDetails?.media query from XHR (it never exists in the XHR responce) --- providers/song-info.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/providers/song-info.js b/providers/song-info.js index fc0c14c581..ff12d55ef1 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -53,8 +53,8 @@ const songInfo = { const handleData = async (responseText, win) => { let data = JSON.parse(responseText); - songInfo.title = data.videoDetails?.media?.song || data?.videoDetails?.title; - songInfo.artist = data.videoDetails?.media?.artist || await getArtist(win) || cleanupArtistName(data?.videoDetails?.author); + songInfo.title = data?.videoDetails?.title; + songInfo.artist = await getArtist(win) || cleanupArtistName(data?.videoDetails?.author); songInfo.views = data?.videoDetails?.viewCount; songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url; songInfo.songDuration = data?.videoDetails?.lengthSeconds; @@ -62,8 +62,6 @@ const handleData = async (responseText, win) => { songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical; - console.log("updating song-info"); - win.webContents.send("update-song-info", JSON.stringify(songInfo)); }; From 6b88397f820eee0d94cbc4f4bef7f420f1ac2468 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 21:21:07 +0300 Subject: [PATCH 11/14] lint --- plugins/downloader/back.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 9d7b95b682..76a77b929f 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -10,16 +10,15 @@ const { cropMaxWidth } = require("./utils"); const { ACTIONS, CHANNEL } = require("./actions.js"); const { getImage } = require("../../providers/song-info"); -const sendError = (win, err) => { - const dialogOpts = { +const sendError = (win, error) => { + win.setProgressBar(-1); // close progress bar + dialog.showMessageBox({ type: "info", buttons: ["OK"], title: "Error in download!", message: "Argh! Apologies, download failed…", - detail: err.toString(), - }; - win.setProgressBar(-1); // close progress bar - dialog.showMessageBox(dialogOpts); + detail: error.toString(), + }); }; let nowPlayingMetadata = {}; @@ -33,13 +32,13 @@ function handle(win) { listenAction(CHANNEL, (event, action, arg) => { switch (action) { - case ACTIONS.ERROR: //arg = error + case ACTIONS.ERROR: // arg = error sendError(win, arg); break; case ACTIONS.METADATA: event.returnValue = JSON.stringify(nowPlayingMetadata); break; - case ACTIONS.PROGRESS: //arg = progress + case ACTIONS.PROGRESS: // arg = progress win.setProgressBar(arg); break; default: @@ -49,15 +48,12 @@ function handle(win) { ipcMain.on("add-metadata", async (event, filePath, songBuffer, currentMetadata) => { let fileBuffer = songBuffer; - let songMetadata; - if (currentMetadata.imageSrcYTPL) { // means metadata come from ytpl.getInfo(); - songMetadata = { + const songMetadata = currentMetadata.imageSrcYTPL ? // This means metadata come from ytpl.getInfo(); + { ...currentMetadata, image: cropMaxWidth(await getImage(currentMetadata.imageSrcYTPL)) - }; - } else { - songMetadata = { ...nowPlayingMetadata, ...currentMetadata }; - } + } : + { ...nowPlayingMetadata, ...currentMetadata }; try { const coverBuffer = songMetadata.image ? songMetadata.image.toPNG() : null; @@ -71,7 +67,7 @@ function handle(win) { writer.setFrame("APIC", { type: 3, data: coverBuffer, - description: "", + description: "" }); } writer.addTag(); From d7e42471a473f0a8cefd955f0a7db5d8fd7116af Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 23:01:57 +0300 Subject: [PATCH 12/14] lint --- plugins/downloader/front.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index f9a0e9b97d..034834aebc 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -42,9 +42,10 @@ global.download = () => { triggerAction(CHANNEL, ACTIONS.PROGRESS, 2); // starts with indefinite progress bar let metadata; let videoUrl = getSongMenu() - ?.querySelector('ytmusic-menu-navigation-item-renderer.iron-selected[tabindex="0"]') - ?.querySelector("#navigation-endpoint") + // selector of first button which is always "Start Radio" + ?.querySelector('ytmusic-menu-navigation-item-renderer.iron-selected[tabindex="0"] #navigation-endpoint') ?.getAttribute("href"); + console.log(videoUrl) if (videoUrl) { videoUrl = baseUrl + "/" + videoUrl; metadata = null; From cf4bbf94e47cc150ac690e47ece415db2605fd47 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 23:02:52 +0300 Subject: [PATCH 13/14] update radioButton querySelector --- plugins/downloader/front.js | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index 034834aebc..879ba2224f 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -45,7 +45,6 @@ global.download = () => { // selector of first button which is always "Start Radio" ?.querySelector('ytmusic-menu-navigation-item-renderer.iron-selected[tabindex="0"] #navigation-endpoint') ?.getAttribute("href"); - console.log(videoUrl) if (videoUrl) { videoUrl = baseUrl + "/" + videoUrl; metadata = null; From d2a5110f3b5d7fb863f650d5f43000ec3cfd242a Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 8 May 2021 23:06:18 +0300 Subject: [PATCH 14/14] querySelector optimization #2 --- providers/song-info.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/providers/song-info.js b/providers/song-info.js index ff12d55ef1..5701d1f38c 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -31,8 +31,7 @@ const getPausedStatus = async (win) => { const getArtist = async (win) => { return await win.webContents.executeJavaScript(` - document.querySelector(".subtitle.ytmusic-player-bar") - ?.querySelector(".yt-formatted-string") + document.querySelector(".subtitle.ytmusic-player-bar .yt-formatted-string") ?.textContent `); }