rtorrent queue

A python script for queuing torrents. Just moves files into the watch directory based on what rtorrent reports, and not merely by using the directories as an indication. You’re expected to have setup SCGI with rtorrent. So that requires compiling with XML-RPC which many distros will not do, and have the option enabled in the .rtorrent.rc. xmlrpc2scgi is also needed.

Code

import xmlrpc2scgi as xs
import os
import glob
import stat
import time
import shutil

# scgi host and port
rtorrent_host="scgi://localhost:5000"
# watch and queue folders
watch = "/home/USERNAME/.rtorrent/watch"
queue = "/home/USERNAME/.rtorrent/queue"
# total number of downloads allowed
max_downloads = 4
# download rate in kbp/s
max_download_rate = 50000
# how often to recheck to add more (in seconds)
recheck_time = 25

rtc = xs.RTorrentXMLRPCClient(rtorrent_host)
while True:
    infohashes = rtc.download_list('incomplete')
    if (len(infohashes) < max_downloads) or (rtc.get_down_rate() < max_download_rate):
        download = []
        for file in glob.glob(queue + '/*.torrent'):
            download.append((os.stat(file)[stat.ST_MTIME], file))
        if len(download) > 0:
            download.sort()
            if os.path.exists(watch + '/' + str(download[0][1]).split('/')[-1]):
                print "%s already exists, deleting from queue folder" % (download[0][1])
                os.remove(download[0][1])
            else:
                print "%s -> %s" % (download[0][1], watch)
                shutil.move(download[0][1], watch)

    time.sleep(recheck_time)

Information

It only goes by incomplete torrents., but you can check by other views or maybe even upload or download speed. If you want to do something like max download/upload speed you can check get_upload_rate/get_download_rate and maybe start torrents that way to try max out the connection. I decided it was a good enough idea to add, so I added speed as a factor, also it fixes a bug with same name torrents. The issues with speed though is the recheck time, because if the speed takes time to build up, and it adds torrents too fast (e.g. getting greater than the max_downloads which is good for slower torrents as it will eventually max out the speed) then too many torrents might be added. It’s best to test it out for your usage. But you want a nice compromise with speed and the number you want running.