Merge pull request #213 from th-ch/song-info-front

Pass metadata to front + use metadata URL in downloader
This commit is contained in:
th-ch
2021-04-02 22:59:57 +02:00
committed by GitHub
4 changed files with 85 additions and 59 deletions

View File

@ -35,7 +35,7 @@ const reinit = () => {
// contextBridge.exposeInMainWorld("downloader", { // contextBridge.exposeInMainWorld("downloader", {
// download: () => { // download: () => {
global.download = () => { global.download = () => {
const videoUrl = window.location.href; const videoUrl = global.songInfo.url || window.location.href;
downloadVideoToMP3( downloadVideoToMP3(
videoUrl, videoUrl,

View File

@ -7,71 +7,84 @@ const is = require("electron-is");
const ytpl = require("ytpl"); const ytpl = require("ytpl");
const { setOptions } = require("../../config/plugins"); const { setOptions } = require("../../config/plugins");
const getSongInfo = require("../../providers/song-info");
const { sendError } = require("./back"); const { sendError } = require("./back");
const { defaultMenuDownloadLabel, getFolder } = require("./utils"); const { defaultMenuDownloadLabel, getFolder } = require("./utils");
let downloadLabel = defaultMenuDownloadLabel; let downloadLabel = defaultMenuDownloadLabel;
let metadataURL = undefined;
let callbackIsRegistered = false;
module.exports = (win, options, refreshMenu) => [ module.exports = (win, options, refreshMenu) => {
{ if (!callbackIsRegistered) {
label: downloadLabel, const registerCallback = getSongInfo(win);
click: async () => { registerCallback((info) => {
const currentURL = win.webContents.getURL(); metadataURL = info.url;
const playlistID = new URL(currentURL).searchParams.get("list"); });
if (!playlistID) { callbackIsRegistered = true;
sendError(win, new Error("No playlist ID found")); }
return;
}
const playlist = await ytpl(playlistID); return [
const playlistTitle = playlist.title; {
label: downloadLabel,
click: async () => {
const currentURL = metadataURL || win.webContents.getURL();
const playlistID = new URL(currentURL).searchParams.get("list");
if (!playlistID) {
sendError(win, new Error("No playlist ID found"));
return;
}
const folder = getFolder(options.downloadFolder); const playlist = await ytpl(playlistID);
const playlistFolder = join(folder, playlistTitle); const playlistTitle = playlist.title;
if (existsSync(playlistFolder)) {
sendError(
win,
new Error(`The folder ${playlistFolder} already exists`)
);
return;
}
mkdirSync(playlistFolder, { recursive: true });
ipcMain.on("downloader-feedback", (_, feedback) => { const folder = getFolder(options.downloadFolder);
downloadLabel = feedback; 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(); refreshMenu();
});
downloadLabel = `Downloading "${playlistTitle}"`; if (is.dev()) {
refreshMenu(); console.log(
`Downloading playlist "${playlistTitle}" (${playlist.items.length} songs)`
);
}
if (is.dev()) { playlist.items.slice(0, options.playlistMaxItems).forEach((song) => {
console.log( win.webContents.send(
`Downloading playlist "${playlistTitle}" (${playlist.items.length} songs)` "downloader-download-playlist",
); song,
} playlistTitle,
options
playlist.items.slice(0, options.playlistMaxItems).forEach((song) => { );
win.webContents.send( });
"downloader-download-playlist", },
song,
playlistTitle,
options
);
});
}, },
}, {
{ 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(options.downloadFolder), });
}); if (result) {
if (result) { options.downloadFolder = result[0];
options.downloadFolder = result[0]; setOptions("downloader", options);
setOptions("downloader", options); } // else = user pressed cancel
} // else = user pressed cancel },
}, },
}, ];
]; };

View File

@ -1,5 +1,11 @@
const { ipcRenderer } = require("electron"); const { ipcRenderer } = require("electron");
global.songInfo = {};
ipcRenderer.on("update-song-info", (_, extractedSongInfo) => {
global.songInfo = JSON.parse(extractedSongInfo);
});
const injectListener = () => { const injectListener = () => {
var oldXHR = window.XMLHttpRequest; var oldXHR = window.XMLHttpRequest;
function newXHR() { function newXHR() {

View File

@ -36,13 +36,13 @@ const songInfo = {
uploadDate: "", uploadDate: "",
imageSrc: "", imageSrc: "",
image: null, image: null,
isPaused: true, isPaused: undefined,
songDuration: 0, songDuration: 0,
elapsedSeconds: 0, elapsedSeconds: 0,
url: "", url: "",
}; };
const handleData = async (_event, responseText) => { const handleData = async (responseText, win) => {
let data = JSON.parse(responseText); let data = JSON.parse(responseText);
songInfo.title = data?.videoDetails?.title; songInfo.title = data?.videoDetails?.title;
songInfo.artist = data?.videoDetails?.author; songInfo.artist = data?.videoDetails?.author;
@ -52,6 +52,8 @@ const handleData = async (_event, responseText) => {
songInfo.image = await getImage(songInfo.imageSrc); songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical; songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical;
win.webContents.send("update-song-info", JSON.stringify(songInfo));
}; };
const registerProvider = (win) => { const registerProvider = (win) => {
@ -77,7 +79,12 @@ const registerProvider = (win) => {
}); });
// This will be called when the song-info-front finds a new request with song data // This will be called when the song-info-front finds a new request with song data
ipcMain.on("song-info-request", handleData); ipcMain.on("song-info-request", async (_, responseText) => {
await handleData(responseText, win);
callbacks.forEach((c) => {
c(songInfo);
});
});
return registerCallback; return registerCallback;
}; };