View Single Post
  #8  
Old 14.10.2019, 02:17
Demongornot Demongornot is offline
JD Beta
 
Join Date: Sep 2019
Location: Universe, Local group, Milky Way, Solar System, Earth, France
Posts: 50
Default

That's an awesome code !
I discovered the "getChildren" method only after I posted the comment, because in my code I didn't created the "History" folder and it didn't work at first, so looking for a folder creating method I also discovered the getChildren, I don't know why but I always miss the "FilePath" methods part in the help...
Most of the time I search for methods using ctrl+F search function, but the names aren't necessarily what I expected them to be, so...
Thanks you for this code anyway, I know this is just to show me and I highly appreciate this !

I fixed my code, for the moment, when a download finished and the "History" folder doesn't exist, on mode 0 and with a higher than 0 number of max downloads, I fixed 10 things.

Code:
Line 66, use ".isFinished()" on an URL rather than on a link, changed from "if (!(linkURL.isFinished())) continue;" to "if (!(AllDownloadLinks[counter].isFinished())) continue;"
Line 76, hostIndex if it was at value -1 stayed at -1 and created an error with the .push function, code added at line 75 "hostIndex = hostsList.length - 1;"
Line 101 - 104, Forgot to increment counter3, added at line 102 "counter3++"
Line 84, misunderstood the writeFile command, thought it created dirrectory too, added at line 80 "path.mkdirs();"
Line 57, changed "var fileText;" to "var fileText = ''" as it wrote "undefined" on the text file otherwise
Line 93, forgot to reset variable fileCounter to 0, creating files with increasing numbers regardless of the host, added at line 93 "fileCounter = 0;"
Line 96, Move "fileCounter++" to after its use as its value should start being used at 0 and not 1, line 98/99 moved from line 96 "fileCounter++;"
Line 119, forgot to change "link" to "DownloadLink" changed at line 119 from "link.getPluginURL();" to "DownloadLink.getPluginURL();"
Line 91, if condition prevented last file to be saved, added a condition at line 91 from "if (counter3 >= MAX_URLS_PER_FILE)" to "if (counter3 >= MAX_URLS_PER_FILE || counter2 == urlsList[counter].length - 1)"
Line 102 103, moved both code to add url to the file list and the new line code before the file creating "if" where it should be, lines 94 and 95 from lines 102 and 103 "fileText += interUrl + urlsList[counter][counter2];" and "interUrl = nl;"
And the new code is :
Code:
const MAX_URLS_PER_FILE = 50; //0 = no limits, I advise setting it to 0 or between 1000 (100 if you don't download a lot) and 100 000.
const FILE_SAVE_MODE = 0; //0 = Short URL with host per file(s); 1 = Long URL with host per file(s); 2 = Long URL on a single file.
const BASEPATH = JD_HOME + '\\History';

