Skip to content

Instantly share code, notes, and snippets.

@Gestas
Created October 10, 2021 16:09
Show Gist options
  • Save Gestas/d36e0b0b554376c73de5367c6a2dab7c to your computer and use it in GitHub Desktop.
Save Gestas/d36e0b0b554376c73de5367c6a2dab7c to your computer and use it in GitHub Desktop.
Add torrent to remote Transmission instance from browser
#!/usr/bin/env bash
# Use your browser to add a torrent to a remote Transmission instance.
# Works for magnet links only.
# Requires "transmission-remote".
# For desktop popups "kdialog" (for KDE) or "zenity" (for Gnome) required.
# USAGE:
# Put this script on your local host, make it executable (chmod +x <script name>).
# Update the port and hostname settings below, lines 23 and 25.
# In your browser settings set this script as the application to handle magnet links.
# Assumes HTTPS, remove "--ssl" on line 48 if not.
# Script exits successfully (0) if we're able to add the torrent.
# Exits with error (1) if we can't.
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
link="$1"
# Port number for Transmission
port=""
# Hostname for Transmission
hostname=""
cleanup(){
trap - SIGINT SIGTERM ERR EXIT
if [ ! -z "${success_msg+x}" ]; then
if [[ $DESKTOP_SESSION == "plasma" ]]; then
kdialog --title "Magnet Manager" --passivepopup "$success_msg" 10
elif [[ $DESKTOP_SESSION == "gnome" ]]; then
zenity --info --title "Magnet Manager" --text "$success_msg"
fi
echo "$success_msg"
fi
if [ ! -z "${error_msg+x}" ]; then
if [[ $DESKTOP_SESSION == "plasma" ]]; then
kdialog --title "Magnet Manager" --error "Failed to add torrent; $error_msg" 10
elif [[ $DESKTOP_SESSION == "gnome" ]]; then
zenity --error --title "Magnet Manager" --text "$error_msg"
fi
echo "$exit_msg"
fi
}
add_magnet(){
add_torrent=$(transmission-remote "$hostname":"$port" "--ssl" "-a" $link || true)
if [[ $add_torrent == *"success"* ]]; then
success_msg="Torrent added to Transmission."
exit 0
else
error_msg="$add_torrent"
exit 1
fi
}
add_magnet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment