Ubuntu – How to open magnet torrent links from Firefox in rTorrent

16.04command linefirefoxrtorrentscripts

I am using the command-line torrent client rTorrent under Xenial Xerus and I would like to:

  1. Find and click on a magnet torrent link with Firefox
  2. Have the magnet link opened automatically in rTorrent and start downloading

I believe a script is needed to be called from within Firefox, but the writing of such a script has so far defeated me…

Best Answer

The problems usually lie with mime type and default handlers.

Firstly, have you changed Firefox's about:config settings? ie:

network.protocol-handler.expose.magnet -> false

and reset other options as per this Firefox deluge Q&A.

Have you set up rTorrent to watch any particular directory?

FILE: ~/.rtorrent.rc

# Maximum and minimum number of peers to connect to per torrent.
min_peers = 50
max_peers = 80

# Maximum number of simultanious uploads per torrent.
max_uploads = 5

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 0
upload_rate = 50

# Default directory to save the downloaded torrents.
directory = $HOME/torrents/downloads

# Watch a directory for new torrents
# SET your watch directory here --v 
schedule = watch_directory,5,5,$HOME/torrents/watch/*.torrent

port_range = 60125-64125
port_random = yes
dht = auto

# UDP port to use for DHT.
dht_port = 63425

# Enable peer exchange (for torrents not marked private)
peer_exchange = yes

# Check hash for finished torrents.
check_hash = yes

encryption = allow_incoming,try_outgoing ,enable_retry

Then it is a simple matter of "save as" into $HOME/torrents/watch.

Change $HOME/torrents/watch to whatever torrents subfolder you use, or at least $HOME to /home/username

Create a file and add the following script:

FILE: maglink-rtorrent.sh

#!/bin/bash

cd $HOME/torrents/watch    # set your watch directory here
[[ "$1" =~ xt=urn:btih:([^&/]+) ]] || exit;
echo "d10:magnet-uri${#1}:${1}e" > "meta-${BASH_REMATCH[1]}.torrent"

Don't forget to make it executable

chmod +x maglink-rtorrent.sh

This also offers the ability to download from terminal by:

cd $HOME/torrents/watch
./maglink-rtorrent.sh "MAGNET-LINK-HERE"

Further awesome Service Tips and rTorrent setup options here.

Futher credits:

Update 2:

If not using rTorrent, but kTorrent or qBittorent, then this following is the way to get that running:

# check defaults
xdg-mime query default x-scheme-handler/magnet
gvfs-mime --query x-scheme-handler/magnet

# set defaults
xdg-mime default qBittorent.desktop x-scheme-handler/magnet
gvfs-mime --set x-scheme-handler/magnet qBittorrent.desktop

There's a further setting (from memory) for whether you require commandline.

For rTorrent though, this link is the FlexGet rTorrent Magnet URI Handler

Full information here.

Hope this helps.

Related Question