Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polonskiy/a54815d256224100ab1eaca1a55c9ce1 to your computer and use it in GitHub Desktop.
Save polonskiy/a54815d256224100ab1eaca1a55c9ce1 to your computer and use it in GitHub Desktop.
Quick and easy way of making Chrome send bittorrent magnet links to a remote Transmission instance using its API on Ubuntu with xdg.

Quick and easy way of making Chrome send bittorrent magnet links to a remote Transmission instance using its API on Ubuntu with xdg. This method does not require modifying any system files or xdg's scripts, whereas most of the examples I found on the Internet did require such hacks.

Chrome does not handle default applications, instead relying on the OS to manage that. The default handler in Ubuntu is xdg, which maps the MIME type to the default application. Extra tooling is required because xdg requires a registered desktop file, so you can't simply give it a command to run.

Steps:

  1. Install the shell script transmission-remote-magnet ideally in /usr/local/bin: ln -s ~/src/transmission-remote-magnet.bash transmission-remote-magnet

  2. Install the desktop file, which is required for xdg to work and will likely require sudo: desktop-file-install ./transmission-remote-magnet.desktop

  3. Tell xdg to use the desktop file for the magnet MIME type: xdg-mime default transmission-remote-magnet x-scheme-handler/magnet

  4. Confirm that it works by either clicking on a magnet link in Chrome or from a shell: xdg-open "magnet:?(bwah)

Recommended reading:

#!/bin/bash
# transmission-remote-magnet
# Usage: transmission-remote-magnet <magnet URL>
#
# Copyright 2014 Sam Bisbee <sam@sbisbee.com>
# Modified from http://blog.flo.cx/2011/02/how-to-open-magnet-links-on-a-remote-transmission-daemon-with-one-click/
LINK="$1"
if [ -z "$LINK" ]; then
echo "Error: need magnet link"
exit 1
fi
zenity --question --text "Are you sure you want to start this torrent?"
test "$?" != "0" && exit 1
HOST="host"
PORT="9091" #transmission's default port
USER="user"
PASS="pass"
SESSID=$(curl --silent --anyauth --user $USER:$PASS "http://$HOST:$PORT/transmission/rpc" | sed 's/.*<code>//g;s/<\/code>.*//g')
curl --silent --anyauth --user $USER:$PASS --header "$SESSID" "http://$HOST:$PORT/transmission/rpc" -d "{\"method\":\"torrent-add\",\"arguments\":{\"filename\":\"${LINK}\"}}"
# So we can see transmission's response
sleep 1
[Desktop Entry]
Name=transmissionRemoteMagnet
Type=Application
Exec=transmission-remote-magnet %u
NoDisplay=true
MimeType=x-scheme-handler/magnet;
Terminal=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment