-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathktorrent-activity.sh
executable file
·120 lines (91 loc) · 3.34 KB
/
ktorrent-activity.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
### KTorrent (very) simple activity tracker script
### Receives 2 parameters:
### * --activity / -a : Shows only active torrents, requires -v
### * --verbose / -v : Show torrents info (if any) in the output
## VARS
APP_NAME="$(qdbus org.ktorrent.ktorrent /MainApplication org.qtproject.Qt.QCoreApplication.applicationName)"
APP_VER="$(qdbus org.ktorrent.ktorrent /MainApplication org.qtproject.Qt.QCoreApplication.applicationVersion)"
ARG_ACTIVEONLY=false
ARG_VERBOSE=false
TORRENTS_IDS=0
TORRENTS_COUNT_TOTAL=0
TORRENTS_COUNT_RUNNING=0
SPEED_DL=0
SPEED_DL_RAW=0
SPEED_UP=0
SPEED_UP_RAW=0
SIZE=0
SIZE_RAW=0
SIZE_COMPL=0
SIZE_COMPL_RAW=0
PERCENTAGE=0
TORRENTS_IDS=($(qdbus org.ktorrent.ktorrent /core torrents))
TORRENTS_COUNT_RUNNING=$(qdbus org.ktorrent.ktorrent /core numTorrentsRunning)
TORRENTS_COUNT_TOTAL=${#TORRENTS_IDS[@]}
declare -a OUTPUT
## AUX: GATHER ARGS
for ((i=1; i<=2; i++)); do
if [[ ${!i} = "--active" || ${!i} = "-a" ]]; then
ARG_ACTIVEONLY=true
elif [[ ${!i} = "--verbose" || ${!i} = "-v" ]]; then
ARG_VERBOSE=true
fi
done
## AUX FUNCTION: PRINT OUTPUT & END
function end {
for LINE in ${!OUTPUT[*]}; do
echo "[${APP_NAME}]: ${OUTPUT[LINE]}"
done
exit
}
## 0. GET RUNNING / TOTAL
function getinitinfo {
if (( TORRENTS_COUNT_TOTAL > 0 )); then
OUTPUT[0]="Torrents: ${TORRENTS_COUNT_RUNNING} running / ${TORRENTS_COUNT_TOTAL} total"
else
OUTPUT[0]="No active torrents"
end
fi
}
## 1. GET INDIVIDUAL TORRENT INFO
OUTPUT_LINE=1
function gettorrents {
for ID in "${TORRENTS_IDS[@]}"; do
TORR_NAME="$(qdbus org.ktorrent.ktorrent /torrent/${ID} name)"
TORR_SPEED_DL_RAW=$(qdbus org.ktorrent.ktorrent /torrent/${ID} downloadSpeed)
TORR_SPEED_DL="$(numfmt --to=iec-i --suffix=B ${TORR_SPEED_DL_RAW})"
TORR_SPEED_UP_RAW=$(qdbus org.ktorrent.ktorrent /torrent/${ID} uploadSpeed)
TORR_SPEED_UP="$(numfmt --to=iec-i --suffix=B ${TORR_SPEED_UP_RAW})"
TORR_SIZE_RAW=$(qdbus org.ktorrent.ktorrent /torrent/${ID} totalSize)
TORR_SIZE="$(numfmt --to=iec-i --suffix=B ${TORR_SIZE_RAW})"
TORR_SIZE_COMPL_RAW=$(qdbus org.ktorrent.ktorrent /torrent/${ID} bytesDownloaded)
TORR_SIZE_COMPL="$(numfmt --to=iec-i --suffix=B ${TORR_SIZE_COMPL_RAW})"
TORR_PERCENTAGE="$((TORR_SIZE_COMPL_RAW*100/TORR_SIZE_RAW))%"
if [[ $ARG_VERBOSE = true ]]; then
if [[ $ARG_ACTIVEONLY = false ]] || [[ $ARG_ACTIVEONLY = true && ( TORR_SPEED_DL_RAW -gt 0 || TORR_SPEED_UP_RAW -gt 0 ) ]]; then
OUTPUT[OUTPUT_LINE]="${OUTPUT_LINE}. ${TORR_NAME} | Speed: ${TORR_SPEED_DL}/s ~ ${TORR_SPEED_UP}/s | Completed: ${TORR_PERCENTAGE} (${TORR_SIZE_COMPL} / ${TORR_SIZE})"
OUTPUT_LINE=$((OUTPUT_LINE + 1))
fi
fi
## 1.2. SUM UP TOTALS
SPEED_DL_RAW=$((SPEED_DL_RAW + TORR_SPEED_DL_RAW))
SPEED_UP_RAW=$((SPEED_UP_RAW + TORR_SPEED_UP_RAW))
SIZE_RAW=$((SIZE_RAW + TORR_SIZE_RAW))
SIZE_COMPL_RAW=$((SIZE_COMPL_RAW + TORR_SIZE_COMPL_RAW))
done
}
## 2. GET TOTALS
function gettotals {
SPEED_DL="$(numfmt --to=iec-i --suffix=B ${SPEED_DL_RAW})"
SPEED_UP="$(numfmt --to=iec-i --suffix=B ${SPEED_UP_RAW})"
SIZE="$(numfmt --to=iec-i --suffix=B ${SIZE_RAW})"
SIZE_COMPL="$(numfmt --to=iec-i --suffix=B ${SIZE_COMPL_RAW})"
PERCENTAGE="$((SIZE_COMPL_RAW*100/SIZE_RAW))%"
OUTPUT[OUTPUT_LINE]="Total speed: ${SPEED_DL}/s ~ ${SPEED_UP}/s | Completed: ${PERCENTAGE} (${SIZE_COMPL} / ${SIZE})"
}
## 3. BUILD INFO
getinitinfo
gettorrents
gettotals
end