rtorrent: handling "magnet:" URIs via a bash script

Source: http://wiki.rtorrent.org/MagnetUri

Create a magnet2torrent script with the following code:

#! /bin/bash

# Convert a magnet URI into a .torrent file
# Run the script with:
# ./magnet2torrent.sh "MAGNET_URI"
# (Don't forget the quotes aroud MAGNET_URI)

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"

Make the script executable:

$ chmod a+x magnet2torrent

Run the script with a magnet URI (after adding it to your $PATH variable):

$ magnet2torrent "MAGNET_URI"