pythonbittorrentlibtorrentmagnet-urilibtorrent-rasterbar

Python/Libtorrent: Retrieve metadata from a magnet link without downloading files


I'm trying to just get the torrent name or a list of files in the torrent without actually downloading them.

This is my current code, it is functional but slow, as it downloads the torrent contents.

import libtorrent as lt
import time

ses = lt.session()
params = {
    'save_path': 'media/',
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True
}
handle = lt.add_magnet_uri(ses, "magnet:?xt=urn:btih:9fea16aff4ece16e04f98321668a265f0fd22b7e&dn=archlinux-2017.08.01-x86_64.iso&tr=udp://tracker.archlinux.org:6969&tr=http://tracker.archlinux.org:6969/announce", params)
while(not handle.has_metadata()):
    time.sleep(1)

print(handle.get_torrent_info().name())

I've also tried using lt.parse_magnet_uri(), but this does not return what I am looking for, or much of anything really past the info hash.


Solution

  • I ended up solving my problem by utilizing the 'file_priorities' property in params. This still downloads the files until the metadata is retrieved, however, so I just saved them to a temporary directory.

    import libtorrent as lt
    import time
    import tempfile
    
    ses = lt.session()
    params = {
        'save_path': tempfile.mkdtemp(),
        'storage_mode': lt.storage_mode_t(2),
        'auto_managed': True,
        'file_priorities': [0]*5
    }
    
    handle = lt.add_magnet_uri(ses, "magnet:?xt=urn:btih:9fea16aff4ece16e04f98321668a265f0fd22b7e&dn=archlinux-2017.08.01-x86_64.iso&tr=udp://tracker.archlinux.org:6969&tr=http://tracker.archlinux.org:6969/announce", params)
    while(not handle.has_metadata()):
        time.sleep(1)
    
    print(handle.get_torrent_info().name())