btcinfo  

Hic inserere motto

Converting magnet links to .torrent files in bash

May 02, 2019 — shinohai

I like simple things that do their job well, and I adore bash. A tool was sorely needed in my arsenal that could convert magnet links from torrent sites to a useable .torrent file. The site magnet2torrent.me is often down, and few options exist on the web to do convert files locally that aren't pythonshit. My search led me to a Shithub repository owned by Isis Buttcruft, and while the tool appears to work as advertised it has been neglected and is heavily commented and contains a few unnecessary parts. Let's trim it up a bit and make it a function.

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

Adding this to my .bash_aliases file now allows me to simply do `magnet "<URL>"` and get a torrent file instantly that can be loaded up into rtorrent or streamed using peerflix.

Tags: Entertainment, UNIX, Bash