jDownloader Python Post-Processing | PlexGuide.com

jDownloader Python Post-Processing

  • Stop using Chrome! Download the Brave Browser via >>> [Brave.com]
    It's a forked version of Chrome with native ad-blockers and Google's spyware stripped out! Download for Mac, Windows, Android, and Linux!
Welcome to the PlexGuide.com
Serving the Community since 2016!
Register Now

H1f0x

Administrator
Original poster
Project Manager
Donor
Jan 1, 2019
109
59
Hi all,

I just created a script which allows you to post process extracted jdownloader2 downloads with the docker container of the community app. It's working, but the code is still ugly - but commented, at last :) I will maybe update the code and publish it on github when i'm done, but in this state i just attach it below. I utilise filebot cli to do the job. I know this can be done also with the event scripter, but i don't like it that much and I use the script with a loc modification as a cronjob.

The script uses Python 3 to run.

Please install filebot on your system, on ubuntu this can be done like this to access the unionfs drive:
Bash:
sudo apt update
sudo apt install snapd
sudo snap install filebot --devmode

How to use jDownloader to work with the script

1. You need to give each package a correct naming. After the download it should also extract the files into the directory with the package name. Where you do this doesn't matter. The path can be specified. The extraction is done by jDownloader.

Code:
Examples:

Default Path for extraction: /mnt/downloads/jdownloader2/extracted

1. TV-Show: Suits
- jDownloader package name: TV=Suits=2011=01 (TV=<show name>=<year>=<season>)

2. Movie: Mortal Engines
- jDownloader package name: MOVIE=Mortal Engines=2018 (MOVIE=<movie name>=<year>)

This gives the following structure after a successful extraction:

/mnt/downloads/jdownloader2/extracted
|_ "TV=Suits=2011=01"
   |_ file.mkv
   |_ info.txt
   |_ etc.
|_ "MOVIE=Mortal Engines=2018"
   |_ file.mkv
   |_ info.txt
   |_ etc.
What does the script do for me?

1. The script searches within the specified path for all mkv and mp4 files not named sample and saves them for processing (information gathering)
2. if no new files can be found, the folder will be deleted for cleanup reason... ugly, but i was to lazy
3. the script processes every file and does a little different procedure for TV and MOVIE files.
a) filebot with TheTVDB for tv-shows
b) filebot with TheMovieDB for movies
c) and some other things as folder structure and so on
4. the copy job is done by python not by filebot in case anythings breaks the script can re-run and copy again
5. a successfully copied file will be delete, so a re-run will not process it again if the script fails later on

Are the logs?
Yes. A logfile will be created at the execution location of the script with current timestamp information.

How does the destination structure look like?
Code:
/mnt/unionfs/
|_ tv
  |_ "Suits ()2011)"
    |_ "Season 01"
      |_ new_filename.mkv
|_ movies
  |_ "Mortal Engines (2018)"
    |_ new_filename.mkv
Sure thing the filebot command can be modified for subtitles and other things. This code is working but i guess it needs to be modified for your own needs as well.

Maybe someone can use it :) If there are questions or you get stuck, hit me up, i try to help!

Cheers
Hifox
 

Attachments

Last edited:

H1f0x

Administrator
Original poster
Project Manager
Donor
Jan 1, 2019
109
59
Yes it is. I should have mentioned it above. sorry for that.
 
  • Like
Reactions: 1 user

H1f0x

Administrator
Original poster
Project Manager
Donor
Jan 1, 2019
109
59
hehe. damn. thats the first thing i do on the system. i added the snap install instructions. :)
 
M

MrDoob

Guest
Python:
import os
import shutil
import logging
import calendar
import time
from subprocess import Popen, PIPE

# declare variables
folder_extracted = '/mnt/downloads/jdownloader2/extracted'
files_extensions = ['.mkv', '.mp4']
folder_destination_base = '/mnt/unionfs'
folder_destination_tv = os.path.join(folder_destination_base, 'tv/')
folder_destination_movies = os.path.join(folder_destination_base, 'movies/')

# logging configuration
ts = str(calendar.timegm(time.gmtime()))
logging.basicConfig(filename='logfile_' + ts + '.log', filemode='w', level=logging.DEBUG)

logging.info(' [*] detecting new extracted files to process..')

# array for files to be processed
files_to_do = []

# detect all files in folder to process, edit "files_extensions" for wanted extensions
for dirpath, dirnames, files in os.walk(folder_extracted):
    for name in files:
        for extension in files_extensions:
            if name.lower().endswith(extension):

                # remove sample files from list
                if name.lower().find('sample') == -1:

                    try:

                        # prepare file and information for processing
                        data = {}
                        data['file'] = os.path.join(dirpath, name)
                        data['description'] = data['file'].split(folder_extracted)[1].split(os.sep)[1]
                        data['type'] = data['description'].split('=')[0]
                        data['name'] = data['description'].split('=')[1] + ' (' + data['description'].split('=')[2] + ')'
                        data['year'] = data['description'].split('=')[2]

                        if data['type'] == 'TV':
                            data['season'] = data['description'].split('=')[3]

                        files_to_do.append(data)
                        logging.debug(' [-] new file detected: ' + data['file'])

                    except:
                        logging.warning(' [!] Error on file: ' + os.path.join(dirpath, name))
                        pass

