【问题标题】:Libtorrent - Given a magnet link, how do you generate a torrent file?Libtorrent - 给定一个磁力链接,你如何生成一个 torrent 文件?
【发布时间】:2011-12-31 18:29:47
【问题描述】:

我已通读manual,但找不到答案。给定一个磁力链接,我想生成一个 torrent 文件,以便在下次启动时加载它,以避免重新下载元数据。我已经尝试过快速恢复功能,但我仍然需要在执行此操作时获取元数据,这可能需要相当长的时间。我看到的示例是为新的 torrent 创建 torrent 文件,而我想创建一个匹配磁铁 uri 的文件。

【问题讨论】:

标签: c++ python libtorrent


【解决方案1】:

在这里找到解决方案:

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*。

当我找到时间时,我会向 libtorrent 开发人员报告这个缺失的 api 错误,因为目前在使用 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 的二进制文件也可用。

【讨论】:

    【解决方案2】:

    只是想使用现代 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
    

    【讨论】:

      【解决方案3】:

      如果保存 resume data 对您不起作用,您可以使用现有连接中的信息生成新的 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()
      

      我怀疑它会使简历比专门为此目的构建的函数更快。

      【讨论】:

        【解决方案4】:
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 2018-01-21
        相关资源
        最近更新 更多