Given a .torrent file how do I generate a magnet link in python? [closed]
Asked Answered
H

1

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?

Hausner answered 18/9, 2012 at 14:55 Comment(1)
There is a approach for python3: pip install magneturi; import magneturi; magneturi.from_torrent_file('xx.torrent')Improper
M
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'
Manfred answered 18/9, 2012 at 15:33 Comment(8)
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?Hausner
That's a tracker URI: en.wikipedia.org/wiki/Magnet_URI_scheme#ParametersManfred
Specifically, to a UDP tracker: en.wikipedia.org/wiki/UDP_trackerManfred
@SebastianSixx added construction of the magnet URIManfred
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.Anaerobic
To get hex-encoded info hash instead of base32-encoded one, use the hexdigest() method, i.e. digest = hashlib.sha1(hashcontents).hexdigest()Razid
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).Ruse
Is it any approach for python 3?Improper

© 2022 - 2024 — McMap. All rights reserved.