View Single Post
  #1366  
Old 17.05.2020, 16:00
sherif011 sherif011 is offline
Super Loader
 
Join Date: Jul 2018
Posts: 27
Default

Quote:
Originally Posted by zreenmkr View Post
it is possible if you meant starting right now begin count download bytes, once bytes downloaded reach 20GB then stop all downloads.

two scripts needed:
1st one Switch - allow to Reset and Restart loaded bytes session Plus set download limit
2nd one Main - monitor loaded bytes session until limit reached

Code:
//1st
// NOTE: A SWITCH FOR: Stop downloads when target reached
// Get initial loaded bytes state and export initial bytes to txt file
// functional:  To Start AND/OR Reset and Restart Initial Loaded Bytes Count
//              also allow user to set Download Limit in GB for new session
// Trigger: 3 Options: Downloadlist Contextmenu Button Pressed | Toolbar Button Pressed | Main Menu Button Pressed
// ***NOTE: this will always Reset last session and Start a New Session


//--------------------------------------------------------
disablePermissionChecks(); //no prompting persmision dialog
//enablePermissionChecks(); //required prompting permision

//--------------------------------------------------------
//setDisableOnException(true); // enable/disable script on error exceptions
//setNotifyOnException(true); // true enable/false disable error notification on exceptions

//--------------------------------------------------------
//setAdvancedAlert(true);


beginLoadedBytesState = getPath(JD_HOME + '/tmp/_init_loaded_bytes_state.txt');
bytesTarget = getPath(JD_HOME + '/tmp/_set_download_limit_per_session.txt');

if (name == 'Start Monitor Loaded Bytes') {
    if (isDownloadControllerRunning()) {

        var loadedBytesInit = 0;

        if (beginLoadedBytesState.exists()) {
            deleteFile(beginLoadedBytesState, false);

            loadedBytesInit = _getCurrentLoadedBytesState();
            writeFile(beginLoadedBytesState, loadedBytesInit, false);

            var bytesLimit = _setDownloadBytesSessionLimit()
            var bytesLimit = bytesLimit.replace(/^\[(.*?)\]/, '$1');
            var bytesLimit = parseInt(bytesLimit.replace(/\[^0-9.]/g, ''));

            alert('New Session with Limit of: ' + bytesLimit + ' GB');
        } else {
            loadedBytesInit = _getCurrentLoadedBytesState();
            writeFile(beginLoadedBytesState, loadedBytesInit, false);
        }
    }
}

//--------------------------------------------------------
function _getCurrentLoadedBytesState() {
    var loadedBytesCount = 0;
    getAllDownloadLinks().filter(function(link) {
        return link.getBytesLoaded();
    }).forEach(function(link) {
        loadedBytesCount += link.getBytesLoaded();
    })
    return loadedBytesCount
}

//--------------------------------------------------------
function _setDownloadBytesSessionLimit() {
    var inputBox = 'notepad.exe';
    if (!bytesTarget.exists()) {
        writeFile(bytesTarget, "[20] <-- Enter New download limit (unit in GB) inside Square Brackets or Leave as it. ***NOTE: Will Reset/Restart New Session!", true);
    }
    callSync(inputBox, bytesTarget);
    return readFile(bytesTarget);
}

Code:
//2nd
// Stop downloads when target reached
// Trigger: Interval (Recommended 60000 or more)


//--------------------------------------------------------
disablePermissionChecks(); //no prompting persmision dialog
//enablePermissionChecks(); //required prompting permision

//--------------------------------------------------------
//setDisableOnException(true); // enable/disable script on error exceptions
//setNotifyOnException(true); // true enable/false disable error notification on exceptions

//--------------------------------------------------------
//setAdvancedAlert(true);



beginLoadedBytesState = getPath(JD_HOME + '/tmp/_init_loaded_bytes_state.txt');
bytesTarget = getPath(JD_HOME + '/tmp/_set_download_limit_per_session.txt');

if (isDownloadControllerRunning()) {

    // get
    if (beginLoadedBytesState.exists()) {

        var target = _getTargetBytes();
        var loadedBytesInit = readFile(beginLoadedBytesState);
        var loadedBytesCurr = _getCurrentLoadedBytesState();

        // condition
        if ((loadedBytesCurr - parseInt(loadedBytesInit)) / 1e9 > target) {
            deleteFile(beginLoadedBytesState, false);
            stopDownloads();
        }

        // debug - make sure to set interval to at least 10secs otherwise you are not going to like this alert
        //var loadedBytes = ((loadedBytesCurr - parseInt(loadedBytesInit)) / 1e9);
        //alert('init:\t' + loadedBytesInit + '\ncurr:\t' + loadedBytesCurr + '\n\r' + 'progress:\t' + loadedBytes + ' GB\ntarget:\t' + target + ' GB');
    } else {
        //Need to run 1st script to set beginLoadedBytesState & bytesTarget
        //otherwise this script does not execute anything on first run or after target met
    }
}


//--------------------------------------------------------
function _getTargetBytes() {
    if (bytesTarget.exists()) {
        var target = readFile(bytesTarget); // <- get limit set by user (in GB)
        var target = target.replace(/^\[(.*?)\]/, '$1');
        var target = parseInt(target.replace(/[^0-9.]/g, ''));
    } else {
        var target = 20; // <- Set limit (in GB) note: user input will overwrite 
    }
    return target
}


//--------------------------------------------------------
function _getCurrentLoadedBytesState() {
    var loadedBytesCount = 0;
    getAllDownloadLinks().filter(function(link) {
        return link.getBytesLoaded();
    }).forEach(function(link) {
        loadedBytesCount += link.getBytesLoaded();
    })
    return loadedBytesCount
}
Thank you, but I'm not sure what trigger I should be using for the 1st script, do I have to add and configure a new button? And what if I need to change the limit, do I have to change it in both scripts?

Last edited by sherif011; 17.05.2020 at 16:09.
Reply With Quote