Looking for python script example - loop through torrents

Suggestions and discussion of future versions
New User
New User
Posts: 7
Joined: Mon Apr 16, 2012 5:23 pm
OS or Distro: Ubuntu

Looking for python script example - loop through torrents

Postby jbrid » Mon Apr 16, 2012 7:08 pm

Does anyone have an example of the python script that loops through the torrents and pulls out various pieces of information about them based on some conditions.

Ex. 1: Print 'Name', 'Ratio', etc. for all active torrents (all torrents where either UpSpeed or Downspeed > 0)

Ex. 2: Print 'Name', 'Ratio', etc. for all torrents from tracker XYZ

I thought this one would be a good example, but apparently the sclient library that it is using is depreciated.

Thanks a lot!

New User
New User
Posts: 7
Joined: Mon Apr 16, 2012 5:23 pm
OS or Distro: Ubuntu

Re: Looking for python script example - loop through torrent

Postby jbrid » Tue Apr 17, 2012 5:59 pm

This is what I was looking for. I realized I could clone something from the script in this thread. Various conditional logic can be put in the for loop. Please forgive me. This is the first python code I have ever touched. I don't have a good handle on the deferred object concept.

Code: Select all


from deluge.log import LOG as log
from deluge.ui.client import client
import deluge.component as component
from twisted.internet import reactor, defer
import time

cliconnect = client.connect()
is_interactive = True # Set this to True to allow direct output or set to False for cron

status_keys = ["state",

count = 0
torrent_ids = []

def printSuccess(dresult, is_success, smsg):
    global is_interactive
    if is_interactive:
        if is_success:
            print "[+]", smsg
            print "[i]", smsg

def printError(emsg):
    global is_interactive
    if is_interactive:
        print "[e]", emsg

def endSession(esresult):
    if esresult:
        print esresult
        printSuccess(None, False, "Client disconnected.")

def printReport(rresult):
    printSuccess(None, True, "TOTAL TORRENTS: %i" % (count))

def on_torrents_status(torrents):
    global filtertime
    for torrent_id, status in torrents.items():
        printSuccess(None, False, "Current torrent id is: %s" % (torrent_id))
        printSuccess(None, False, "--Torrent name is: %s" % (status["name"]))
        printSuccess(None, False, "--Torrent state is: %s" % (status["state"]))
        printSuccess(None, False, "--Torrent ratio is: %s" % (status["ratio"]))
        printSuccess(None, False, "--Torrent DL rate is: %s" % (status["download_payload_rate"]))
        printSuccess(None, False, "--Torrent UL rate is: %s" % (status["upload_payload_rate"]))
        printSuccess(None, False, "--Torrent tracker is: %s" % (status["tracker_status"]))
        global count
        count += 1

def on_session_state(result):
    client.core.get_torrents_status({"id": result}, status_keys).addCallback(on_torrents_status)

def on_connect_success(result):
    printSuccess(None, True, "Connection was successful!")
    curtime = time.time()
    printSuccess(None, False, "Current unix time is %i" % (curtime))

cliconnect.addCallbacks(on_connect_success, endSession, errbackArgs=("Connection failed: check settings and try again."))


Posts: 16
Joined: Wed May 09, 2012 1:32 pm
OS or Distro: debian/osx

Re: Looking for python script example - loop through torrent

Postby ratzeputz » Tue Oct 30, 2012 10:10 pm

Is it possible to use all of the implemented fields of status array in the call

Code: Select all

   client.core.get_torrents_status({"id": result}...

stated on this page -> http://www.rasterbar.com/products/libto ... #peer-info

Or is there only a limited subset available? Because if print status using the example given above with the extension to:

Code: Select all

    client.core.get_torrents_status({"id": result}, ["queue_position","completed_time","last_seen_complete","name","upload_payload_rate","time_added","save_path","is_finished"]).addCallback(on_torrents_status)

an example print looks like this:

Code: Select all

{'upload_payload_rate': 0, 'time_added': 1349747840.0, 'is_finished': True, 'save_path': '/home/dir', 'name': 'ABCDDD'}

or for a incomplete torrent like this:

Code: Select all

{'upload_payload_rate': 0, 'time_added': 1351595136.0, 'is_finished': False, 'save_path': '/home/dir/incomplete', 'name': 'ABCD'}

So some of the info seems to be missing or am I doing something wrong here?


Return to “Development”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest