Libtorrent - 给定磁力链接,你如何生成一个torrent文件?

时间:2022-11-23 09:59:38

I have read through the manual and I cannot find the answer. Given a magnet link I would like to generate a torrent file so that it can be loaded on the next startup to avoid redownloading the metadata. I have tried the fast resume feature, but I still have to fetch meta data when I do it and that can take quite a bit of time. Examples that I have seen are for creating torrent files for a new torrent, where as I would like to create one matching a magnet uri.

我已通读了手册,但找不到答案。给定一个磁力链接我想生成一个torrent文件,以便它可以在下次启动时加载,以避免重新加载元数据。我尝试过快速恢复功能,但是当我这样做时仍然需要获取元数据,这可能需要相当长的时间。我见过的例子是为新的torrent创建torrent文件,我想在其中创建一个匹配磁体uri。

4 个解决方案

#1


12  

Solution found here:

解决方案在这里:

http://code.google.com/p/libtorrent/issues/detail?id=165#c5

See creating torrent:

查看创建torrent:

http://www.rasterbar.com/products/libtorrent/make_torrent.html

Modify first lines:

修改第一行:

file_storage fs;

// recursively adds files in directories
add_files(fs, "./my_torrent");

create_torrent t(fs);

To this:

torrent_info ti = handle.get_torrent_info()

create_torrent t(ti)

"handle" is from here:

“句柄”来自这里:

torrent_handle add_magnet_uri(session& ses, std::string const& uri add_torrent_params p);

Also before creating torrent you have to make sure that metadata has been downloaded, do this by calling handle.has_metadata().

在创建torrent之前,您必须确保已下载元数据,请通过调用handle.has_metadata()来执行此操作。

UPDATE

Seems like libtorrent python api is missing some of important c++ api that is required to create torrent from magnets, the example above won't work in python cause create_torrent python class does not accept torrent_info as parameter (c++ has it available).

好像libtorrent python api缺少从磁体创建torrent所需的一些重要的c ++ api,上面的例子在python中不起作用,因为create_torrent python类不接受torrent_info作为参数(c ++有它可用)。

So I tried it another way, but also encountered a brick wall that makes it impossible, here is the code:

所以我尝试了另一种方式,但也遇到了一个让它变得不可能的砖墙,这里是代码:

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()

There is an error thrown on this line:

这一行引发了一个错误:

torfile.set_hash(i, hash)

It expects hash to be const char* but torrent_info.hash_for_piece(int) returns class big_number which has no api to convert it back to const char*.

它希望hash是const char *但是torrent_info.hash_for_piece(int)返回类big_number,它没有api将它转换回const char *。

When I find some time I will report this missing api bug to libtorrent developers, as currently it is impossible to create a .torrent file from a magnet uri when using python bindings.

当我找到一些时间我将向libtorrent开发人员报告这个丢失的api bug时,因为目前在使用python绑定时不可能从磁体uri创建.torrent文件。

torrent_info.orig_files() is also missing in python bindings, I'm not sure whether torrent_info.files() is sufficient.

python绑定中也缺少torrent_info.orig_files(),我不确定torrent_info.files()是否足够。

UPDATE 2

I've created an issue on this, see it here: http://code.google.com/p/libtorrent/issues/detail?id=294

我在此处创建了一个问题,请在此处查看:http://code.google.com/p/libtorrent/issues/detail?id = 294

Star it so they fix it fast.

明星,以便他们快速修复它。

UPDATE 3

It is fixed now, there is a 0.16.0 release. Binaries for windows are also available.

它现在已修复,有0.16.0版本。 Windows的二进制文件也可用。

#2


5  

Just wanted to provide a quick update using the modern libtorrent Python package: libtorrent now has the parse_magnet_uri method which you can use to generate a torrent handle:

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


0  

If saving the resume data didn't work for you, you are able to generate a new torrent file using the information from the existing connection.

如果保存简历数据不适合您,则可以使用现有连接中的信息生成新的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()

I doubt that it'll make the resume faster than the function built specifically for this purpose.

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

#4


0  

Try to see this code http://code.google.com/p/libtorrent/issues/attachmentText?id=165&aid=-5595452662388837431&name=java_client.cpp&token=km_XkD5NBdXitTaBwtCir8bN-1U%3A1327784186190 it uses add_magnet_uri which I think is what you need

尝试查看此代码http://code.google.com/p/libtorrent/issues/attachmentText?id=165&aid=-5595452662388837431&name=java_client.cpp&token=km_XkD5NBdXitTaBwtCir8bN-1U%3A1327784186190它使用了add_magnet_uri,我认为这就是你需要的

#1


12  

Solution found here:

解决方案在这里:

http://code.google.com/p/libtorrent/issues/detail?id=165#c5

See creating torrent:

查看创建torrent:

http://www.rasterbar.com/products/libtorrent/make_torrent.html

Modify first lines:

修改第一行:

file_storage fs;

// recursively adds files in directories
add_files(fs, "./my_torrent");

create_torrent t(fs);

To this:

torrent_info ti = handle.get_torrent_info()

create_torrent t(ti)

"handle" is from here:

“句柄”来自这里:

torrent_handle add_magnet_uri(session& ses, std::string const& uri add_torrent_params p);

Also before creating torrent you have to make sure that metadata has been downloaded, do this by calling handle.has_metadata().

在创建torrent之前,您必须确保已下载元数据,请通过调用handle.has_metadata()来执行此操作。

UPDATE

Seems like libtorrent python api is missing some of important c++ api that is required to create torrent from magnets, the example above won't work in python cause create_torrent python class does not accept torrent_info as parameter (c++ has it available).

好像libtorrent python api缺少从磁体创建torrent所需的一些重要的c ++ api,上面的例子在python中不起作用,因为create_torrent python类不接受torrent_info作为参数(c ++有它可用)。

So I tried it another way, but also encountered a brick wall that makes it impossible, here is the code:

所以我尝试了另一种方式,但也遇到了一个让它变得不可能的砖墙,这里是代码:

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()

There is an error thrown on this line:

这一行引发了一个错误:

torfile.set_hash(i, hash)

It expects hash to be const char* but torrent_info.hash_for_piece(int) returns class big_number which has no api to convert it back to const char*.

它希望hash是const char *但是torrent_info.hash_for_piece(int)返回类big_number,它没有api将它转换回const char *。

When I find some time I will report this missing api bug to libtorrent developers, as currently it is impossible to create a .torrent file from a magnet uri when using python bindings.

当我找到一些时间我将向libtorrent开发人员报告这个丢失的api bug时,因为目前在使用python绑定时不可能从磁体uri创建.torrent文件。

torrent_info.orig_files() is also missing in python bindings, I'm not sure whether torrent_info.files() is sufficient.

python绑定中也缺少torrent_info.orig_files(),我不确定torrent_info.files()是否足够。

UPDATE 2

I've created an issue on this, see it here: http://code.google.com/p/libtorrent/issues/detail?id=294

我在此处创建了一个问题,请在此处查看:http://code.google.com/p/libtorrent/issues/detail?id = 294

Star it so they fix it fast.

明星,以便他们快速修复它。

UPDATE 3

It is fixed now, there is a 0.16.0 release. Binaries for windows are also available.

它现在已修复,有0.16.0版本。 Windows的二进制文件也可用。

#2


5  

Just wanted to provide a quick update using the modern libtorrent Python package: libtorrent now has the parse_magnet_uri method which you can use to generate a torrent handle:

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


0  

If saving the resume data didn't work for you, you are able to generate a new torrent file using the information from the existing connection.

如果保存简历数据不适合您,则可以使用现有连接中的信息生成新的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()

I doubt that it'll make the resume faster than the function built specifically for this purpose.

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

#4


0  

Try to see this code http://code.google.com/p/libtorrent/issues/attachmentText?id=165&aid=-5595452662388837431&name=java_client.cpp&token=km_XkD5NBdXitTaBwtCir8bN-1U%3A1327784186190 it uses add_magnet_uri which I think is what you need

尝试查看此代码http://code.google.com/p/libtorrent/issues/attachmentText?id=165&aid=-5595452662388837431&name=java_client.cpp&token=km_XkD5NBdXitTaBwtCir8bN-1U%3A1327784186190它使用了add_magnet_uri,我认为这就是你需要的