Rename and move torrent files for Plex

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

Rename and move torrent files for Plex

Postby themsk » Thu May 07, 2020 8:50 pm

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

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, and

Code: Select all



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

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

Code: Select all


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')

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

def clean_name(name):
    name = name.replace(" ", ".").rstrip()
    if'.E[0-9].', name):name = re.sub(r'(.E[0-9].)', r'\1 ____', name)
    elif'.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):

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("") and not filename.endswith(""):
         #  filename =os.path.splitext(filename)[0]
           #filename = filename + ""
    destination = dest + filename
    shutil.copyfile(source, destination)

def get_files(torrent, torrentpath):
    if'.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 + "/"
    for path, dirs, files in os.walk(os.path.abspath(torrentpath +"/" +torrent)):
        for filename in files:
            if filename.endswith(approved_extr) and not'sample', filename, re.IGNORECASE) and not'', 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')

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 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:

Return to “Plugins”

Who is online

Users browsing this forum: No registered users and 6 guests