Skip to main content

Magnet2Torrent

Pure python project to turn a magnet link into a .torrent file. The goal is to do it as fast as possible.

Getting Started

Installing

pip install magnet2torrent

Usage

Download an ubuntu iso torrent.

magnet2torrent fetch "magnet:?xt=urn:btih:e2467cbf021192c241367b892230dc1e05c0580e&dn=ubuntu-19.10-desktop-amd64.iso&tr=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr=https%3A%2F%2Fipv6.torrent.ubuntu.com%2Fannounce"

Run it as an HTTP server.

magnet2torrent serve

Run it as an HTTP server with lots of features enabled.

magnet2torrent --use-dht --dht-state-file dht.state --torrent-cache-folder torcache serve --apikey secretkey

# try to fetch a torrent from the running server
# The response is json encoded and contains either the torrent or an error about it
curl "http://127.0.0.1:18667/?apikey=secretkey&magnet=magnet%3A%3Fxt%3Durn%3Abtih%3Ae2467cbf021192c241367b892230dc1e05c0580e%26dn%3Dubuntu-19.10-desktop-amd64.iso%26tr%3Dhttps%253A%252F%252Ftorrent.ubuntu.com%252Fannounce%26tr%3Dhttps%253A%252F%252Fipv6.torrent.ubuntu.com%252Fannounce"
# it will return {"status": "success", "filename": "ubuntu-19.10-desktop-amd64.iso.torrent", "torrent_data": "... base64 encoded torrent data ..."}

Use from python

import asyncio

from magnet2torrent import Magnet2Torrent, FailedToFetchException

async def fetch_that_torrent():
    m2t = Magnet2Torrent("magnet:?xt=urn:btih:e2467cbf021192c241367b892230dc1e05c0580e&dn=ubuntu-19.10-desktop-amd64.iso&tr=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr=https%3A%2F%2Fipv6.torrent.ubuntu.com%2Fannounce")
    try:
        filename, torrent_data = await m2t.retrieve_torrent()
    except FailedToFetchException:
        print("Failed")

asyncio.run(fetch_that_torrent())

If you want to use DHT to retrieve, you will have to bootstrap and run it.

import asyncio
import os

from magnet2torrent import Magnet2Torrent, FailedToFetchException, settings


DHT_STATE_FILE = "/tmp/dht.state"

async def start_dht():
    if os.path.exists(DHT_STATE_FILE):
        dht_server = DHTServer.load_state(DHT_STATE_FILE)
        await dht_server.listen(settings.DHT_PORT)
    else:
        dht_server = DHTServer()
        await dht_server.listen(settings.DHT_PORT)
        await dht_server.bootstrap(settings.DHT_BOOTSTRAP_NODES)
    return dht_server

async def fetch_that_torrent(dht_server):
    m2t = Magnet2Torrent("magnet:?xt=urn:btih:e2467cbf021192c241367b892230dc1e05c0580e&dn=ubuntu-19.10-desktop-amd64.iso", dht_server=dht_server)
    try:
        filename, torrent_data = await m2t.retrieve_torrent()
    except FailedToFetchException:
        print("Failed")

dht_server = asyncio.run(start_dht())
asyncio.run(fetch_that_torrent(dht_server))
dht_server.save_state(DHT_STATE_FILE)

Attacks on DHT

There are a number of attacks against Bittorrent DHT going on permanently. They have a variety of goals like trying to find new content on the DHT or just disrupt its operation.

One specific affects magnet2torrent, the "i am the peer for this" and then give back zero peers or just itself. This attack kinda short circuits the attempt to find a torrent. It mostly happen with low-peer torrents and when only the DHT got peers so it will be a bit uncommon.

The question I'm trying to answer here is "why can deluge/qbittorrent/picotorret etc. find a torrent when this library cannot". And that's probably why, libtorrent-rasterbar is smarter about it.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

The DHT part is forked from bmueller/kademlia - its license can be found in the dht folder or in the original project.

Metadata

Release files for magnet2torrent 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magnet2torrent 1.2.1
File Size Uploaded
magnet2torrent-1.2.1.tar.gz 24.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for magnet2torrent 1.2.1
File Interpreter ABI Platform
magnet2torrent-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 52.7 kB

Release files / magnet2torrent-1.2.1.tar.gz

Download URL magnet2torrent-1.2.1.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4ba53d304f1db33d33fe227aee5b34f6e640fd2d3199ff0ec3d8cc050cded631
BLAKE2b-256 checksum
How to use checksums
113562b00d45692cc88d14245044c8e19f9d462a5a00227a406927c397103787
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / magnet2torrent-1.2.1-py3-none-any.whl

Download URL magnet2torrent-1.2.1-py3-none-any.whl
Size 28.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f909e0f58776167b7c078b38bfdcc91663db2b9fd5af86b1c1473054a9f34e0f
BLAKE2b-256 checksum
How to use checksums
cd6454a0c46c9994cc6556f4af1788c8bae89e20deb57385ef87a70263a03836
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

1 release file

1.0.4

1 release file

1.0.3

2 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page