# if list is empty, delete extracted folder
if not files_to_do:
    logging.info(' [*] cleanup of folder: ' + folder_extracted)
    shutil.rmtree(folder_extracted)
    exit(0)

logging.info(' [+] processing detected files with "filebot" and moving them..')

# processing files
for file in files_to_do:

    logging.info(' [*] processing file: ' + file['file'])

    # process TV files
    if file['type'] == 'TV':

            try:
                # prepare filebot command
                filebot = ('filebot -rename "%s" -non-strict --db "TheTVDB" --q "%s"  --log fine' % (file['file'], file['name']))
                logging.debug(' [-] filebot command used: ' + filebot)

                # execute filebot command
                process = Popen(filebot, shell=True, stdout=PIPE, stderr=PIPE)
                stdout, stderr = process.communicate()

                stdout_str = stdout.decode('utf-8')

                # verify output of filebot
                if stdout_str.lower().find('already exists') == -1:
                    new_file = stdout_str.split('\n')[2].split('] to [')[1].replace(']','')
                    logging.info(' [+] new filename: ' + new_file)
                else:
                    new_file = file['file']
                    logging.info(' [+] filename already exists: ' + new_file)

                # split file head and tail of new filename
                new_file_head, new_file_tail = os.path.split(new_file)

            except:
                logging.warning(' [!] error during "filebot" processing, going to next file')
                continue

            try:
                # create directory strucutre at target location
                season = ('Season %s' % file['season'])
                folder_destination_tv_show = os.path.join(folder_destination_tv, file['name'])
                folder_destination_tv_show_season = os.path.join(folder_destination_tv_show, season)

                if os.path.isdir(folder_destination_tv_show) == False:
                    os.makedirs(folder_destination_tv_show)

                if os.path.isdir(folder_destination_tv_show_season) == False:
                    os.makedirs(folder_destination_tv_show_season)

            except:
                logging.warning(' [!] error during folder verification, going to next file')
                continue

            logging.info(' [*] copying file to location: ' + folder_destination_tv_show_season)

            try:
                # copy file, not move, in case of error the script can rerun properly
                shutil.copy2(os.path.join(new_file_head, new_file_tail), os.path.join(folder_destination_tv_show_season, new_file_tail))
                logging.info(' [+] copy job was successful!')

            except:
                logging.warning(' [!] error during copy job, going to next file')
                continue

            try:
                # delete file after successful processing
                os.remove(os.path.join(new_file_head, new_file_tail))
                logging.info(' [+] deleted file on src location successfully!')

            except:
                logging.warning(' [!] error during deletion of file, going to next file')
                continue


    if file['type'] == 'MOVIE':

        try:
            # prepare filebot command
            filebot = ('filebot -rename "%s" -non-strict --db "TheMovieDB" --q "%s"  --log fine' % (file['file'], file['name']))
            logging.debug(' [-] filebot command used: ' + filebot)

            # execute filebot command
            process = Popen(filebot, shell=True, stdout=PIPE, stderr=PIPE)
            stdout, stderr = process.communicate()

            stdout_str = stdout.decode('utf-8')

            # verify output of filebot
            if stdout_str.lower().find('already exists') == -1:
                new_file = stdout_str.split('\n')[2].split('] to [')[1].replace(']', '')
                logging.info(' [+] new filename: ' + new_file)
            else:
                new_file = file['file']
                logging.info(' [+] filename already exists: ' + new_file)

            # split file head and tail of new filename
            new_file_head, new_file_tail = os.path.split(new_file)

        except:
            logging.warning(' [!] error during "filebot" processing, going to next file')
            continue

        try:
            # create directory strucutre at target location
            folder_destination_movies_movie = os.path.join(folder_destination_movies, file['name'])

            if os.path.isdir(folder_destination_movies_movie) == False:
                os.makedirs(folder_destination_movies_movie)

        except:
            logging.warning(' [!] error during folder verification, going to next file')
            continue

        logging.info(' [*] copying file to location: ' + folder_destination_movies_movie)

        try:
            # copy file, not move, in case of error the script can rerun properly
            shutil.copy2(os.path.join(new_file_head, new_file_tail),
                         os.path.join(folder_destination_movies_movie, new_file_tail))
            logging.info(' [+] copy job was successful!')

        except:
            logging.warning(' [!] error during copy job, going to next file')
            continue

        try:
            # delete file after successful processing
            os.remove(os.path.join(new_file_head, new_file_tail))
            logging.info(' [+] deleted file on src location successfully!')

        except:
            logging.warning(' [!] error during deletion of file, going to next file')
            continue

logging.info(' [+] processed all files! Exit.')

exit(0)
 
  • Like
Reactions: 1 user

Recommend NewsGroups

      Up To a 58% Discount!

Trending