我已阅读手册,但找不到答案。给定一个磁力链接,我想生成一个 torrent 文件,以便在下次启动时加载它,以避免重新下载元数据。我已经尝试过快速恢复功能,但我仍然需要在执行此操作时获取元数据,这可能需要相当长的时间。我见过的例子是为一个新的 torrent 创建 torrent 文件,我想创建一个匹配磁铁 uri 的文件。
4 回答
在这里找到的解决方案:
http://code.google.com/p/libtorrent/issues/detail?id=165#c5
请参阅创建洪流:
http://www.rasterbar.com/products/libtorrent/make_torrent.html
修改第一行:
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_metadata()
.
更新
似乎 libtorrent python api 缺少一些从磁铁创建 torrent 所需的重要 c++ api,上面的示例在 python 中不起作用,因为create_torrent
python 类不接受 torrent_info 作为参数(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)
它期望 hash 是const char*
但torrent_info.hash_for_piece(int)
返回的类big_number
没有 api 将其转换回 const char*。
当我找到一些时间时,我会将这个缺失的 api 错误报告给 libtorrent 开发人员,因为目前在使用 python 绑定时无法从磁铁 uri 创建 .torrent 文件。
torrent_info.orig_files()
python绑定中也缺少,我不确定是否torrent_info.files()
足够。
更新 2
我已经为此创建了一个问题,请在此处查看: http ://code.google.com/p/libtorrent/issues/detail?id=294
给它加星标,以便他们快速修复它。
更新 3
它现在已修复,有一个 0.16.0 版本。Windows 的二进制文件也可用。
只是想使用现代 libtorrent Python 包提供快速更新:libtorrent 现在具有parse_magnet_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
尝试查看此代码http://code.google.com/p/libtorrent/issues/attachmentText?id=165&aid=-5595452662388837431&name=java_client.cpp&token=km_XkD5NBdXitTaBwtCir8bN-1U%3A1327784186190 它使用 add_magnet_uri 我认为这是你需要的
如果保存简历数据对您不起作用,您可以使用现有连接中的信息生成新的 torrent 文件。
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()
我怀疑它会使简历比专门为此目的构建的功能更快。