Locked History Actions

MagnetUri

Magnet URIs in rTorrent

1. Introduction

To learn all about magnet links, read the Wikipedia article. Magnet URIs are implemented in the stable version 0.8.9, and can be loaded from a watch directory via special bencoded files (note that the unstable 0.9.0 has a bug regarding loading these files). At the time of this writing, rTorrent is able to get torrent files via DHT, but does not distribute them itself.

And yes, "getting them via DHT" implies that you need to activate DHT for magnets to do anything at all.

Copy the magnet link to the clipboard, press Backspace or Enter in rTorrent, then simply paste the link into the prompt and press Enter again. The created entry is named using the info hash plus .meta, just wait for DHT to kick in and provide the actual .torrent file, which can take a while.

3. Handling "magnet:" URIs via PyroScope "mktor"

An already working installation of PyroScope is assumed here, so the mktor command is available. Currently, you need an installation from source (SVN HEAD).

  1. Update PyroScope to the latest SVN head revision (beyond r1746).

  2. Add the setting "magnet_watch" to your ~/.pyroscope/config.ini (in the [GLOBAL] section), pointing to the watch directory you want rTorrent to load magnet links from.

Then set mktor as the magnet: URI handler

  • Firefox Linux:
    1. Open about:config in a Firefox tab.

    2. Open the context menu in the list of configuration values, and select "New... » Boolean".
    3. Enter "network.protocol-handler.expose.magnet" as the name, and "false" as the value.

    4. Open a magnet: link somewhere, and choose "mktor" as the handling application.

    5. The magnet link should now be loaded into rTorrent, according to your watch directory settings.

  • Firefox Windows:
  • The current release version of pyrocore includes an older mktor.exe. We can, however, make this work by grabbing a newer mktor.py from HEAD.
    1. Install Python

    2. Install setuptools (using ez_setup.py if on 64-bit Windows)

    3. From python scripts dir,
      • run easy_install pyrocore
      • run pyroadmin --create-config
    4. Map location of rtorrent watch directory to network drive (For example Z:\). If rtorrent is on the same machine as Firefox, skip this.
    5. In C:\Users\user\.pyroscope\config.ini, add this in the [GLOBAL] section:
        # Created torrent files will go here
        magnet_watch = Z:\
    6. Download latest mktor.py script (versions older than r1746 won't work) from here

    7. Create a batch (ie "callmktor.bat") file containing:
          C:\path\to\mktor\script\mktor.py "%*"
    8. Convert the batch file to a .exe (ie "callmktor.exe"). This is because Firefox can only call .exe and .com applications. Quick Batch File Compiler works well (That is, it will produce a .exe that handles the whole magnet string handed by Firefox correctly, despite the special characters).
    9. In Firefox:
      • Open about:config in a Firefox tab.
      • Open the context menu in the list of configuration values, and select "New... » Boolean".
      • Enter "network.protocol-handler.expose.magnet" as the name, and "false" as the value.
      • Open a magnet: link somewhere, and choose "mktor" as the handling application.
      • The magnet link should now be loaded into rTorrent, according to your watch directory settings.

  • Firefox Mac OS X
    1. Open "AppleScript Editor"

    2. Insert something like the following:
        on open location this_URL
              do shell script "~/bin/mktor \"" & this_URL & "\""
        end open location
    3. Save the script with "File Format" as "Application" in your "Applications" folder (e.g., /Applications/MagnetUriHandler)

    4. Add the following to the application's Info.plist (e.g., vim /Applications/MagnetUriHandler.app/Contents/Info.plist)

         <key>CFBundleIdentifier</key>
         <string>org.rtorrent.AppleScript.MagnetUriHandler</string>
         <key>CFBundleURLTypes</key>
         <array>
          <dict>
           <key>CFBundleURLName</key>
           <string>Magnet URI Handler</string>
           <key>CFBundleURLSchemes</key>
           <array>
            <string>magnet</string>
           </array>
          </dict>
         </array>
    5. Firefox should now pick up your new URI handler automatically.

  • luakit — Simply add this to your ~/.config/luakit/rc.lua:

    webview.init_funcs.magned_hook = function (view, w)
        view:add_signal("navigation-request", function (v, uri)
            if string.match(uri, "^magnet:") then
                local cmd = string.format("%s %q", "mktor", uri)
                luakit.spawn(cmd)
                return false
            end
        end)
    end

4. Handling "magnet:" URIs via a bash script

4.1. Original script

Use the following bash script and save it as "magnet.sh" or something in your home folder and adjust the path to your watch directory. If you have multiple watch directories, make multiple scripts.

  • cd ~/watch || exit    # set your watch directory here
    [[ "$1" =~ xt=urn:btih:([^&/]+) ]] || exit
    echo "d10:magnet-uri${#1}:${1}e" > "meta-${BASH_REMATCH[1]}.torrent"
    xmessage -nearmouse 'torrent added to rtorrent'

Make the script executable

  •   chmod 0755 magnet.sh

Run the script with a magnet URI:

  •   ./magnet.sh "MAGNET_URI"

The quotes around the MAGNET_URI are important, otherwise not the whole URI could be read a single argument passed to a script but multiple arguments.

In your browser, you can set to open magnet links with that script file.

4.2. Variation with human readable file names

The script from above will use a hash to name the generated .torrent file. This version will use the torrent name (if available) and use the hash as a fallback.

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

5. FlexGet rTorrent Magnet URI Handler

See rTorrent Magnet URI Handler for details on a plugin that will store magnet URIs compatible to a rTorrent watch dir.