torrent_info() and magnet links in libtorrent python bindings
Asked Answered
N

1

3

I was searching how to pass an argument in torrent_info() function during the use of magnet links in libtorrent. Especially my target is to analyze peers and pieces. With the use of .torrent file the process is obvious throw other given paradigms in this site:

e = lt.bdecode(open("torrent.torrent", 'rb').read())
info = lt.torrent_info(e)

But what happens with the magnet links?

params = {
    'save_path': 'C:\Python26',
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True}
link = "magnet:?........."

handle = lt.add_magnet_uri(ses, link, params)

Which variable is equivalent to "e" of the .torrent process in magnet links case in order to be able to use torrent_info function properly?

Natachanatal answered 20/4, 2012 at 18:10 Comment(0)
L
6

Adding a magnet link gives you a torrent handle, from which you will be able to get torrent infos (once the metadata has been fetched - it will throw otherwise).

Unlike torrent files, where the metadata is already here, magnet links require the metadata to be retrieved from the network as a starter, and that can take some time.

I'm more used to the C++ library, but well - to have it demo at the dirtiest, you can do something in the line of:

handle = lt.add_magnet_uri(ses, link, params)
while (not handle.has_metadata()):
   time.sleep(.1)
info = handle.get_torrent_info()

... then, you can read all about it here ;) http://www.rasterbar.com/products/libtorrent/manual.html#torrent-info

Linkoski answered 18/6, 2012 at 20:12 Comment(3)
Thanks a lot. This will prove useful in adding magnet link support in my program.Knowitall
Would you be kind enough to post the C++ equivalent of the Python code you have?Forwardness
Sure. Examples here: github.com/Roxee/qt-roxeetorrent/blob/master/src/… to add a Magnet link. And here github.com/Roxee/qt-roxeetorrent/blob/master/src/… to get some info from the handle, if metadata is here. All that code is messy, but you will get the idea. Obviously we don't "wait" in C++, though you could use metadata_received_alert (rasterbar.com/products/libtorrent/manual.html) to get notified that the metadata is here and do something. Ping me on github if you can't figure it out.Linkoski

© 2022 - 2024 — McMap. All rights reserved.