Simple libtorrent Python client
Asked Answered
D

2

0

I tried creating a simple libtorrent python client (for magnet uri), and I failed, the program never continues past the "downloading metadata". If you may help me write a simple client it would be amazing.

P.S. When I choose a save path, is the save path the folder which I want my data to be saved in? or the path for the data itself.

(I used a code someone posted here)

import libtorrent as lt
import time

ses = lt.session()
ses.listen_on(6881, 6891)
params = {
'save_path': '/home/downloads/',
'storage_mode': lt.storage_mode_t(2),
'paused': False,
'auto_managed': True,
'duplicate_is_error': True}
 link = "magnet:?xt=urn:btih:4MR6HU7SIHXAXQQFXFJTNLTYSREDR5EI&tr=http://tracker.vodo.net:6970/announce"
handle = lt.add_magnet_uri(ses, link, params)
ses.start_dht()

print 'downloading metadata...'
while (not handle.has_metadata()):
    time.sleep(1)
print 'got metadata, starting torrent download...'
while (handle.status().state != lt.torrent_status.seeding):
    s = handle.status()
    state_str = ['queued', 'checking', 'downloading metadata', \
            'downloading', 'finished', 'seeding', 'allocating']
    print '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s %.3' % \
            (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
            s.num_peers, state_str[s.state], s.total_download/1000000)
    time.sleep(5)
Duffer answered 13/11, 2015 at 14:7 Comment(1)
It would help if you can post your code.Bonne
H
2

What happens it is that the first while loop becomes infinite because the state does not change.

You have to add a s = handle.status (); for having the metadata the status changes and the loop stops. Alternatively add the first while inside the other while so that the same will happen.

Heller answered 9/8, 2016 at 20:49 Comment(0)
B
0

Yes, the save path you specify is the one that the torrents will be downloaded to.

As for the metadata downloading part, I would add the following extensions first:

ses.add_extension(lt.create_metadata_plugin)
ses.add_extension(lt.create_ut_metadata_plugin)

Second, I would add a DHT bootstrap node:

ses.add_dht_router("router.bittorrent.com", 6881)

Finally, I would begin debugging the application by seeing if my network interface is binding or if any other errors come up (my experience with BitTorrent download problems, in general, is that they are network related). To get an idea of what's happening I would use libtorrent-rasterbar's alert system:

ses.set_alert_mask(lt.alert.category_t.all_categories)

And make a thread (with the following code) to collect the alerts and display them:

while True:
    ses.wait_for_alert(500)
    alert = lt_session.pop_alert()

    if not alert:
        continue

    print "[%s] %s" % (type(alert), alert.__str__())

Even with all this working correctly, make sure that torrent you are trying to download actually has peers. Even if there are a few peers, none may be configured correctly or support metadata exchange (exchanging metadata is not a standard BitTorrent feature). Try to load a torrent file (which doesn't require downloading metadata) and see if you can download successfully (to rule out some network issues).

Barnet answered 26/11, 2015 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.