Skip to content

Commit

Permalink
Merge pull request #213 from th-ch/song-info-front
Browse files Browse the repository at this point in the history
Pass metadata to front + use metadata URL in downloader
  • Loading branch information
th-ch authored Apr 2, 2021
2 parents d67d697 + 640f146 commit 2254cac
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 59 deletions.
2 changes: 1 addition & 1 deletion plugins/downloader/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const reinit = () => {
// contextBridge.exposeInMainWorld("downloader", {
// download: () => {
global.download = () => {
const videoUrl = window.location.href;
const videoUrl = global.songInfo.url || window.location.href;

downloadVideoToMP3(
videoUrl,
Expand Down
123 changes: 68 additions & 55 deletions plugins/downloader/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,84 @@ const is = require("electron-is");
const ytpl = require("ytpl");

const { setOptions } = require("../../config/plugins");
const getSongInfo = require("../../providers/song-info");
const { sendError } = require("./back");
const { defaultMenuDownloadLabel, getFolder } = require("./utils");

let downloadLabel = defaultMenuDownloadLabel;
let metadataURL = undefined;
let callbackIsRegistered = false;

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;
}
module.exports = (win, options, refreshMenu) => {
if (!callbackIsRegistered) {
const registerCallback = getSongInfo(win);
registerCallback((info) => {
metadataURL = info.url;
});
callbackIsRegistered = true;
}

const playlist = await ytpl(playlistID);
const playlistTitle = playlist.title;
return [
{
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 playlistFolder = join(folder, playlistTitle);
if (existsSync(playlistFolder)) {
sendError(
win,
new Error(`The folder ${playlistFolder} already exists`)
);
return;
}
mkdirSync(playlistFolder, { recursive: true });
const playlist = await ytpl(playlistID);
const playlistTitle = playlist.title;

ipcMain.on("downloader-feedback", (_, feedback) => {
downloadLabel = feedback;
refreshMenu();
});
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();
downloadLabel = `Downloading "${playlistTitle}"`;
refreshMenu();

if (is.dev()) {
console.log(
`Downloading playlist "${playlistTitle}" (${playlist.items.length} songs)`
);
}
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
);
});
playlist.items.slice(0, options.playlistMaxItems).forEach((song) => {
win.webContents.send(
"downloader-download-playlist",
song,
playlistTitle,
options
);
});
},
},
},
{
label: "Choose download folder",
click: () => {
let result = dialog.showOpenDialogSync({
properties: ["openDirectory", "createDirectory"],
defaultPath: getFolder(options.downloadFolder),
});
if (result) {
options.downloadFolder = result[0];
setOptions("downloader", options);
} // else = user pressed cancel
{
label: "Choose download folder",
click: () => {
let result = dialog.showOpenDialogSync({
properties: ["openDirectory", "createDirectory"],
defaultPath: getFolder(options.downloadFolder),
});
if (result) {
options.downloadFolder = result[0];
setOptions("downloader", options);
} // else = user pressed cancel
},
},
},
];
];
};
6 changes: 6 additions & 0 deletions providers/song-info-front.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const { ipcRenderer } = require("electron");

global.songInfo = {};

ipcRenderer.on("update-song-info", (_, extractedSongInfo) => {
global.songInfo = JSON.parse(extractedSongInfo);
});

const injectListener = () => {
var oldXHR = window.XMLHttpRequest;
function newXHR() {
Expand Down
13 changes: 10 additions & 3 deletions providers/song-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const songInfo = {
uploadDate: "",
imageSrc: "",
image: null,
isPaused: true,
isPaused: undefined,
songDuration: 0,
elapsedSeconds: 0,
url: "",
};

const handleData = async (_event, responseText) => {
const handleData = async (responseText, win) => {
let data = JSON.parse(responseText);
songInfo.title = data?.videoDetails?.title;
songInfo.artist = data?.videoDetails?.author;
Expand All @@ -52,6 +52,8 @@ const handleData = async (_event, responseText) => {
songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical;

win.webContents.send("update-song-info", JSON.stringify(songInfo));
};

const registerProvider = (win) => {
Expand All @@ -77,7 +79,12 @@ const registerProvider = (win) => {
});

// 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;
};
Expand Down

0 comments on commit 2254cac

Please sign in to comment.