19

I need a way to convert .torrents into magnet links. Would like a way to do so in python. Are there any libraries that already do this?

anlar
  • 477
  • 1
  • 11
  • 26
Sebastian Sixx
  • 203
  • 1
  • 2
  • 4
  • There is a approach for python3: pip install magneturi; import magneturi; magneturi.from_torrent_file('xx.torrent') – ife Mar 17 '17 at 10:19

1 Answers1

36

You can do this with the bencode module, extracted from BitTorrent.

To show you an example, I downloaded a torrent ISO of Ubuntu from here:

http://releases.ubuntu.com/12.04/ubuntu-12.04.1-desktop-i386.iso.torrent

Then, you can parse it in Python like this:

>>> import bencode
>>> torrent = open('ubuntu-12.04.1-desktop-i386.iso.torrent', 'r').read()
>>> metadata = bencode.bdecode(torrent)

A magnet hash is calculated from only the "info" section of the torrent metadata and then encoded in base32, like this:

>>> hashcontents = bencode.bencode(metadata['info'])
>>> import hashlib
>>> digest = hashlib.sha1(hashcontents).digest()
>>> import base64
>>> b32hash = base64.b32encode(digest)
>>> b32hash
'CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6'

You can verify that this is correct by looking here and you will see the magnet link is:

magnet:?xt=urn:btih:CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6

If you want to fill in some extra parameters to the magnet URI:

>>> params = {'xt': 'urn:btih:%s' % b32hash,
...           'dn': metadata['info']['name'],
...           'tr': metadata['announce'],
...           'xl': metadata['info']['length']}
>>> import urllib
>>> paramstr = urllib.urlencode(params)
>>> magneturi = 'magnet:?%s' % paramstr
>>> magneturi
'magnet:?dn=ubuntu-12.04.1-desktop-i386.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&xl=729067520&xt=urn%3Abtih%3ACT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6'
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • It works, now I have a question. If I look up the href of the magnet links for instance in the piratebay they look like this: magnet:?xt=urn:btih:bbb6db69965af769f664b6636e7914f8735141b3&dn=Ubuntu-12.04-desktop-i386.iso&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80 What are the "udp" parameters used for? – Sebastian Sixx Sep 18 '12 at 17:47
  • That's a tracker URI: http://en.wikipedia.org/wiki/Magnet_URI_scheme#Parameters – jterrace Sep 18 '12 at 18:01
  • Specifically, to a UDP tracker: http://en.wikipedia.org/wiki/UDP_tracker – jterrace Sep 18 '12 at 18:01
  • @SebastianSixx added construction of the magnet URI – jterrace Sep 18 '12 at 18:17
  • The answer is very useful but I would like to point out that it looks like currently magnet links are generated using `Hex` encoding and not `Base32` mostly. – Predelnik Mar 29 '15 at 17:04
  • To get hex-encoded info hash instead of base32-encoded one, use the `hexdigest()` method, i.e. `digest = hashlib.sha1(hashcontents).hexdigest()` – doubleDown Jan 06 '16 at 05:09
  • There is an issue with the `xt` parameter in @jterrace's method: the `urn:btih` portion should not be url-encoded. Instead, use: `magneturi = 'magnet:?xt=urn:btih:%s&%s' % (b32hash, paramstr)`. – Régis B. Jan 13 '17 at 18:05
  • Is it any approach for python 3? – ife Mar 17 '17 at 10:06