JDownloader Community - Appwork GmbH
 

Go Back   JDownloader Community - Appwork GmbH > International Support > JDownloader 2
Reply
 
Thread Tools Display Modes
  #1  
Old 22.09.2018, 01:04
webdjoe webdjoe is offline
Junior Loader
 
Join Date: Apr 2017
Posts: 11
Default Event Scripter Example - Organizing Headless Media Library

This script supports adding Packages through the myjDownloader app, plugin or website and have jDownloader automatically rename, organize and add to a plex server on same machine. Although you can use emby or almost any other common media server. This is done by adding "MVF" or "TVF" ("mvf" or "tvf) to the end of the package name which should be set to the name of the movie or tv show episode, respectively.

I created an event scripter script to automatically download and organize a headless media server with jDownloader, Filebot and Plex. If you also use Sonarr and Radarr you can integrate a postRequest() function or call an external script to hit the respective API. I thought having sonarr and radarr were too resource intensive when compared to just calling a filebot script, although they offer a more robust media management solution including the ability to add higher quality file with different extensions and not leave a duplicate in your library.

I used a quick and dirty method to get around the duplicate issue as I commonly download lower quality when initially available. I renamed all file names in library to mkv extension. I created a packagizer rule to rename all media file that aren't archived to MKV extension
Code:
Package Name Contains: (tvf$|TVF$|mvf$|MVF$) - ENABLE REGEX
Filetype is: Video File
TO:
Filename To: <jd:orgpackagename>.mkv
The finished archive script renames the extracted media file automatically. You can remove the rename function if you don't need it, although it won't cause an issue if you were to leave it on.


Add this code to event scripter entry that starts on package finished:
Code:
//Put in event scripter that is initiated on package finished
//At the end of the package name put mv for movies or tv for TV shows
//"mvf" or "tvf" must be present at end of package name in order to run script
// "mvf" forces the batch file to run the filebot amc script with "movies" as UT_LABEL and
//"tvf" forces batch file to run amc script with "series" as UT_LABEL

var script = JD_HOME + '/jdownloader-postprocess.cmd'; //JD_HOME is jDownloader variable for app directory
var downfol = package.getDownloadFolder();
var fname = package.getDownloadLinks()[0].getName().toString()
var name = package.getName();
var path = downfol + '/' + fname
var links = package.getDownloadLinks() ? package.getDownloadLinks() : [];

//Extract The Type of Media File from Package Name
nametype = name.slice(-3);

//Ensure package has completed
function isReallyFinished() {
    for (var i = 0; i < links.length; i++) {

        if (links[i].getArchive() != null && links[i].getExtractionStatus() != "SUCCESSFUL" || !package.isFinished()) {
            return false
        }
    }

    return true

}

// Test to ensure download is a media file

function mediafile() {
        if (nametype === "mvf" || nametype === "tvf" || nametype === "TVF" || nametype === "MVF") {

            return true

        }

        return false
    }
    //Action to run if package is finnished and it is a media file
if (isReallyFinished() && mediafile()) {
    //Set label for filebot
    if (nametype == "mvf" || nametype == "MVF") {
        var label = "movies"
    }
    if (nametype == "tvf" || nametype == "TVF") {
        var label = "series"
    }



    var command = [script, path, name.slice(0, -4), label, 'PACKAGE_FINISHED']
    log(callSync(command))
    log(deleteFile(path, false))
}

function splitPath(path) {
    return path.toString().match(/(.+\\)(.+)(\..+)/);

}
Add this to event scripter that starts on archive extraction finished:

Code:
//APPEND MVF or TVF to package name to enable
//For use with filebot.  I have files renamed to mkv to avoid duplicates
//Movies stored on EXFAT which doesn't use xattr file streams so filebot cannot check for dupes
//Making every movie MKV is a quick dirty fix for thia if you frequently upgrade quality based on availability
//If dupe check isn't needed and your file system supports xattr you can remove the rename function

var script = JD_HOME + '/jdownloader-postprocess.cmd'
var package = archive.getDownloadLinks() ? archive.getDownloadLinks()[0].getPackage() : null
var links = package ? package.getDownloadLinks() : []
var videxts = ["avi", "mp4", "mpeg", "mkv", "mpg", "AVI", "MPEG", "MPG", "MP4", "MKV"];
var path = package.getDownloadFolder();
var name = package.getName();
//Get Last 3 Characters to Determine Movie or TV Show

nametype = name.slice(-3);

//Remove Last 4 Characters to Get Name of Show/Movie

medianame = name.slice(0, -4);

//Determine if archive is successfully extracted
function isReallyFinished() {
    for (var i = 0; i < links.length; i++) {
        if (links[i].getArchive() != null && links[i].getExtractionStatus() != "SUCCESSFUL" || !package.isFinished()) {
            return false
        }
    }

    return true
}

//Make sure the download is a TV Show or Movie
function mediafile() {
    if (nametype === "mvf" || nametype === "tvf" || nametype === "TVF" || nametype === "MVF") {

        return true

    }

    return false
}

//Action if all conditions are met

if (package && isReallyFinished() && mediafile()) {
    //Set movie or tv show type for filebot
    if (nametype == "mvf" || nametype == "MVF") {
        var label = "movies"
    }
    if (nametype == "tvf" || nametype == "TVF") {
        var label = "series"
    }
    //Rename Files
    archive.getExtractedFilePaths().forEach(function(file) {
            log(videxts.indexOf(file.getExtension()));

            if (videxts.indexOf(file.getExtension()) > -1) {
                var path = splitPath(file);
                file.renameTo(path[1] + name.slice(0, -4) + ".mkv");
            }
            if (videxts.indexOf(file.getExtension()) == -1) return;
        })
        //Build File path - I used the actual file itself instead of folder in case
        //link was downloaded to the base of the download folder
    var finalfile = archive.getExtractToFolder() + '/' + medianame + ".mkv"


    var command = [script, finalfile, medianame, label, 'ARCHIVE_EXTRACTED']
        //Execute jdownloader-postprocess.cmd
    log(callSync(command))
    log(deleteFile(path, false))
}
// Regex to split file path for rename
function splitPath(path) {
    return path.toString().match(/(.+\\)(.+)(\..+)/);

}
If you do not want the media file to be renamed you must run the command function in the archive.getExtractedFilePaths().forEach() loop or just put the extracttofolder function in the command.

And then this command file (or covert to bash if your running linux):

Code:
@ECHO OFF

:: Input Parameters
set ARG_PATH=%1
set ARG_NAME=%2
set ARG_LABEL=%3

:: Configuration
set CONFIG_OUTPUT=/Plex/Library/Path/

filebot -script fn:amc --output %CONFIG_OUTPUT% --action duplicate --conflict override -non-strict %ARG_PATH% --log-file amc.log --def plex=PLEXSERVER:NOTMYTOKEN skipExtract=y unsorted=y music=n artwork=n excludeList=excludes.txt ut_kind=multi ut_title=%ARG_NAME% ut_label=%ARG_LABEL% movieFormat="/Plex/Library/Path/{Plex}" seriesFormat="/Plex/Library/Path/{Plex}"
I'm not a pro at javascript so any questions, suggestions, constructive criticism would be appreciated. I can't take credit for most of this as the support here is awesome. Credit goes to mpgai, Jiaz, RedNoah and a few others that posted some great stuff in here.
Reply With Quote
  #2  
Old 24.09.2018, 10:41
Jiaz's Avatar
Jiaz Jiaz is offline
JD Manager
 
Join Date: Mar 2009
Location: Germany
Posts: 79,554
Default

Thanks for sharing your findings and your little how-to
__________________
JD-Dev & Server-Admin
Reply With Quote
Reply


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:59.
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 - 2024, Jelsoft Enterprises Ltd.