Work with ktorrent in command line

Ktorrent provides a nice user interface, but when you want to schedule a task with Ktorrent. You need to use command line to give yourself a lift. I include some simple examples and a shell script found in the Ktorrent forum for your interest. Please note that you need to start Ktorrent in the first place, otherwise the following commands will not work. To start Ktorrent, simply type /usr/bin/ktorrent in command line.

ktorrent

Start all torrents download:

qdbus org.ktorrent.ktorrent /core startAll

Stop all torrents download:

qdbus org.ktorrent.ktorrent /core stopAll

The script to control Ktorrent in command line

#!/bin/bash
# Public domain script by amaurea/amaur on IRC (freenode for example).
# gary example
# qdbus org.ktorrent.ktorrent /core startAll


case $1 in
        help)
                echo "kt: A simple console interface for ktorrent.
Usage: In the following \"id\" indicates either a torrent hash or index.
       [] indicates optional arguments.

       kt start [id]: If ktorrent is not running, start it. Otherwise,
                      if id is given, start that torrent, otherwise start
                      all torrents.
       kt quit: Quit ktorrent.
       kt load url: Load the torrent given by url. Note that the torrent must
                    be manually startet afterwards.
       kt ls: Print a list of all torrents, of the format: index hash name.
       kt info [id]: Print more detailed info about the selected (or all)
                     torrent(s).
       kt stop [id]: Stop the torrent given by id, or all if id is missing.
       kt name [id]: Like ls, but names only.
       kt remove id: Remove the torrent given by id (but not the actual files).
       kt clear: Remove all torrents.
       kt files [id]: List information about the files of the selected torrent.
       kt pri [id] [priority]: Give the selected torrent the given priority.
       kt pri [id] [file index] [priority]: Set the priority of the given file.
       kt pri [id] equal: Give all files of the torrent the same priority.
       kt pri [id] first: Download the first files in the torrent first."
       exit ;;
esac
pid=$(pidof ktorrent)
if [ ! $pid ]; then
        case "$1" in
                start)
                        ktorrent --display :0.0 ;;
                *)
                        echo "ktorrent is not running!" ;;
        esac
        exit
