TorrentBD-Torrent Downloader

A Scripts that donwload torrent file from torrentBD. Just Config the UserConfig you are done.

// ==UserScript==
// @name        TorrentBD-Torrent Downloader
// @namespace   Violentmonkey Scripts
// @match       https://www.torrentbd.net/*
// @match       https://www.torrentbd.com/*
// @match       https://www.torrentbd.org/*
// @icon        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAABaCAYAAAA4qEECAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAkASURBVHhe7dxrcFTlGQfw//Oec/aSGzGsgHZ00QIbLpXlUsfLsFAqWNAWxxl7sbYsCFKkDu10ZJyp0+kXe4GhdCilpSKstlXH2qGCExnscFmmpRdGlgFJFpWQikBTVi6RZDd7zvv0QxIa35JkL9ndl7i/DzD8n5O9/LN7zuHs2QOUlZWVlZWVlZWVlWWL1EAndy5cbR14fhWmPfS0L3khUX353Gk2LDdV3zg6YKc6PE4yycwS6Y42gBnCcsN0VwBguKtrhbTTZy68/85pV0WNqB51s7z84dnTqbY2554nN9rPPTxKqvdXSCUvun7W4hoyjJFsO6NIiKBkOZKAsdJOu4TpCjjpFBmW63pmrmbHYRAghOlmMMDcdSM9f9OVP0BCgKUES5kEEQnDlNJOfwAiaViui3aqo9l0ey/aqY4m01PRwo59wnR7/216K8/F/rS683+PcHAUrehpDz1dnWq7cJOdSn6apTMBzOPAPBYEP4A6MKpABICL+bC6MQCSAH8I4DRAzcx8jIRoNF2eZjLNd257YFnri8smdf9Gs1ewZzRx7vKR0nGmSDsdJKK7mXk8mD8FwKMuq68rv/RWEJ0A8HcS4i1hmAfd1bXHD237qa3+RF8Grejb5q+8wUl33i6d9N0s5Z0s5TgAI9TlhogOgBsB+qdhud4C0T7DW3HiyI51aXXBHjkXfcfDPxIXzzZPAXAfO84MZjkZwPXqcp8QklmuOb7/hafUQY+si57w+cf80nEeYeksADAVgKEu80kkTGt94+5nV6p5j4yLnnTvihF2qmMVS7kYwHXq/JONQ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5H1aqg7LYtGV9m3dO9r995IfisejWzq9e9rhjbraFWiJXbB5w++CiAAYAyAJfFoRMuLBw4JgVDYCITCk9W8rKysrKxssPwX6ewLVs3KSMoAAAAASUVORK5CYII=
// @version     1.1
// @run-at      document-end
// @author      TheMyth
// @license MIT
// @description A Scripts that donwload torrent file from torrentBD. Just Config the UserConfig you are done.
// ==/UserScript==
const UserConfig = {
  max: null, // [number,"kib"| "kb"|"mib"|"mb"|"gib"|"gb"|"tib"|"tb"] or null
  min: [100, "mb"], // [number,"kib"| "kb"|"mib"|"mb"|"gib"|"gb"|"tib"|"tb"] or null
  downloadLimit: null, //number or null
  sumDownloadSize: [1, "gb"], // [number,"kib"| "kb"|"mib"|"mb"|"gib"|"gb"|"tib"|"tb"] or null
};
let sumDownlodSize = 0.0;
let DownloadedTorrents = 0;

let Config = (function () {
  const t_c = {};
  /**
   *
   * @param {Array.<string,number>} x
   * @returns {Float}
   */
  function converter(x) {
    if (["kib", "kb"].includes(String(x[1]).toLowerCase())) {
      return parseFloat(x[0]);
    } else if (["mib", "mb"].includes(String(x[1]).toLowerCase())) {
      return parseFloat(x[0]) * 1024;
    } else if (["gib", "gb"].includes(String(x[1]).toLowerCase())) {
      return parseFloat(x[0]) * 1024 * 1024;
    } else if (["tib", "tb"].includes(String(x[1]).toLowerCase())) {
      return parseFloat(x[0]) * 1024 * 1024 * 1024;
    } else {
      return null;
    }
  }
  if (UserConfig.max) {
    t_c.max = converter(UserConfig.max);
  } else {
    t_c.max = null;
  }
  if (UserConfig.min) {
    t_c.min = converter(UserConfig.min);
  } else {
    t_c.min = null;
  }
  if (UserConfig.sumDownloadSize) {
    t_c.sumDownloadSize = converter(UserConfig.sumDownloadSize);
  } else {
    t_c.sumDownloadSize = null;
  }
  t_c.downloadLimit = UserConfig.downloadLimit;
  console.log(`User Config:`, t_c);
  console.log("Last History:",getLocal());
  return t_c;
})();

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
function setLocal(object) {
  localStorage.setItem("endof", JSON.stringify(object));
  alert(`Download Done!\nTotalDownloads:${object.DownloadedTorrents}\nLatTorrentName:${object.lastTorrentName}\nTotalFileSize:${object.sumDownlodSize}`)
}

function getLocal() {
  if (localStorage.getItem("endof")) {
    return JSON.parse(localStorage.getItem("endof"));
  } else {
    return null;
  }
}

