Jdownloader 2 Event Scripter, Connecting to VPN When Download Speeds Are Low

166 views Asked by At

I'm trying to write a script that detects when the download speed is below a certain threshold (in this case, 90kb/s), and then launches a batch file that connects to a random VPN network. The batch file is working perfectly, but this event script is not. It ignores the wait time, and launches the batch file even if there are no files downloading, and when there are multiple files downloading that don't match the criteria.

// Function to wait for a download to start
function waitForDownloadStart(downloadLink, timeoutMs) {
    var startTime = new Date().getTime();
    
    while (!downloadLink.isStarted() && (new Date().getTime() - startTime) < timeoutMs) {
        // Wait for the download to start
    }
    
    return downloadLink.isStarted();
}

// Flag to indicate whether there is any download over 90kb/s
var anyDownloadOver90KBps = false;

// Check if downloads are running at all
if (isDownloadControllerRunning() && !isDownloadControllerStopping()) {
    var running = getRunningDownloadLinks();
    
    // Loop through all running downloads
    for (var i = 0; i < running.length; i++) {
        var downloadLink = running[i];
        
        // Wait for the download to start (timeout of 30 seconds)
        var downloadStarted = waitForDownloadStart(downloadLink, 30000);
        
        if (downloadStarted && downloadLink.getDownloadDuration() > 30000) {
            // Check if the current speed is below 90kb/s
            if (downloadLink.getSpeed() >= 90 * 1024) {
                // Set the flag to indicate there's a download over 90kb/s
                anyDownloadOver90KBps = true;
                break; // No need to check further, we found one.
            }
        }
    }
}

// Only proceed if there are no downloads over 90kb/s
if (!anyDownloadOver90KBps) {
    var vpnreset = "C:/Program Files/NordVPN/nordvpn_reconnect.bat";
    callSync(vpnreset);
}
0

There are 0 answers