fi
eval "export $(cat /proc/$(pidof ktorrent)/environ | perl -pne 's/00/\n/g' | grep DBUS_SESSION_BUS_ADDRESS)"
loc="org.ktorrent.ktorrent"
cmd="qdbus $loc"
case "$1" in
        load)
                res=`$cmd /core loadSilently "$2" 1` ;;
        list|ls)
                torrents=`$cmd /core torrents`
                i=0
                for torrent in $torrents; do
                        name=`$cmd /torrent/$torrent name`
                        printf "%d %s %s\n" $i $torrent "$name"
                        i=$(($i+1))
                done ;;
        info)
                if [ "$2" ]; then
                        if (( ${#2} < 4 )); then
                                torrents=(`$cmd /core torrents`)
                                torrents=${torrents[$2]}
                        else torrents=$2; fi
                else torrents=`$cmd /core torrents`; fi
                i=0
                for torrent in $torrents; do
                        name=`$cmd /torrent/$torrent name`
                        size=`$cmd /torrent/$torrent totalSize`
                        dsize=`$cmd /torrent/$torrent bytesToDownload`
                        prog=`$cmd /torrent/$torrent bytesDownloaded`
                        speed=`$cmd /torrent/$torrent downloadSpeed`
                        seed=`$cmd /torrent/$torrent seedersConnected`
                        leech=`$cmd /torrent/$torrent leechersConnected`
                        priority=`$cmd /torrent/$torrent priority`
                        sl=`printf "[%d|%d]" $seed $leech`
                        pri=`printf "(%d)" $priority`
                        printf "%3.0lf%% of %11d %4.0lf kb/s %8s %4s %s\n" $((100*$prog/$dsize)) $dsize $(($speed/1000)) $sl $pri "$name"
                        i=$(($i+1))
                done ;;
        name|stop|start|remove|files)
                if (( ${#2} < 4 )); then
                        torrents=(`$cmd /core torrents`)
                        torrent=${torrents[$2]}
                else torrent=$2; fi
                case "$1" in
                        name)
                                $cmd /torrent/$torrent name ;;
                        start)
                                if [ "$2" ]; then res=`$cmd /core start $torrent`
                                else res=`$cmd /core startAll`; fi;;
                        stop)
                                if [ "$2" ]; then res=`$cmd /core stop $torrent`
                                else res=`$cmd /core stopAll`; fi;;
                        remove)
                                # qdbus boolean bug workaround: use dbus-send instead
                                res=`dbus-send --type=method_call --dest=$loc /core org.ktorrent.core.remove string:"$torrent" boolean:false` ;;
                        files)
                                n=`$cmd /torrent/$torrent numFiles`
                                for (( i=0; i < $n; i++ )); do
                                        path=`$cmd /torrent/$torrent filePath $i`
                                        pct=`$cmd /torrent/$torrent filePercentage $i`
                                        size=`$cmd /torrent/$torrent fileSize $i`
                                        priority=`$cmd /torrent/$torrent filePriority $i`
                                        printf "%d %3.0lf%% of %11d [%d] %s\n" $i $pct $size $priority "$path"
                                done ;;
                esac ;;
        pri|priority|prioritize)
                if [ $3 ]; then
                        if (( ${#2} < 4 )); then
                                torrents=(`$cmd /core torrents`)
                                torrent=${torrents[$2]}
                        else torrent=$2; fi
                        if [ ! $torrent ]; then exit; fi
                        n=`$cmd /torrent/$torrent numFiles`
                        if [ $4 ]; then
                                res=`$cmd /torrent/$torrent setFilePriority $3 $4`
                        else
                                case $3 in
                                        equal|equalize)
                                                for (( i=0; i < $n; i++ )); do
                                                        res=`$cmd /torrent/$torrent setFilePriority $i 40`
                                                done ;;
                                        inc|increasing)
                                                for (( i=0; i < $n; i++ )); do
                                                        pri=`printf "%2.0lf" $(((4*$i/$n+3)*10))`
                                                        res=`$cmd /torrent/$torrent setFilePriority $i $pri`
                                                done ;;
                                        dec|decreasing)
                                                for (( i=0; i < $n; i++ )); do
                                                        pri=`printf "%2.0lf" $(((4*($n-$i-1)/$n+3)*10))`
                                                        res=`$cmd /torrent/$torrent setFilePriority $i $pri`
                                                done ;;
                                        first)
                                                m=$(($n < 3 ? $n : 3))
                                                for (( i=0; i < $m; i++ )); do
                                                        res=`$cmd /torrent/$torrent setFilePriority $i $(((6-$i)*10))`
                                                done
                                                for (( i=3; i < $n; i++ )) do
                                                        res=`$cmd /torrent/$torrent setFilePriority $i 30`
                                                done ;;
                                        *)
                                                res=`$cmd /torrent/$torrent setPriority $3` ;;
                                esac
                        fi
                else echo Too few arguments!; fi ;;
        clear)
                torrents=`$cmd /core torrents`
                for torrent in $torrents; do
                        res=`dbus-send --type=method_call --dest=$loc /core org.ktorrent.core.remove string:"$torrent" boolean:false`
                done ;;
        quit)
                res=`$cmd /MainApplication quit` ;;
        *)
                echo "Unrecognized command: '$1'" ;;
esac 

3 thoughts on “Work with ktorrent in command line

  1. Giovanni Vinaccia

    the script didn’t work for me. the problem was in eval “export $(cat /proc/$(pidof ktorrent)/environ | perl -pne ‘s/00/\n/g’ | grep DBUS_SESSION_BUS_ADDRESS)” especially the regex didnt match correctly and grep didnt work because of some unprintable characters in the output of perl
    i solved with eval “export $(strings /proc/${pid}/environ | grep DBUS_SESSION_BUS_ADDRESS)”
    hope this is useful

    thank you for the excellent and very useful script

  2. Thanks Giovanni! useful indeed.

    • Salafrance Underhill

      Hi Giovanni,

      The script didn’t quite work on my Suse 12.1 64-bit system. The DBUS_SESSION_ADDRESS parsing code didn’t work out of the box, so I added the following function:

      function sessionbus()
      {
      dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
      if [ -e “$dbus_session_file” ]; then
      . “$dbus_session_file”
      #echo $DBUS_SESSION_BUS_ADDRESS $DBUS_SESSION_BUS_PID
      echo “DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS”
      fi
      }

      And then I substituted your ‘eval’ expression with the following.

      export `sessionbus`

      I *think* the dbus session file is created on session start, and further suspect that not all session managers will do this. This is the first dbus code I’ve ever written, and I’ve not done any prior reading, so any appropriate corrections would be very welcome. Thank you for the script.

Leave a comment