if (link.isFinished()) {
    var myDownloadLink = link;
    var path = getPath(BASEPATH);
    var nl = getEnvironment().getNewLine();
    var separator = ' ';
    if (path.exists()) {
        var linkHost = myDownloadLink.getDownloadHost();
        var linkURL = getShortURL(myDownloadLink);
        var fileText = linkURL;
        var filePath;
        var arrayText = [];
        var pass = true;
        if (FILE_SAVE_MODE > 0 || (FILE_SAVE_MODE == 0 && MAX_URLS_PER_FILE == 0)) {
            if (FILE_SAVE_MODE == 2) filePath = getPath(path + '\\History.txt');
            if (FILE_SAVE_MODE < 2) filePath = getPath(path + '\\' + linkHost + separator + '0.txt');
            if (filePath.exists()) {
                arrayText = readFile(filePath).split(nl);
                if (arrayText != null) {
                    if (arrayText.indexOf(linkURL) >= 0) pass = false;
                    fileText = nl + linkURL;
                }
            }
        } else {
            var fileCounter = 0;
            var previousLength = 0;
            var loopContinue = true;
            while (loopContinue) {
                filePath = getPath(path + '\\' + linkHost + separator + fileCounter + '.txt');
                if (filePath.exists()) {
                    arrayText = readFile(filePath).split(nl);
                    previousLength = arrayText.length;
                    if (arrayText.indexOf(linkURL) >= 0) {
                        loopContinue = false;
                        pass = false;
                    }
                    fileCounter++;
                } else {
                    if (fileCounter > 0 && previousLength < MAX_URLS_PER_FILE) {
                        fileCounter--;
                        fileText = nl + linkURL;
                    }
                    loopContinue = false;
                }
            }
            if (pass) {
                if (filePath.exists()) linkURL = nl + linkURL;
                writeFile(filePath, fileText, true);
            }
        }
    } else {
        var AllDownloadLinks = getAllDownloadLinks();
        if (AllDownloadLinks.length > 0) {
            var fileText = "";
            var hostsList = [];
            var urlsList = [];
            var linkURL;
            var linkHost;
            var hostIndex;
            var counter;
            for (counter = 0; counter < AllDownloadLinks.length; counter++) {
                linkURL = getShortURL(AllDownloadLinks[counter]);
                if (!(AllDownloadLinks[counter].isFinished())) continue;
                if (FILE_SAVE_MODE > 1) {
                    urlsList.push(linkURL);
                } else {
                    linkHost = AllDownloadLinks[counter].getDownloadHost();
                    hostIndex = hostsList.indexOf(linkHost);
                    if (hostIndex < 0) {
                        hostsList.push(linkHost);
                        urlsList.push([]);
                        hostIndex = hostsList.length - 1;
                    }
                    urlsList[hostIndex].push(linkURL);
                }
            }
            if (urlsList.length > 0) {
                path.mkdirs();
                var filePath;
                if (FILE_SAVE_MODE > 1) {
                    fileText = urlsList.join(nl);
                    filePath = getPath(path + '\\History.txt');
                    writeFile(filePath, fileText, true);
                } else {
                    var counter2;
                    var counter3;
                    var fileCounter = 0;
                    var interUrl = '';
                    for (counter = 0; counter < urlsList.length; counter++) {
                        if (FILE_SAVE_MODE == 0 && MAX_URLS_PER_FILE > 0) {
                            counter3 = 0;
                            fileCounter = 0;
                            for (counter2 = 0; counter2 < urlsList[counter].length; counter2++) {
                                fileText += interUrl + urlsList[counter][counter2];
                                interUrl = nl;
                                if (counter3 >= MAX_URLS_PER_FILE || counter2 == urlsList[counter].length - 1) {
                                    counter3 = 0;
                                    filePath = getPath(path + '\\' + hostsList[counter] + separator + fileCounter + '.txt');
                                    writeFile(filePath, fileText, true);
                                    fileCounter++;
                                    fileText = '';
                                    interUrl = '';
                                }
                                counter3++;
                            }
                        } else {
                            fileText = urlsList[counter].join(nl);
                            filePath = getPath(path + '\\' + hostsList[counter] + separator + fileCounter + '.txt');
                            writeFile(filePath, fileText, true);
                        }
                    }
                }
            }
        }
    }
}

function getShortURL(DownloadLink) {
    if (FILE_SAVE_MODE > 0) return DownloadLink.getContentURL() || DownloadLink.getPluginURL();
    var url = DownloadLink.getProperty("LINKDUPEID") || DownloadLink.getPluginURL();
    return url.replace(/(^(https?|ftp):\/\/[^\/]+\/)/, '').replace(/.+:\/\//, '');
}
I haven't tested the other modes yet and though when a download finish it save it, it don't save it on the proper file yet (it increment the counter, or don't decrement it or something like that, didn't look into it yet).
AFAIK the part to save all the finished downloads into multiples hosts files with a URL limit per file is working fine.

To test it in "real conditions" I have created a script which, when I press a toolbar button, basically reset the last download (a 10kb image so it isn't long) and start downloads, so it trigger properly "download stopped" scripts.
Reply With Quote