Add Maximum active downloads option

pull/90/head
aandrew-me 3 years ago
parent 724ac2884c
commit a716eac90e

@ -14,7 +14,7 @@ h1{
#version{ #version{
margin:5px; margin:5px;
} }
input[type="text"]{ input[type="text"], .input{
border-radius: 5px; border-radius: 5px;
padding:5px; padding:5px;
outline: none; outline: none;
@ -23,6 +23,10 @@ input[type="text"]{
height:35px; height:35px;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
} }
.input{
width:70px;
font-size: larger;
}
.prefBox, #pathConfig{ .prefBox, #pathConfig{
display:flex; display:flex;

@ -115,10 +115,6 @@
<h3 id="subHeader">Subtitles</h3> <h3 id="subHeader">Subtitles</h3>
<span id="subTxt">Download subtitles if available</span> <span id="subTxt">Download subtitles if available</span>
<input id="subChecked" type="checkbox"> <input id="subChecked" type="checkbox">
<!-- <br><br>
<span>Download auto generated subtitles</span>
<input id="autoSubChecked" type="checkbox"> -->
</div> </div>
<!-- Extraction options start --> <!-- Extraction options start -->
@ -143,11 +139,10 @@
</div> </div>
<br><br><br><br> <br><br>
<!-- Downloads list --> <!-- Downloads list -->
<div id="list"> <div id="list"></div>
</div>
<div id="goToTop"></div> <div id="goToTop"></div>

@ -149,9 +149,11 @@
<button class="redBtn" id="resetFoldernameFormat">Reset to default</button> <button class="redBtn" id="resetFoldernameFormat">Reset to default</button>
</div> </div>
<script> <br>
<div class="prefBox">
</script> <span id="maxTxt">Maximum number of active downloads</span>
<input type="number" min="1" class="input" id="maxDownloads" value="5">
</div>
</body> </body>
</html> </html>

@ -172,5 +172,21 @@ getId("resetFoldernameFormat").addEventListener("click", () => {
localStorage.setItem("foldernameFormat", "%(playlist_title)s"); localStorage.setItem("foldernameFormat", "%(playlist_title)s");
}); });
// Max active downloads
getId("maxDownloads").addEventListener("input", ()=>{
const number = Number(getId("maxDownloads").value)
if (number < 1){
localStorage.setItem("maxActiveDownloads", 1)
}
else{
localStorage.setItem("maxActiveDownloads", number)
}
})
if (localStorage.getItem("maxActiveDownloads")){
getId("maxDownloads").value = localStorage.getItem("maxActiveDownloads")
}
// Translation file // Translation file
require("../src/translate_preferences"); require("../src/translate_preferences");

