-
Notifications
You must be signed in to change notification settings - Fork 6
/
transmission-uploader
executable file
·112 lines (93 loc) · 2.54 KB
/
transmission-uploader
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
#!/bin/sh
# Inspired from
# https://blog.flo.cx/2011/02/how-to-open-magnet-links-on-a-remote-transmission-daemon-with-one-click/
_file=/dev/null
_server="http://127.0.0.1"
_port="9091"
_myname="$(basename "$0")"
if [ -r "$HOME/.config/$_myname.rc" ]; then
. "$HOME/.config/$_myname.rc"
fi
__usage () {
cat << EOF
Add a torrent or magnet link to your transmission web interface
$0 [-k file] [-s server] [-p port] [-u user -w password] [-v] [link [link [...]]]
$0 -h
Default values are:
o file: $_file
Used to keep track of which links have already been uploaded to your
transmission daemon
o server: $_server
o port: $_port
o user: <empty>
o passwd: <empty>
-h: print this help text
-v: be verbose (set -x)
The following syntax also works:
< torrent_list.txt | $0
EOF
}
while getopts ":hk:s:p:u:vw:" _opt; do
case "$_opt" in
h) __usage ; exit 0 ;;
k) _file="$OPTARG" ;;
p) _port="$OPTARG" ;;
s) _server="$OPTARG" ;;
u) _user="$OPTARG" ;;
v) set -x ;;
w) _password="$OPTARG" ;;
*) __usage >&2 ; exit 1 ;;
esac
done
shift $((OPTIND-1))
# set true if you want every torrent to be paused initially
#_paused="true"
_paused="false"
_endpoint="$_server:$_port/transmission/rpc"
_exit_code=0
if [ -z "$_user" ]; then
_sessid="$(curl --silent "$_endpoint" | sed 's/.*<code>//g;s/<\/code>.*//g')"
{ if [ -z "$1" ]; then
cat -
else
for _i in "$@"; do
echo "$_i"
done
fi } |
{ while IFS= read -r _link; do
if ! grep -q "$_link" "$_file"; then
if curl --silent --header "$_sessid" "$_endpoint" -d \
"{\"method\":\"torrent-add\",\"arguments\":\
{\"paused\":$_paused,\"filename\":\"$_link\"}\
}" | grep -q '"result":"success"'; then
echo "$_link" >> "$_file"
else
: $((_exit_code+=1))
fi
fi
done }
else
_sessid="$(curl --silent --anyauth --user "$_user:$_password" "$_endpoint" |
sed 's/.*<code>//g;s/<\/code>.*//g')"
{ if [ -z "$1" ]; then
cat -
else
for _i in "$@"; do
echo "$_i"
done
fi } |
{ while IFS= read -r _link; do
if ! grep -q "$_link" "$_file"; then
if curl --silent --anyauth --user "$_user:$_password" --header "$_sessid" \
"$_endpoint" -d \
"{\"method\":\"torrent-add\",\"arguments\":\
{\"paused\":$_paused,\"filename\":\"$_link\"}\
}" | grep -q '"result":"success"'; then
echo "$_link" >> "$_file"
else
: $((_exit_code+=1))
fi
fi
done }
fi
exit $_exit_code