How to open magnet torrent links from Firefox in rTorrent?

7,101

Solution 1

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.

Solution 2

The following script is a spin on Max Gonzih's code that works both with regular .torrent files and magnet links:

#!/bin/bash

torrent_file_or_magnet_link="$1"

# Edit rtorrent.rc to automatically start downloads when a .torrent file
# appears in this directory.
cd "$HOME/.rtorrent/watch/start/"

# XT stands for "exact topic".
# BTIH is the BitTorrent info hash:
# https://en.wikipedia.org/wiki/Magnet_URI_scheme#BitTorrent_info_hash_(BTIH)
# This is the hex-encoded SHA-1 hash of the torrent file info section
magnet_regex="xt=urn:btih:([^&/]+)"
if [[ "$torrent_file_or_magnet_link" =~ $magnet_regex ]]; then
  torrent_hash=${BASH_REMATCH[1]};
  magnet_link_length=${#torrent_file_or_magnet_link}
  # To conform with the bencode encoding, the magnet link's number of characters
  # must be part of the torrent file, otherwise rTorrent can't read it.
  # Same for the "e" at the end.
  # See https://en.wikipedia.org/wiki/Bencode
  torrent_file_content="d10:magnet-uri${magnet_link_length}:${torrent_file_or_magnet_link}e"

  # Note that rTorrent will read this torrent file, start downloading
  # the file and then remove the torrent file
  echo "$torrent_file_content" > "$torrent_hash.torrent"
else
  cp "$torrent_file_or_magnet_link" .
fi

You can use this in a script (let's call it pass_to_rtorrent.sh) and have Firefox pass the torrent file or magnet link to the script:

Firefox rTorrent screenshot

Make sure the script is executable: chmod +x pass_to_rtorrent.sh

Share:
7,101

Related videos on Youtube

andrew.46
Author by

andrew.46

The Tao is close, but you look far away. Life is simple yet you look for problems...

Updated on September 18, 2022

Comments

  • andrew.46
    andrew.46 over 1 year

    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...

  • andrew.46
    andrew.46 almost 8 years
    I have managed to crack this one on my own using: paxmontem.blogspot.com.au/2012/03/… but I would be more than happy to award the bonus if you can bend your answer this way...
  • greg.arnott
    greg.arnott almost 8 years
    Ha! I was going to add that script to my answer, but chose not to for it being a terminal option - where I thought you were after a pure browser one-click option. link
  • andrew.46
    andrew.46 almost 8 years
    Not so much the terminal script on the bottom of your link. Looks like the Browser can be set to pass the magnet link to a script to clean it up and pass it to the rtorrent watch directory. I used this page: paxmontem.blogspot.com.au/2012/03/… but it all feeds off the ARCH wiki page...