Rename and move torrent files for Plex

Suggest, post, or discuss plugins for Deluge
Post Reply
themsk
New User
New User
Posts: 2
Joined: Thu May 07, 2020 8:03 pm

Rename and move torrent files for Plex

Post by themsk »

Hi to all.
I have a Raspeberry set as Plex media server, so, i use deluge a lot, but i dont like to get to involved on moving the files from deluge download folder (located on a ssd) to Plex movies and tv show folders, and above all..the wasted time renaming the files so the plex like them more..

So.. i have created a script that is used by deluge thru Execute plugin.
What this script do:
1. Clearing the file names, and from
47.Ronin.2013.720p.BluRay.DTS.x264-LolHD.mkv
it will become:
47 Ronin (2013).mkv
the renaming will be to parent folder and to subtitle file too.
2. Get rid of junk file like sample.mkv, .nfo, .info files and others. Only media and subtitles will be moved.
3. Move the media to right location (folder): If its a movie, will be copyed to movies folder, if is a tv show, it will be copyed to tvshow folder.

So lets get started. You will have two files, copy.sh and copy.py
copy.sh

Code: Select all

#!/bin/bash

torrentid="$1"
torrentname="$2"

echo $(/home/pi/scripts/copytoplex/copy.py $torrentid $torrentname) > /dev/null

Please see that my files are located in /home/pi/scripts/copytoplex/, so you must change this with your path.

copy.py

Code: Select all

#!/usr/bin/python

import sys, os, fnmatch, re, shutil, logging
import stat


logger = logging.getLogger('copytoplex')
hdlr = logging.FileHandler('/home/pi/scripts/copytoplex/copy.log')
formatter = logging.Formatter('%(asctime)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

torrentid = sys.argv[1]
torrentname = sys.argv[2]




def clean_name(name):
    name = name.replace(" ", ".").rstrip()
    if re.search('.E[0-9].', name):name = re.sub(r'(.E[0-9].)', r'\1 ____', name)
    elif re.search('.S[0-9].', name):name = re.sub(r'(.S[0-9].)', r'\1 ____', name)
    else: name = re.sub(r'\.([0-9]{3,4}p)', '____', name)
    #else: name = re.sub(r'([\[\(]?((?:19[0-9]|20[0-2])[0-9])[\]\)]?)', r' (\1) ____', name)
    #name = re.sub(r'([\[\(]?((?:19[0-9]|20[0-2])[0-9])[\]\)]?)', r' (\1) ____', name)
    name = name.split('____');
    name = name[0].replace(".", " ").rstrip()
    name= re.sub(' {2,}', ' ', name)
    return name


def make_dirs(path):
    if not os.path.exists(path):
        os.mkdir(path)
        logger.info(path)
         

def copy_files(filename, dest, source):
    filename, file_extension = os.path.splitext(filename)
    filename = clean_name(filename) + file_extension
    #if filename.endswith(".srt"):
       #if not filename.endswith(".ro.srt") and not filename.endswith(".en.srt"):
         #  filename =os.path.splitext(filename)[0]
           #filename = filename + ".ro.srt"
    destination = dest + filename
    shutil.copyfile(source, destination)
    print(filename)


def get_files(torrent, torrentpath):
    
    if re.search('.S[0-9].', torrent):label='tvshows'
    else: label='movies'
    approved_extr = ('.srt', '.mkv', '.avi', '.str')
    torrent_name = clean_name(torrent)
    dest = r"/mnt/plex/" + label + "/" + torrent_name + "/"
    make_dirs(dest)
    #logger.info(torrent)
    for path, dirs, files in os.walk(os.path.abspath(torrentpath +"/" +torrent)):
        for filename in files:
            if filename.endswith(approved_extr) and not re.search('sample', filename, re.IGNORECASE) and not re.search('english.srt', filename, re.IGNORECASE):
                source = r""+torrentpath +"/" +torrent + "/" + filename
                copy_files(filename, dest, source)
    os.chmod(dest, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
                


get_files(torrentname, '/mnt/hdd/downloading')
exit()

Please see on the last lines that my downloaded media are located in /mnt/hdd/downloading, so change the location with your folder.
on this line
dest = r"/mnt/plex/" + label + "/" + torrent_name + "/"
change the /mnt/plex/ with you folder that contain libraries of Plex, the folders movies and tvshows

Now, if you dont have the plugin Execute installed on deluge, download it and install it.
On the Execute plugin, select condition Torrent Complete and put the path to copy.sh file and hit Add
Click ok and restart the deluge daemon, not the interface. If you don't know how to restart daemon, restart all the os system, otherwise the execute plugin will not make the command.

Good luck
Also, see this simple tool for delete all folder downloaded:
viewtopic.php?f=9&t=55679&p=231736#p231736
Post Reply