Libtorrent -给定一个磁铁链接,你如何生成一个torrent文件
Libtorrent - Given a magnet link, how do you generate a torrent file?
我已经通读了手册,但还是找不到答案。给定一个磁铁链接,我想生成一个种子文件,以便它可以在下次启动时加载,以避免重新下载元数据。我已经尝试了快速简历功能,但我仍然需要获取元数据,当我这样做,这可能需要相当多的时间。我所看到的例子是为一个新的洪流创建洪流文件,其中我想创建一个匹配磁铁uri的洪流文件。
解决方案:
http://code.google.com/p/libtorrent/issues/detail?id=165 c5
参见创建torrent:
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)
"handle"来自这里:
torrent_handle add_magnet_uri(session& ses, std::string const& uri add_torrent_params p);
在创建种子之前,你必须确保元数据已经下载,通过调用handle.has_metadata()
来完成。
似乎libtorrent python api缺少一些重要的c++ api,这些api需要从磁体创建torrent,上面的例子在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)
期望哈希值为const char*
,但torrent_info.hash_for_piece(int)
返回的类big_number
没有api将其转换回const char*。
当我找到一些时间,我会报告这个缺失的api错误libtorrent开发人员,因为目前是不可能从磁铁uri创建一个.torrent文件时,使用python绑定。
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
如果保存简历数据对您不起作用,您可以使用现有连接中的信息生成新的种子文件。
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()
我怀疑它是否会比专门为此目的构建的函数更快。
尝试查看此代码http://code.google.com/p/libtorrent/issues/attachmentText?id=165&aid=-5595452662388837431&name=java_client.cpp&token=km_XkD5NBdXitTaBwtCir8bN-1U%3A1327784186190它使用add_magnet_uri我认为这是你需要的
- 我在c++代码中生成了一个运行时#3异常
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 如何生成一个随机的 n 位数,其中 n 是任意的
- 有没有办法生成一个包含平方的序列,这些平方加起来就是一个整数平方?
- 如何在一个表达式中生成并返回结果?
- 我将如何在 cpp 中生成一个随机且独特的标题
- 生成一个类Name_class并将两种数据类型存储在一个向量中
- 从大范围内生成一个随机数,但从大范围的中间开始生成较小的范围
- 在一个生成文件中编译 C++ 和 C
- 生成一个使用 boost 的 .cu 文件
- 在 C++17 中的每个实例化时生成一个新类型
- 我在一个生成随机数的程序工作,我想将这些数字存储在一个数组中
- 如何调用参数由另一个函数生成的函数?
- 使一个类生成另一个类
- Python 从一个文件生成不同的文件
- MPI,通过其中一个过程生成一个孩子
- 提升因果报应-从一个属性生成多个字符串
- 从一个集合生成所有可能的有序子集
- 函数使用从另一个函数生成的字符串
- 可变模板——从另一个类生成一个c++类