Python libtorrent使用磁铁链接创建空的torrent

时间:2015-11-26 10:28:10

标签: python bittorrent libtorrent magnet-uri libtorrent-rasterbar

我尝试下载一个torrent(特定的.torrent文件),只有 一个info_hash。我知道之前已经讨论过这个,我甚至搜索并修改了我的代码。结果如下:

import libtorrent as lt
import time
import sys
import bencode

ses = lt.session()
ses.listen_on(6881, 6891)
params = {
    'save_path': '.',
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True
    }

info_hash = "2B3AF3B4977EB5485D39F96FE414729530F48386"
link = "magnet:?xt=urn:btih:" + info_hash

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

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

while (not h.has_metadata()):
    time.sleep(1)

torinfo = h.get_torrent_info()

fs = lt.file_storage()
for f in torinfo.files():
  fs.add_file(f)
torfile = lt.create_torrent(fs)
torfile.set_comment(torinfo.comment())
torfile.set_creator(torinfo.creator())

f = open("torrentfile.torrent", "wb")
f.write(lt.bencode(torfile.generate()))
f.close()

这会产生一个无法通过传输加载的torrent文件。它缺少跟踪器和真实的碎片(创建\ x00而不是实际碎片) 以下行保存碎片,但仍然缺少跟踪器,无法通过传输打开:

f = open("torrentfile.torrent", "wb")
f.write(lt.bencode(torinfo.metadata()))
f.close()

如何通过使用磁力链接(如代码中所述)创建看起来像实际洪流的洪流? (我使用的是Ubuntu 15.04 x64和libtorrent 0.16.18-1)

我不是非法下载洪流背后的文件 - 但是,我有洪流可以与我的脚本下载的洪流进行比较。

1 个答案:

答案 0 :(得分:1)

您没有设置片段哈希和片段大小(file_storage对象的片段大小)。请参阅documentation

但是,创建.torrent文件的更简单,更健壮的方法是使用直接接受create_torrent对象的torrent_info构造函数。即:

torfile = lt.create_torrent(h.get_torrent_info())
f = open("torrentfile.torrent", "wb")
f.write(lt.bencode(torfile.generate()))
f.close()
相关问题