/**
 *
 * @param {HTMLElement} element
 */
function getTheFileSize(element) {
  let el_TextContent = element
    .querySelector('div[title="File Size"')
    .textContent.split(" ");
  if (el_TextContent[2].toLowerCase() == "kib") {
    return parseFloat(el_TextContent[1]);
  } else if (el_TextContent[2].toLowerCase() == "mib") {
    return parseFloat(el_TextContent[1]) * 1024;
  } else if (el_TextContent[2].toLowerCase() == "gib") {
    return parseFloat(el_TextContent[1]) * 1024 * 1024;
  } else if (el_TextContent[2].toLowerCase() == "tib") {
    return parseFloat(el_TextContent[1]) * 1024 * 1024 * 1024;
  }
}

async function getTorrentData() {
  while (true) {
    /**
     *
     * @param {HTMLElement} element
     * @param {Float} fileSize
     */
    async function downloadHelper(element, fileSize) {
      let title = element.querySelector("td:nth-child(2) a");
      let dwnLink = element.querySelector("td:nth-child(3) a");
      sumDownlodSize += fileSize;
      DownloadedTorrents += 1;
      dwnLink.click();
      console.log(`${DownloadedTorrents}:${title.textContent}`);
    }
    let tableBody = document.querySelector(
      "#kuddus-results-container > table > tbody"
    );
    let lastTorrentName = "";
    for (let index = 0; index < tableBody.children.length; index++) {
      const element = tableBody.children[index];

      let fileSize = getTheFileSize(element);
      if (Config.max && Config.min) {
        if (fileSize >= Config.min && fileSize <= Config.max) {
          downloadHelper(element, fileSize);
          await sleep(2000);
        }
      } else if (Config.max && fileSize <= Config.max) {
        downloadHelper(element, fileSize);
        await sleep(2000);
      } else if (Config.min && fileSize >= Config.min) {
        downloadHelper(element, fileSize);
        await sleep(2000);
      }

      if (Config.downloadLimit && Config.downloadLimit == DownloadedTorrents) {
        lastTorrentName =
          element.querySelector("td:nth-child(2) a").textContent;
        break;
      } else if (
        Config.sumDownloadSize &&
        sumDownlodSize >= Config.sumDownloadSize
      ) {
        lastTorrentName =
          element.querySelector("td:nth-child(2) a").textContent;
        break;
      } else if (
        !Config.sumDownloadSize &&
        !Config.downloadLimit &&
        DownloadedTorrents == 100
      ) {
        lastTorrentName =
          element.querySelector("td:nth-child(2) a").textContent;
        break;
      }
    }
    console.log(sumDownlodSize);
    console.log(DownloadedTorrents);
    let next = document.querySelector('li[title="Next page"]');
    if (Config.downloadLimit && Config.downloadLimit == DownloadedTorrents) {
      setLocal({
        sumDownlodSize: sumDownlodSize,
        DownloadedTorrents: DownloadedTorrents,
        page: parseInt(next.getAttribute("data-paginate-to")) - 1,
        lastTorrentName: lastTorrentName,
      });
      break;
    } else if (
      Config.sumDownloadSize &&
      sumDownlodSize >= Config.sumDownloadSize
    ) {
      setLocal({
        sumDownlodSize: sumDownlodSize,
        DownloadedTorrents: DownloadedTorrents,
        page: parseInt(next.getAttribute("data-paginate-to")) - 1,
        lastTorrentName: lastTorrentName,
      });
      break;
    } else if (
      !Config.sumDownloadSize &&
      !Config.downloadLimit &&
      DownloadedTorrents == 100
    ) {
      setLocal({
        sumDownlodSize: sumDownlodSize,
        DownloadedTorrents: DownloadedTorrents,
        page: parseInt(next.getAttribute("data-paginate-to")) - 1,
        lastTorrentName: lastTorrentName,
      });
      break;
    }

    if (!next) {
      break;
    } else {
      next.click();
      await sleep(2000);
    }
  }
}

async function dwnBtnFunc() {

  let resultContainer = document.querySelector("#kuddus-results-container");
  if (resultContainer.childNodes.length == 0) {
    alert("No Torrent Found on th page");
  } else if (resultContainer.childNodes.length > 0) {
    sumDownlodSize = 0.0;
    DownloadedTorrents = 0;
    getTorrentData();
  }
}

let panel = document.querySelector("#kuddus-wrapper");

const dwnBtn = document.createElement("button");
dwnBtn.textContent = "DownloadStart";
dwnBtn.setAttribute("align", "center");
dwnBtn.setAttribute("title", "DownloadStart");
dwnBtn.style =
  "position: fixed; top: -6px; left: 70px; border-radius: 4px; margin: 6px 8px; padding: 6px 14px; border: none; opacity: 0.5;";
dwnBtn.addEventListener("click", dwnBtnFunc);

if (panel.children.length > 0) {
  // Check if panel has children to avoid errors
  panel.insertBefore(dwnBtn, panel.children[panel.children.length - 1]);
} else {
  panel.appendChild(dwnBtn); // If panel has no children, just append it
}