C++ Libtorrent-给定磁铁链接,如何生成torrent文件?
我已经通读了这本书,但找不到答案。给定一个磁铁链接,我想生成一个torrent文件,以便在下一次启动时加载它,以避免重新下载元数据。我已经尝试过快速恢复功能,但我仍然需要在执行时获取元数据,这可能需要相当长的时间。我看到的示例是为新的torrent创建torrent文件,我想创建一个与磁铁uri匹配的文件。如果保存文件对您无效,您可以使用现有连接中的信息生成新的torrent文件C++ Libtorrent-给定磁铁链接,如何生成torrent文件?,c++,python,libtorrent,C++,Python,Libtorrent,我已经通读了这本书,但找不到答案。给定一个磁铁链接,我想生成一个torrent文件,以便在下一次启动时加载它,以避免重新下载元数据。我已经尝试过快速恢复功能,但我仍然需要在执行时获取元数据,这可能需要相当长的时间。我看到的示例是为新的torrent创建torrent文件,我想创建一个与磁铁uri匹配的文件。如果保存文件对您无效,您可以使用现有连接中的信息生成新的torrent文件 fs = libtorrent.file_storage() libtorrent.add_files(fs, "s
fs = libtorrent.file_storage()
libtorrent.add_files(fs, "somefiles")
t = libtorrent.create_torrent(fs)
t.add_tracker("http://10.0.0.1:312/announce")
t.set_creator("My Torrent")
t.set_comment("Some comments")
t.set_priv(True)
libtorrent.set_piece_hashes(t, "C:\\", lambda x: 0), libtorrent.bencode(t.generate())
f=open("mytorrent.torrent", "wb")
f.write(libtorrent.bencode(t.generate()))
f.close()
我怀疑它是否会使简历比专门为此目的构建的功能更快。尝试查看此代码
它使用add_magnet_uri,我认为这是您需要的解决方案,可以在这里找到:
请参见创建torrent:
修改第一行:
file_storage fs;
// recursively adds files in directories
add_files(fs, "./my_torrent");
create_torrent t(fs);
为此:
torrent_info ti = handle.get_torrent_info()
create_torrent t(ti)
“句柄”是从这里开始的:
torrent_handle add_magnet_uri(session& ses, std::string const& uri add_torrent_params p);
另外,在创建torrent之前,您必须确保元数据已下载,请通过调用handle.has\u metadata()
执行此操作
更新
好像LyTrryPython API缺少一些从磁铁创建激流所需的重要C++ API,上面的例子在Python中不起作用。代码> CureTyTrRoo< /Cord>类Python类不接受TrrTuniFig作为参数(C++有可用)。 所以我尝试了另一种方法,但也遇到了一堵砖墙,这使它变得不可能,下面是代码:
if handle.has_metadata():
torinfo = handle.get_torrent_info()
fs = libtorrent.file_storage()
for file in torinfo.files():
fs.add_file(file)
torfile = libtorrent.create_torrent(fs)
torfile.set_comment(torinfo.comment())
torfile.set_creator(torinfo.creator())
for i in xrange(0, torinfo.num_pieces()):
hash = torinfo.hash_for_piece(i)
torfile.set_hash(i, hash)
for url_seed in torinfo.url_seeds():
torfile.add_url_seed(url_seed)
for http_seed in torinfo.http_seeds():
torfile.add_http_seed(http_seed)
for node in torinfo.nodes():
torfile.add_node(node)
for tracker in torinfo.trackers():
torfile.add_tracker(tracker)
torfile.set_priv(torinfo.priv())
f = open(magnet_torrent, "wb")
f.write(libtorrent.bencode(torfile.generate()))
f.close()
此行上引发了一个错误:
torfile.set_hash(i, hash)
它期望散列为const char*
但torrent\u info。散列为块(int)
返回类big\u number
,该类没有api将其转换回const char*
当我有时间的时候,我会向libtorrent开发人员报告这个缺失的api错误,因为目前在使用python绑定时,不可能从磁铁uri创建.torrent文件
python绑定中也缺少torrent\u info.orig\u files()
,我不确定torrent\u info.files()
是否足够
更新2
我已就此创建了一个问题,请参见此处:
启动它,以便他们快速修复它
更新3
现在已经修复,有一个0.16.0版本。windows的二进制文件也可用。只是想使用现代的libtorrent Python包提供一个快速更新:libtorrent现在有了
parse\u magnet\u uri
方法,可用于生成torrent句柄:
import libtorrent, os, time
def magnet_to_torrent(magnet_uri, dst):
"""
Args:
magnet_uri (str): magnet link to convert to torrent file
dst (str): path to the destination folder where the torrent will be saved
"""
# Parse magnet URI parameters
params = libtorrent.parse_magnet_uri(magnet_uri)
# Download torrent info
session = libtorrent.session()
handle = session.add_torrent(params)
print "Downloading metadata..."
while not handle.has_metadata():
time.sleep(0.1)
# Create torrent and save to file
torrent_info = handle.get_torrent_info()
torrent_file = libtorrent.create_torrent(torrent_info)
torrent_path = os.path.join(dst, torrent_info.name() + ".torrent")
with open(torrent_path, "wb") as f:
f.write(libtorrent.bencode(torrent_file.generate()))
print "Torrent saved to %s" % torrent_path
诚然,文档是可怕的(尽管使用起来奇怪)。但是,元数据扩展有什么问题吗?