Directory Based on Tracker

dsouvik215

Member
May 25, 2018
896
0
16
Hello all,

Can someone help me configure ruTorrent to download files to directories based on the tracker? I basically download Movies from PTP and tv shows from BTN and just want them to go to specific folders, so that when I rsync them to local directories, my Plex server can pick them and categorize them properly.

From all my searching, I can see the question asked a lot but have not been able to find any answers. My seedbox is running rTorrent 8.9, has AutoTools, and ruTorrent 3.4.

Thanks in advance
 

dsouvik215

Member
May 25, 2018
896
0
16
I actually am trying to figure out how to do it with auto-lablel and auto-move but I can't figure out the variable name for the tracker.

I already tried {tracker} in the auto-move path and that didn't cut it.
 

jith45

Member
May 25, 2018
960
0
16
I tried to do the same, but I couldn't get it to work with a plugin. So I made my own sort script in python, that runs when a file is completed.

Code:
system.method.set_key = event.download.finished,sort_finished,"execute={~/sort.py,$d.get_hash=}"The trigger event in .rtorrent.rc

Code:
#!/usr/bin/env python
import xmlrpclib, os, sys, re, shutil, yaml

def htc(m):
return chr(int(m.group(1),16))

def urldecode(url):
rex = re.compile('%([0-9a-hA-H][0-9a-hA-H])',re.M)
return rex.sub(htc,url)

print '=== BEGIN ==='

# Load config file
with file("config.yml", "r") as stream:
config = yaml.load(stream)

# Set base location
base_location = config['base_location']

# Add default location at score of 0
matches = [[config['default'], 0]]

# Find hash from input
rid = sys.argv[1]
print ' Hash: %s' % rid

#rtorrent xmlrpc
rtorrent = xmlrpclib.ServerProxy('http://localhost/rutorrent/RPC2')

#load torrent info
label = urldecode(rtorrent.d.get_custom1(rid))
base_path = rtorrent.d.get_base_path(rid)

# sanity check to make sure torrent actually exists
if not os.path.exists(base_path):
print '%s: Base path doesn\'t exist, quitting' % base_path
exit()

# outputting some information
(start_dir, start_name) = os.path.split(base_path)
print 'File name: %s' % start_name
print ' File dir: %s' % start_dir
print ' Label: %s' % label

# determine match based on label name
if len(label) != 0:
matches.append([ label.lower().replace(" ","_"), config['label_score'] ])

# determine a match based on tracker
trackers = rtorrent.t.multicall(rid, '', 't.get_url=')
find_trackers = re.compile('^https?://(\w+\.)*(\w+\.\w+):)\d+)?/').search(trackers[0][0])
if find_trackers:
tracker = find_trackers.group(2)
print ' Tracker: %s' % tracker
if tracker in config['trackers']:
matches.append(config['trackers'][tracker])

print ' Matches: %s' % matches

# test matches in order of score
for match in sorted(matches, key=lambda m: m[1], reverse=True):
final_dir = os.path.join(base_location, match[0])
if os.path.exists(final_dir):
break

final_path = os.path.join(final_dir, start_name)
print ' Move to: %s' % (final_dir)

# some checks based on computer path
if base_path == final_path:
print '%s: File is already at target location' % start_name
exit()
if os.path.exists(final_path):
print '%s: Another file already exists at target location' % start_name
exit()

# move torrent to new location and inform rtorrent
print 'Moving and telling rtorrent to move %s to %s' % (rid, final_dir)
rtorrent.d.set_directory(rid, final_dir)
shutil.move(base_path, final_dir)
rtorrent.d.resume(rid)The python sort script, sort.py

Code:
base_location: '/media/storage'
default: downloads

label_score: 50

trackers:
animebyt.es: ['anime', 30]
torrentvault.org: ['tv_shows', 70]
config.yml. The settings for where each track should go

It uses a combinations of labels and tracker to find the directory.
 

dsouvik215

Member
May 25, 2018
896
0
16
Cool, thanks!!! I am not sure if my seedbox provider allows me to run a python script but I am trying this anyhow.

I just put sort.py and config.yml in the same location as rtorrent.rc. Is that correct?
Is the path in config.yml relative to the user base directory or rtorrent's base directory?
Do I have to request a restart of rtorrent for this to start working?
 

jith45

Member
May 25, 2018
960
0
16
I use rtorrent on my personal linux server, so I have no idea about seed box specific questions.

About restarting. You will need to run the system.method.set_key command. This can be done be either restarting rtorrent, reloading the .rtorrent.rc, or in the the rtorrent command line program pressing ctrl+x, pasting in the line, and then hit enter.

You should just put the files in the same directory as .rtorrent.rc. By default that is loaded from ~/, but if its loaded from somewhere else you might need to set the .rtorrent.rc command to use an absolute path to score.py.

You will also need to edit the line in the '#rtorrent xmlrpc' section to use the address of your XML RPC. (I should have put that in config.yml but oh well)

To be more robust and less likely to fail, you can change the the '# Load config file section' section to as follows. So it loads the config file from the same directory as score.py, instead of the current working directory (which may not be the same).
Code:
# Load config file
config_file = os.path.join(os.path.dirname(__file__), "config.yml")
with file(config_file, "r") as stream:
config = yaml.load(stream)
If you have any other questions feel free to ask, but tbh this probably isn't a good solution unless you have some programming knowledge. I am thinking about making this script into a rutorrent plugin, so there would be a nice GUI for everything.