-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtm
executable file
·328 lines (245 loc) · 6.43 KB
/
tm
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env bash
# shellcheck disable=SC2034,SC1090,SC2207
shopt -s extglob
export TOP_PID=$$
version="0.1.2"
release="20210512"
main () {
config=${XDG_CONFIG_HOME:-$HOME/.config}/tm.conf
netrc=~/.tm-netrc
tm_host="localhost:4081"
# source config file if it exists
[[ -f ${config} ]] && source "${config}"
declare -a commands=($(declare -F|grep tm_|sed -e 's/.*tm_\(\S\+\).*/\1/'))
declare -a programs=($(declare -F|grep tm_|sed -e 's/.*\(tm_\S\+\).*/\1/;s/_/-/g'))
while getopts "a:chH:kln:" OPTION
do
case $OPTION in
k)
create_symlinks
exit
;;
a)
tm_add "${OPTARG}"
exit
;;
l)
tm_ls
exit
;;
n)
netrc="${OPTARG}"
;;
H)
tm_host="${OPTARG}"
;;
c)
if [[ ! -f "${config}" ]]; then
cat <<-EOT > "${config}"
netrc="$netrc"
tm_host="$tm_host"
EOT
else
exit_with_error "-c: config file ${config} exists, either remove it or edit it directly"
fi
;;
h)
help
exit
;;
*)
exit_with_error "unknown option $OPTION"
;;
esac
done
# shift out options
shift $((OPTIND-1))
cmd="${1//-/_}"
program=$(basename "$0")
IFS='|'
if [[ $cmd =~ ${commands[*]} ]]; then
unset IFS
shift
tm_"$cmd" "$@"
elif [[ $program =~ ${programs[*]} ]]; then
unset IFS
${program//-/_} "$@"
else
unset IFS
exit_with_error "no such command: $cmd\navailable commands: ${commands[*]//_/-}"
fi
}
# commands
tm_cmd () {
if [[ -n $(which transmission-remote) ]]; then
transmission-remote "$tm_host" -N "$netrc" "$@"
else
exit_with_error "transmission-remote not found, please install it first (apt install transmission-cli)"
fi
}
tm_help () {
tm_cmd -h
}
tm_add () {
tm_cmd -a "$@"
}
tm_add_selective () {
torrent="$1"
shift
files="${*,,}"
keep=0
count=0
if [[ -z "$torrent" || -z "$files" ]]; then
echo 'use: tm-add-selective <torrent_file> <file1>[,file2,file3,file4...]'
exit 1
fi
check_torrent "$torrent"
tm_cmd --start-paused
btih=$(tm_torrent_hash "$torrent")
# check if torrent is already downloading
if tm_active "$btih"; then
running=1
tm_stop "$btih"
else
tm_add "$torrent"
fi
if tm_info "$btih" > /dev/null; then
count=$(tm_file_count "$btih")
# if the torrent only has 1 file it does not make sense to do a selective download...
if [[ $count -gt 1 ]]; then
if [[ $running -eq 0 ]]; then
# need to keep at least 1 file active, otherwise transmission removes the torrent
tm_cmd -t "$btih" -G 1-$((count-1))
fi
while read -r id; do
[[ $id -eq 0 ]] && keep=1
tm_cmd -t "$btih" -g "$id"
done < <(tm_cmd -t "$btih" -f|grep -E "${files/,/|}"|cut -d ':' -f 1)
[[ $keep -eq 0 && $running -eq 0 ]] && tm_cmd -t "$btih" -G 0
fi
else
echo "error adding torrent"
exit 1
fi
tm_cmd --no-start-paused
tm_start "$btih"
}
tm_remove () {
tm_cmd -t "$@" -r
}
tm_start () {
if tm_active "$@"; then
tm_cmd -t "$@" -s
fi
}
tm_stop () {
tm_cmd -t "$@" -S
}
tm_info () {
tm_cmd -t "$@" -i
}
tm_files () {
tm_cmd -t "$@" -f
}
tm_ls () {
tm_cmd -l
}
tm_file_count () {
tm_files "$1"|head -1|sed 's/.* (\([[:digit:]]\+\) files):/\1/'
}
tm_active () {
tm_cmd -t "$@" -ip|grep -q Address
}
# torrent file related commands
tm_torrent_show () {
check_torrent "$@"
transmission-show "$@"
}
tm_torrent_files () {
tm_torrent_show "$@"|awk '/^FILES/ {start=1}; NF>1 && start==1 {print $0}'|sed -e 's/^\s\+\(.*\) ([0-9.]\+ .B)$/\1/'
}
tm_torrent_hash () {
tm_torrent_show "$@"|awk ' /^\s+Hash:/ {print $2}'
}
# helper functions
exit_with_error () {
echo -e "$(basename "$0"): $*" >&2
kill -s TERM $TOP_PID
}
check_torrent () {
if ! (file "$1"|grep -i bittorrent)>/dev/null; then
exit_with_error "$1 is not a torrent file"
fi
}
create_symlinks () {
basedir="$(dirname "$0")"
sourcefile="$(readlink -e "$0")"
prefix=$(basename "$sourcefile")
for cmd in "${commands[@]}"; do
name="${prefix}-${cmd//_/-}"
if [[ ! -e "$basedir/$name" ]]; then
ln -s "$sourcefile" "$basedir/$name"
fi
done
exit
}
help () {
sourcefile="$(readlink -e "$0")"
prefix=$(basename "$sourcefile")
echo "$(basename "$(readlink -f "$0")")" "version $version"
cat <<- EOF
Use: $prefix COMMAND OPTIONS [parameters]
$prefix-COMMAND OPTIONS [parameters]
A helper script for transmission-remote and related tools, adding some
functionality like selective download etc.
PROGRAMS/COMMANDS
EOF
for cmd in "${programs[@]}"; do
echo -e " $cmd\r\t\t\t${cmd/$prefix-}"
done
cat <<- EOF
OPTIONS
-k create symbolic links
creates links to all supported commands
e.g. $prefix-cmd, $prefix-ls, $prefix-add, ...
links are created in the directory where $prefix resides
-n NETRC set netrc ($netrc)
-H HOST set host ($tm_host)
-c create a config file using current settings (see -n, -H)
-l execute command 'ls'
-a TORR execute command 'add'
-h this help message
EXAMPLES
In all cases it is possible to replace $prefix-COMMAND with $prefix COMMAND
show info about running torrents:
$ $prefix-ls
add a torrent or a magnet link:
$prefix-add /path/to/torrent/file.torrent
$prefix-add 'magnet:?xt=urn:btih:123...'
add a torrent and selectivly download two files
this only works with torrent files (i.e. not magnet links) for now
$prefix-add-selective /path/to/torrent/file.torrent filename1,filename2
show information about a running torrent, using its btih or ID:
$prefix-show f0a7524fe95910da462a0d1b11919ffb7e57d34a
$prefix-show 21
show files for a running torrent identified by btih (can also use ID)
$prefix-files f0a7524fe95910da462a0d1b11919ffb7e57d34a
stop a running torrent, using its ID (can also use btih)
$prefix-stop 21
get btih for a torrent file
$prefix-torrent-hash /path/to/torrent/file.torrent
remove a torrent from transmission
$prefix-remove 21
execute any transmission-remote command - notice the double dash
see man transmission-remote for more info on supported commands
$prefix-cmd -- -h
$prefix cmd -h
CONFIGURATION FILES
$config
$prefix can be configured by editing the script itself or the configuration file:
netrc=~/.tm-netrc
tm_host="transmission-host.example.org:4081"
values set in the configuration file override those in the script
EOF
}
main "$@"