fix downloader playlist download

This commit is contained in:
Araxeus
2021-11-21 19:44:01 +02:00
parent 6726e2600b
commit 185ebbf417
2 changed files with 13 additions and 12 deletions

View File

@ -1,25 +1,23 @@
const { existsSync, mkdirSync } = require("fs"); const { existsSync, mkdirSync } = require("fs");
const { join } = require("path"); const { join } = require("path");
const { URL } = require("url");
const { dialog } = require("electron"); const { dialog, ipcMain } = require("electron");
const is = require("electron-is"); const is = require("electron-is");
const ytpl = require("ytpl"); const ytpl = require("ytpl");
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const { setOptions } = require("../../config/plugins"); const { setOptions } = require("../../config/plugins");
const registerCallback = 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 playingPlaylistId = undefined;
let callbackIsRegistered = false; let callbackIsRegistered = false;
module.exports = (win, options) => { module.exports = (win, options) => {
if (!callbackIsRegistered) { if (!callbackIsRegistered) {
registerCallback((info) => { ipcMain.on("video-src-changed", async (_, data) => {
metadataURL = info.url; playingPlaylistId = JSON.parse(data)?.videoDetails?.playlistId;
}); });
callbackIsRegistered = true; callbackIsRegistered = true;
} }
@ -28,17 +26,17 @@ module.exports = (win, options) => {
{ {
label: downloadLabel, label: downloadLabel,
click: async () => { click: async () => {
const currentURL = metadataURL || win.webContents.getURL(); const currentPagePlaylistId = new URL(win.webContents.getURL()).searchParams.get("list");
const playlistID = new URL(currentURL).searchParams.get("list"); const playlistId = currentPagePlaylistId || playingPlaylistId;
if (!playlistID) { if (!playlistId) {
sendError(win, new Error("No playlist ID found")); sendError(win, new Error("No playlist ID found"));
return; return;
} }
console.log(`trying to get playlist ID: '${playlistID}'`); console.log(`trying to get playlist ID: '${playlistId}'`);
let playlist; let playlist;
try { try {
playlist = await ytpl(playlistID, { playlist = await ytpl(playlistId, {
limit: options.playlistMaxItems || Infinity, limit: options.playlistMaxItems || Infinity,
}); });
} catch (e) { } catch (e) {

View File

@ -19,6 +19,8 @@ const songInfo = {
songDuration: 0, songDuration: 0,
elapsedSeconds: 0, elapsedSeconds: 0,
url: "", url: "",
videoId: "",
playlistId: "",
}; };
// Grab the native image using the src // Grab the native image using the src
@ -41,7 +43,7 @@ const handleData = async (responseText, win) => {
if (microformat) { if (microformat) {
songInfo.uploadDate = microformat.uploadDate; songInfo.uploadDate = microformat.uploadDate;
songInfo.url = microformat.urlCanonical?.split("&")[0]; songInfo.url = microformat.urlCanonical?.split("&")[0];
songInfo.playlistId = new URL(microformat.urlCanonical).searchParams.get("list");
// used for options.resumeOnStart // used for options.resumeOnStart
config.set("url", microformat.urlCanonical); config.set("url", microformat.urlCanonical);
} }
@ -54,6 +56,7 @@ const handleData = async (responseText, win) => {
songInfo.songDuration = videoDetails.lengthSeconds; songInfo.songDuration = videoDetails.lengthSeconds;
songInfo.elapsedSeconds = videoDetails.elapsedSeconds; songInfo.elapsedSeconds = videoDetails.elapsedSeconds;
songInfo.isPaused = videoDetails.isPaused; songInfo.isPaused = videoDetails.isPaused;
songInfo.videoId = videoDetails.videoId;
const oldUrl = songInfo.imageSrc; const oldUrl = songInfo.imageSrc;
songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0]; songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0];