@ -29,10 +29,21 @@ let title, onlyvideo, id, thumbnail, ytdlp, duration, extractFormat;
let rangeCmd = ""; let rangeCmd = "";
let subs = ""; let subs = "";
let subLangs; let subLangs;
// let autoSubs = ""
let rangeOption = "--download-sections"; let rangeOption = "--download-sections";
let cookieArg = ""; let cookieArg = "";
let browser = ""; let browser = "";
let maxActiveDownloads = 5;
if (localStorage.getItem("maxActiveDownloads")){
const number = Number(localStorage.getItem("maxActiveDownloads"))
if (number < 1){
maxActiveDownloads = 1
}
else{
maxActiveDownloads = number
}
}
let currentDownloads = 0;
let controllers = new Object;
// Video and audio preferences // Video and audio preferences
let preferredVideoQuality = ""; let preferredVideoQuality = "";
@ -439,13 +450,68 @@ async function getInfo(url) {
// Video download event // Video download event
getId("videoDownload").addEventListener("click", (event) => { getId("videoDownload").addEventListener("click", (event) => {
getId("hidden").style.display = "none"; getId("hidden").style.display = "none";
download("video"); console.log(`Current:${currentDownloads} Max:${maxActiveDownloads}`);
if (currentDownloads < maxActiveDownloads) {
download("video");
currentDownloads++;
} else {
const randId = Math.random().toFixed(10).toString().slice(2);
const item = `
<div class="item" id="${randId}">
<img src="${thumbnail}" alt="No thumbnail" class="itemIcon" crossorigin="anonymous">
<div class="itemBody">
<div class="itemTitle">${title}</div>
<div class="itemType">${i18n.__("Video")}</div>
<p>${i18n.__("Download pending")}</p>
</div>
</div>
`;
getId("list").innerHTML += item;
const interval = setInterval(() => {
if (currentDownloads < maxActiveDownloads) {
getId(randId).remove()
download("video");
currentDownloads++;
clearInterval(interval);
}
}, 2000);
}
}); });
// Audio download event // Audio download event
getId("audioDownload").addEventListener("click", (event) => { getId("audioDownload").addEventListener("click", (event) => {
getId("hidden").style.display = "none"; getId("hidden").style.display = "none";
download("audio"); console.log(`Current:${currentDownloads} Max:${maxActiveDownloads}`);
if (currentDownloads < maxActiveDownloads) {
download("audio");
currentDownloads++;
} else {
const randId = Math.random().toFixed(10).toString().slice(2);
const item = `
<div class="item" id="${randId}">
<img src="${thumbnail}" alt="No thumbnail" class="itemIcon" crossorigin="anonymous">
<div class="itemBody">
<div class="itemTitle">${title}</div>
<div class="itemType">${i18n.__("Video")}</div>
<p>${i18n.__("Download pending")}</p>
</div>
</div>
`;
getId("list").innerHTML += item;
const interval = setInterval(() => {
if (currentDownloads < maxActiveDownloads) {
getId(randId).remove()
download("video");
currentDownloads++;
clearInterval(interval);
}
}, 2000);
}
}); });
getId("extractBtn").addEventListener("click", () => { getId("extractBtn").addEventListener("click", () => {
@ -557,14 +623,13 @@ function manageAdvanced(duration) {
////////////////////////////// //////////////////////////////
function download(type) { function download(type) {
let willBeSaved = true;
manageAdvanced(duration); manageAdvanced(duration);
const url = getId("url").value; const url = getId("url").value;
let ext; let ext;
let extractExt; let extractExt;
let format_id; let format_id;
const randomId = Math.random().toFixed(10).toString().slice(2); const randomId = "a" + Math.random().toFixed(10).toString().slice(2);
if (type === "video") { if (type === "video") {
const videoValue = getId("videoFormatSelect").value; const videoValue = getId("videoFormatSelect").value;
@ -658,6 +723,9 @@ function download(type) {
} }
const controller = new AbortController(); const controller = new AbortController();
controllers[randomId] = controller;
console.log(rangeOption + " " + rangeCmd); console.log(rangeOption + " " + rangeCmd);
if (type === "video" && onlyvideo) { if (type === "video" && onlyvideo) {
@ -735,7 +803,6 @@ function download(type) {
} }
getId(randomId + ".close").addEventListener("click", () => { getId(randomId + ".close").addEventListener("click", () => {
willBeSaved = false;
controller.abort(); controller.abort();
}); });
@ -765,8 +832,10 @@ function download(type) {
.once("ytDlpEvent", (eventType, eventData) => { .once("ytDlpEvent", (eventType, eventData) => {
getId(randomId + "prog").textContent = i18n.__("Downloading..."); getId(randomId + "prog").textContent = i18n.__("Downloading...");
}) })
.once("close", () => { .once("close", (code) => {
if (willBeSaved) { console.log("Closed with code " + code);
if (code == 0) {
currentDownloads--;
// const items = JSON.parse(localStorage.getItem("itemList")); // const items = JSON.parse(localStorage.getItem("itemList"));
// // Clearing item from localstorage // // Clearing item from localstorage
// for (let item of items) { // for (let item of items) {
@ -801,6 +870,7 @@ function download(type) {
} }
}) })
.once("error", (error) => { .once("error", (error) => {
currentDownloads--;
getId(randomId + "prog").textContent = i18n.__( getId(randomId + "prog").textContent = i18n.__(
"Some error has occurred. Hover to see details" "Some error has occurred. Hover to see details"
); );
@ -812,6 +882,8 @@ function download(type) {
// Removing item // Removing item
function fadeItem(id) { function fadeItem(id) {
controllers[id].abort()
currentDownloads --;
let count = 0; let count = 0;
let opacity = 1; let opacity = 1;
const fade = setInterval(() => { const fade = setInterval(() => {

Loading…
Cancel
Save