This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Shell script to control KT

Tags: None
(comma "," separated)
johnjaylward
Registered Member
Posts
63
Karma
0

Thu Feb 19, 2009 12:47 am
I used to use it to get the command to restart it when not at the desktop (i.e. over ssh) if it crashed for some reason. If anyone knows the command for doing that, it'd be appreciated. After it's restarted, I can use the web interface.
J
Registered Member
Posts
86
Karma
0

Thu May 07, 2009 10:10 pm
From #KTorrent @ FreeNode:
<amaur> jotik: I am not interested in registering at the forum just for this now. but you can post it there if you wish


I haven't checked the code yet, nor ran it, but perhaps someone will find this useful:
Code: Select all
#!/bin/bash
# Public domain script by amaurea/amaur on IRC (freenode for example).
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/\000/\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


<amaur> I called it kt, and use it like: kt load "url", kt ls, kt info, kt start [num|hash] and similarly with stop and remove, kt clear to remove everything and kt file id|hash to get a file list


Edit (15.05.2009): New script. Amaur made some changes, but still doesn't want to register to the forums.


Thank you KTorrent developers! :)
_________________
"Thou shalt not steal." - STOP PIRACY NOW!
thetaco
Registered Member
Posts
1
Karma
0

Re: Shell script to control KT

Sat May 14, 2011 3:08 pm
Great script. I had some problems getting it fired up first because pidof returned more than one pid. I added the -s option to both pidof calls and now it works fine.
Yingchun
Registered Member
Posts
2
Karma
0

Re: Shell script to control KT

Sat Aug 13, 2011 2:03 am
I come here to have a good look .
xandry
Registered Member
Posts
1
Karma
0

Re: Shell script to control KT

Fri Oct 14, 2011 11:07 am
Links on script not worked. :(


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], Google [Bot], Sogou [Bot]