2
def magnet2torrent(id, info_hash):

    info_hash_id=id

    magnet="magnet:?xt=urn:btih:"+info_hash

    ses = lt.session()
    params = {
        'save_path': './',
        'duplicate_is_error': True,
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    handle = lt.add_magnet_uri(ses, magnet, params)

    print("Downloading Metadata (this may take a while)")
    i = 0;
    while (not handle.has_metadata()):
#        i = i+1
        if i > 300 :
            return
        sleep(1)
    ses.pause()
    print("Done")

    torinfo = handle.get_torrent_info()
    con = db.get_conncetion()
    cur = con.cursor()
    for f in torinfo:
        cur.execute("INSERT INTO file_list (info_hash_id, name, size) VALUES (\""+str(info_hash_id)+"\", \""+str(f.path)+"\", "+str(f.size)+");")
        print("INSERT INTO file_list (info_hash_id, name, size) VALUES (\""+str(info_hash_id)+"\", \""+str(f.path)+"\", "+str(f.size)+");")
        con.commit()
        cur.close()
        con.close()

I think if I can get torrent file from info_hash then I can get file list from torrent file.

but when I run my code

while (not handle.has_metadata()):
not ended. but webpage like http://magnet2torrent.com give me torrent immediately

How can I get file list from info_hash?

1 Answers1

2

if you don't have any trackers in your magnet links, you need to start the DHT. You can do that by calling:

ses.add_dht_router("router.utorrent.com", 6881)
ses.start_dht()

after constructing the session object.

Arvid
  • 10,915
  • 1
  • 32
  • 40
  • I add that code like
        magnet="magnet:?xt=urn:btih:"+info_hash
    
        ses = lt.session()
        ses.add_dht_router("router.utorrent.com", 6881)
        ses.start_dht()
        params = {
            'save_path': './',
            'duplicate_is_error': True,
            'storage_mode': lt.storage_mode_t(2),
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        handle = lt.add_magnet_uri(ses, magnet, params)
    but loop not ended
    – user2648192 Nov 23 '14 at 08:46
  • do you find any peers at all? do you end up having any nodes in the DHT routing table? – Arvid Nov 24 '14 at 07:53