#!/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: o passwd: -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/.*//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/.*//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