From 810468bff0b6a6f973ac9837f43b6475e22f5263 Mon Sep 17 00:00:00 2001 From: aandrew-me Date: Sat, 1 Oct 2022 18:48:25 +0600 Subject: [PATCH] Add support to use cookies --- assets/css/extra.css | 3 ++ html/preferences.html | 27 ++++++++++- package.json | 2 +- src/playlist.js | 91 ++++++++++++++++++++++-------------- src/renderer.js | 21 ++++++++- src/translate_preferences.js | 4 +- translations/en.json | 4 +- 7 files changed, 111 insertions(+), 41 deletions(-) diff --git a/assets/css/extra.css b/assets/css/extra.css index 07d10bd..c56dbc5 100644 --- a/assets/css/extra.css +++ b/assets/css/extra.css @@ -94,4 +94,7 @@ select { font-size: large; margin: 8px; outline: none; +} +#browserInfo{ + cursor: pointer; } \ No newline at end of file diff --git a/html/preferences.html b/html/preferences.html index 5f423d4..a303209 100644 --- a/html/preferences.html +++ b/html/preferences.html @@ -47,6 +47,20 @@ +
+ Select browser to use cookies from + ℹ️ + diff --git a/package.json b/package.json index 2698589..406117f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "yt-dlp-wrap-extended": "^2.3.12" }, "name": "ytdownloader", - "version": "3.5.3", + "version": "3.6.0", "main": "main.js", "scripts": { "start": "electron .", diff --git a/src/playlist.js b/src/playlist.js index 9cdf96c..28b1056 100644 --- a/src/playlist.js +++ b/src/playlist.js @@ -7,6 +7,8 @@ const ytDlp = localStorage.getItem("ytdlp"); const ytdlp = new YTDlpWrap(ytDlp); const downloadDir = localStorage.getItem("downloadPath"); const i18n = new (require("../translations/i18n"))(); +let cookieArg = ""; +let browser = ""; function getId(id) { return document.getElementById(id); @@ -17,7 +19,6 @@ function pasteLink() { getId("link").textContent = " " + url; getId("options").style.display = "block"; getId("incorrectMsg").textContent = ""; - } getId("pasteLink").addEventListener("click", () => { @@ -35,20 +36,30 @@ const playlistTxt = "Downloading playlist: "; const videoIndex = "Downloading video "; getId("download").addEventListener("click", () => { + // Whether to use browser cookies or not + if (localStorage.getItem("browser")) { + browser = localStorage.getItem("browser"); + } + if (browser) { + cookieArg = "--cookies-from-browser"; + } else { + cookieArg = ""; + } let count = 0; let playlistName; const date = new Date(); - const today = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate(); + const today = + date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(); let playlistDirName = "Playlist_" + today; // Opening folder - let folderLocation = path.join(downloadDir, playlistDirName) - if (platform() == "win32"){ + let folderLocation = path.join(downloadDir, playlistDirName); + if (platform() == "win32") { folderLocation = folderLocation.split(path.sep).join("\\\\"); } getId("options").style.display = "none"; getId("pasteLink").style.display = "none"; - getId("playlistName").textContent = i18n.__("Processing") + "..." + getId("playlistName").textContent = i18n.__("Processing") + "..."; const quality = getId("select").value; const format = `"mp4[height<=${quality}]+m4a/mp4[height<=${quality}]/bv[height<=${quality}]+ba/best[height<=${quality}]/best"`; const controller = new AbortController(); @@ -64,6 +75,8 @@ getId("download").addEventListener("click", () => { playlistDirName, "%(playlist_index)s.%(title)s.%(ext)s" )}"`, + cookieArg, + browser, `"${url}"`, ], @@ -75,8 +88,9 @@ getId("download").addEventListener("click", () => { console.log(eventData); if (eventData.includes(playlistTxt)) { - playlistName = eventData.split(":")[1].slice(1) - getId("playlistName").textContent = i18n.__("Downloading playlist:") + " "+ playlistName; + playlistName = eventData.split(":")[1].slice(1); + getId("playlistName").textContent = + i18n.__("Downloading playlist:") + " " + playlistName; console.log(playlistName); } @@ -85,12 +99,16 @@ getId("download").addEventListener("click", () => { const itemTitle = i18n.__("Video") + " " + eventData.split(" ")[3]; if (count > 1) { - getId(`p${count - 1}`).textContent = i18n.__("File saved. Click to Open") + getId(`p${count - 1}`).textContent = i18n.__( + "File saved. Click to Open" + ); } const item = `

${itemTitle}

-

${i18n.__("Downloading...")}

+

${i18n.__( + "Downloading..." + )}

`; getId("list").innerHTML += item; } @@ -98,40 +116,41 @@ getId("download").addEventListener("click", () => { downloadProcess.on("progress", (progress) => { if (getId(`p${count}`)) { - getId(`p${count}`).textContent = `${i18n.__("Progress")} ${progress.percent}% | ${i18n.__("Speed")} ${progress.currentSpeed || 0}` + getId(`p${count}`).textContent = `${i18n.__("Progress")} ${ + progress.percent + }% | ${i18n.__("Speed")} ${progress.currentSpeed || 0}`; } }); downloadProcess.on("error", (error) => { - getId("pasteLink").style.display = "inline-block" + getId("pasteLink").style.display = "inline-block"; getId("options").style.display = "block"; - getId("playlistName").textContent = "" - getId("incorrectMsg").textContent = i18n.__("Some error has occured. Check your network and use correct URL"); - getId("incorrectMsg").title = error + getId("playlistName").textContent = ""; + getId("incorrectMsg").textContent = i18n.__( + "Some error has occured. Check your network and use correct URL" + ); + getId("incorrectMsg").title = error; }); - downloadProcess.on("close", ()=>{ - getId(`p${count}`).textContent = i18n.__("File saved. Click to Open") + downloadProcess.on("close", () => { + getId(`p${count}`).textContent = i18n.__("File saved. Click to Open"); getId("pasteLink").style.display = "inline-block"; const notify = new Notification("ytDownloader", { body: i18n.__("Playlist downloaded"), icon: "../assets/images/icon.png", - }) - - notify.onclick = () =>{ - openFolder(folderLocation) - } + }); - }) + notify.onclick = () => { + openFolder(folderLocation); + }; + }); }); - -function openFolder(location){ - shell.openPath(location) +function openFolder(location) { + shell.openPath(location); } - function closeMenu() { getId("menuIcon").style.transform = "rotate(0deg)"; menuIsOpen = false; @@ -157,16 +176,18 @@ getId("aboutWin").addEventListener("click", () => { closeMenu(); ipcRenderer.send("load-page", __dirname + "/about.html"); }); -getId("homeWin").addEventListener("click", ()=>{ +getId("homeWin").addEventListener("click", () => { closeMenu(); ipcRenderer.send("load-win", __dirname + "/index.html"); -}) +}); // Translations -getId("pasteLink").textContent = i18n.__("Click to paste playlist link from clipboard [Ctrl + V]") -getId("preferenceWin").textContent = i18n.__("Preferences") -getId("aboutWin").textContent = i18n.__("About") -getId("homeWin").textContent = i18n.__("Homepage") -getId("linkTitle").textContent = i18n.__("Link:") -getId("videoFormat").textContent = i18n.__("Select Format ") -getId("download").textContent = i18n.__("Download") \ No newline at end of file +getId("pasteLink").textContent = i18n.__( + "Click to paste playlist link from clipboard [Ctrl + V]" +); +getId("preferenceWin").textContent = i18n.__("Preferences"); +getId("aboutWin").textContent = i18n.__("About"); +getId("homeWin").textContent = i18n.__("Homepage"); +getId("linkTitle").textContent = i18n.__("Link:"); +getId("videoFormat").textContent = i18n.__("Select Format "); +getId("download").textContent = i18n.__("Download"); diff --git a/src/renderer.js b/src/renderer.js index 42f2c10..51022a1 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -31,6 +31,8 @@ let subs = ""; let subLangs; // let autoSubs = "" let rangeOption = "--download-sections"; +let cookieArg = "" +let browser = "" function getId(id) { return document.getElementById(id); @@ -199,10 +201,21 @@ async function getInfo(url) { getId("startTime").value = ""; getId("endTime").value = ""; + // Whether to use browser cookies or not + if (localStorage.getItem("browser")){ + browser = localStorage.getItem("browser") + } + if(browser){ + cookieArg = "--cookies-from-browser" + } + else{ + cookieArg = "" + } + let validInfo = true; let info = ""; - const infoProcess = cp.spawn(ytDlp, ["-j", "--no-playlist", `"${url}"`], { + const infoProcess = cp.spawn(ytDlp, ["-j", "--no-playlist", cookieArg, browser, `"${url}"`], { shell: true, }); @@ -600,6 +613,8 @@ function download(type) { subs, subLangs, "--no-playlist", + cookieArg, + browser, `"${url}"`, ], { shell: true, detached: false }, @@ -623,6 +638,8 @@ function download(type) { "--ffmpeg-location", ffmpeg, "--no-playlist", + cookieArg, + browser, `"${url}"`, ], { shell: true, detached: false }, @@ -644,6 +661,8 @@ function download(type) { subs, subLangs, "--no-playlist", + cookieArg, + browser, `"${url}"`, ], { shell: true, detached: false }, diff --git a/src/translate_preferences.js b/src/translate_preferences.js index 9922dfd..ee59c96 100644 --- a/src/translate_preferences.js +++ b/src/translate_preferences.js @@ -15,4 +15,6 @@ getId("selectLocation").textContent = i18n.__("Select Download Location") getId("transparentText").textContent = i18n.__("Enable transparent dark mode(only Linux, needs relaunch)") getId("preferences").textContent = i18n.__("Preferences") getId("selectLn").textContent = i18n.__("Select Language (Requires relaunch)") -// getId("restart").textContent = i18n.__("Restart app") \ No newline at end of file +// getId("restart").textContent = i18n.__("Restart app") +getId("browserInfo").title = i18n.__("This option lets you download restricted content. You will get errors if cookies are not there") +getId("browserTxt").textContent = i18n.__("Select browser to use cookies from") \ No newline at end of file diff --git a/translations/en.json b/translations/en.json index 74aa720..a545f99 100644 --- a/translations/en.json +++ b/translations/en.json @@ -53,5 +53,7 @@ "Link:": "Link:", "Downloading playlist:": "Downloading playlist:", "Download playlist":"Download playlist", - "Playlist downloaded":"Playlist downloaded" + "Playlist downloaded":"Playlist downloaded", + "This option lets you download restricted content. You will get errors if cookies are not there":"This option lets you download restricted content. You will get errors if cookies are not there", + "Select browser to use cookies from":"Select browser to use cookies from" }