JDownloader Community - Appwork GmbH
 

Reply
 
Thread Tools Display Modes
  #1  
Old 28.11.2024, 16:11
pspzockerscene's Avatar
pspzockerscene pspzockerscene is offline
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 73,796
Default Write comment into text file

The following script writes a comment on a download item into a .txt file with the same name of the source item.

Code:
Spoiler:

Code:
// Writes link comment to text file
// Trigger: "A download has stopped"
// Has some settings, just search for "settings" in this code

// Main function that runs the script based on the early return principle
function run() {
    // Early return if the download is not finished
    if (!link.isFinished()) {
        // console.log("Link is not yet downloaded");
        return;
    }

    var comment = link.getComment(); // Retrieve the comment from the link

    // Early return if the comment is empty
    if (!comment) {
        return;
    }

    // Retrieve the link's file name and download folder path
    var fileName = link.name;
    var sourcePath = package.getDownloadFolder();
    
    // "Settings", see vars down below
    // Define allowed file types (set to null or undefined to allow all types)
    var allowedFileTypes = null; // Example: set to null to allow all file types
    // var allowedFileTypes = [".mp3"];
    // Option to overwrite existing files
    var overwriteExisting = false; // Set to true to overwrite existing files

    // Check if the file type is allowed (skip check if allowedFileTypes is null or undefined)
    if (allowedFileTypes && !allowedFileTypes.some(function(extension) { return fileName.endsWith(extension); })) {
        return;
    }

    // Change the file name to add ".txt" as the new extension
    var outputPath = sourcePath + "/" + fileName + ".txt";

    // Early return if the file exists and overwriting is disabled
    if (!overwriteExisting && getPath(outputPath).exists()) {
        return;
    }
    
    // Write the comment to the file
    writeFile(outputPath, comment, true);
}

// Execute the run function
run();

Pastebin:
pastebin.com/raw/DcpjqF2f

Before you ask:
The reason I put all the code into a function is to be able to do "return;" instead of an exception since otherwise it won't compile in our framework.

Here is a merged version of mine and this one:
Spoiler:

Code:
// Writes link comment to text file
// Trigger: "A download has stopped"
// Has some settings, just search for "settings" in this code

// Main function that runs the script based on the early return principle
function run() {
    // "Settings", see vars down below
    // Define allowed file types (set to null or undefined to allow all types)
    var allowedFileTypes = null; // Example: set to null to allow all file types
    // var allowedFileTypes = [".mp3"];
    
    // Option to overwrite existing files
    var overwriteExisting = false; // Set to true to overwrite existing files
    
    // Allowed hosts (only these hosts will proceed; set to null to allow all hosts)
    var myHosts = ["soundcloud.com", "audiocloud.com", "ping.online.net"];

    // Early return if the download is not finished
    if (!link.isFinished()) {
        // console.log("Link is not yet downloaded");
        return;
    }

    var comment = link.getComment(); // Retrieve the comment from the link

    // Early return if the comment is empty
    if (!comment) {
        return;
    }

    // Retrieve the link's file name, download folder path, and host
    var fileName = link.name;
    var sourcePath = package.getDownloadFolder();
    var host = link.downloadHost;

    // Skip host check if myHosts is null or undefined
    if (myHosts && myHosts.indexOf(host) === -1) {
        return;
    }

    // Skip file type check if allowedFileTypes is null or undefined
    var fileExtension = fileName.split('.').pop();
    if (allowedFileTypes && allowedFileTypes.indexOf(fileExtension) === -1) {
        return;
    }

    // Change the file name to add ".txt" as the new extension
    var outputPath = sourcePath + "/" + fileName + ".txt";

    // Early return if the file exists and overwriting is disabled
    if (!overwriteExisting && getPath(outputPath).exists()) {
        return;
    }
    
    // Write the comment to the file
    writeFile(outputPath, comment, true);
}

// Execute the run function
run();

I liked your "inline function" more since it eliminates one indentation.
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?

Last edited by pspzockerscene; 10.01.2025 at 14:02.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 04:24.
Provided By AppWork GmbH | Privacy | Imprint
Parts of the Design are used from Kirsch designed by Andrew & Austin
Powered by vBulletin® Version 3.8.10 Beta 1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.