Convert Torrent Magnet link to a .torrent file with c#

3.5k views Asked by At

Is there a way to do it? I already tried with monotorrent, but due to the lack of up-to-date documentation i coudn't get it to work. I've already tried this with monotorrent, but still i can't find a way to get the .torrent file or even start the download to get the .torrent ...

The following piece of code was made that question as base, but it doesn't save anything to "D:\A" or to "D:\TorrentSave"

    private void GerarTorrent(string magnet)
    {
        MonoTorrent.Client.TorrentManager b = new MonoTorrent.Client.TorrentManager(new MonoTorrent.MagnetLink(magnet), "D:\\A", new MonoTorrent.Client.TorrentSettings(), "D:\\TorrentSave");
        MonoTorrent.Client.EngineSettings engineSettings = new MonoTorrent.Client.EngineSettings();
        MonoTorrent.Client.ClientEngine clientEngine = new MonoTorrent.Client.ClientEngine(engineSettings);
        clientEngine.Register(b);
        clientEngine.StartAll();
        b.Start();
    }

To generate the .torrent, it doesn't have to be monotorrent, in fact the only usage of this api would be for that, generating .torrent files from a magnet link...

EDIT: Updated code with my attempt on doing what Fᴀʀʜᴀɴ Aɴᴀᴍ said:

    private void GerarTorrent(string hash)
    {
        MonoTorrent.Client.TorrentManager b = new MonoTorrent.Client.TorrentManager(MonoTorrent.InfoHash.FromHex(hash), "D:\\A", new MonoTorrent.Client.TorrentSettings(), "D:\\TorrentSave", new List<List<string>>());
        MonoTorrent.Client.EngineSettings engineSettings = new MonoTorrent.Client.EngineSettings();
        MonoTorrent.Client.ClientEngine clientEngine = new MonoTorrent.Client.ClientEngine(engineSettings);
        clientEngine.Register(b);
        clientEngine.StartAll();
        b.Start();
    }

Hash used = "5FC86BA08451CF4221E0091F31AF1A52C2219009"

1

There are 1 answers

5
Fᴀʀʜᴀɴ Aɴᴀᴍ On

You need to pass only the hash and not the entire magnet link to the TorrentManager constructor.

A magnet link looks like this:

magnet:?xt=urn:btih:18981bc9759950b4715ad46adcaf514e6a773cfe

So, a more generalized form:

magnet:?xt=urn:btih:<hash>

You need to extract this <hash> and pass it to the constructor:

manager = new TorrentManager(InfoHash.FromHex(hash), downloadsPath, torrentDefaults, downloadsPathForTorrent, new List<List<string>>());