View Single Post
  #6  
Old 29.09.2016, 14:01
mgpai mgpai is offline
Script Master
 
Join Date: Sep 2013
Posts: 505
Default

Split packages and create sub-folders

Adding custom command to context menu
  1. Add script to event scripter
  2. Go to Linkgrabber Tab > Context Menu > Open Menu Manager
  3. Add Submenu > Name it "Scripts" (can specify any name)
  4. Select "Scripts" Submenu > Add Action > EventScripter Trigger
  5. In the right-side panel change the name from "EventScripter Trigger" to "Split Packages" (Exact name specified in the script, case-sensitive)
  6. Save
Code:
// Split Packages and create sub-folder based on package length
// Trigger : Linkgrabber Contextmenu Button Pressed
// Forum Topic: https://board.jdownloader.org/showthread.php?t=70979

if (name == "Split Packages") {
    var package = lgSelection.getContextPackage();
    var links = package.getDownloadLinks();
    var newSize = 100; // Specify max number of links per package
    var UUIDS = [];
    var count = 1;

    if (links.length > newSize) {
        for (i = 0; i < links.length; i++) {
            var link = links[i];
            UUIDS.push(link.getUUID());
        }

        while (UUIDS.length) {
            var oldFolder = package.getDownloadFolder();
            var packageName = package.getName();
            var splitUUIDS = UUIDS.splice(0, newSize);
            var newFolder = oldFolder + "/" + pad(count);
            setNewFolder();
            count++;
        }
    }
}

// Add sub folder
function setNewFolder() {
    callAPI("linkgrabberv2", "movetoNewPackage", splitUUIDS, [], packageName, newFolder);
}

// Pad number with leading zeros
function pad(num) {
    var a = num + "";
    var b = (links.length / newSize).toFixed(0);
    while (a.length < b.length) a = "0" + a;
    return a;
}
Reply With Quote