Page 1 of 1

Looking for python script example - loop through torrents

Posted: Mon Apr 16, 2012 7:08 pm
by jbrid
Hi,
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!

Re: Looking for python script example - loop through torrent

Posted: Tue Apr 17, 2012 5:59 pm
by jbrid
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

#!/usr/bin/python

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",
        "save_path",
        "tracker",
        "tracker_status",
        "next_announce",
        "name",
        "total_size",
        "progress",
        "num_seeds",
        "total_seeds",
        "num_peers",
        "total_peers",
        "eta",
        "download_payload_rate",
        "upload_payload_rate",
        "ratio",
        "distributed_copies",
        "num_pieces",
        "piece_length",
        "total_done",
        "files",
        "file_priorities",
        "file_progress",
        "peers",
        "is_seed",
        "is_finished",
        "active_time",
        "seeding_time"
        ]

count = 0
torrent_ids = []

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

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

def endSession(esresult):
    if esresult:
        print esresult
        reactor.stop()
    else:
        client.disconnect()
        printSuccess(None, False, "Client disconnected.")
        reactor.stop()

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

def on_torrents_status(torrents):
    global filtertime
    tlist=[]
    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
    defer.DeferredList(tlist).addCallback(printReport)

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))
    client.core.get_session_state().addCallback(on_session_state)

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

reactor.run()


Re: Looking for python script example - loop through torrent

Posted: Tue Oct 30, 2012 10:10 pm
by ratzeputz
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?

Regards,
ratze