マニュアルを読みましたが、答えが見つかりません。マグネットリンクを指定して、メタデータの再ダウンロードを回避するために次回の起動時にロードできるようにトレントファイルを生成したいと思います。高速レジューム機能を試しましたが、それでもメタデータをフェッチする必要があり、かなりの時間がかかる可能性があります。私が見た例は、新しいトレントのトレントファイルを作成するためのものですが、マグネット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);
また、トレントを作成する前に、メタデータがダウンロードされていることを確認する必要があります。これを行うには、を呼び出しhandle.has_metadata()
ます。
アップデート
libtorrent python apiには、マグネットからトレントを作成するために必要な重要なc ++ apiの一部が欠落しているようです。上記の例は、create_torrent
pythonクラスがtorrent_infoをパラメーターとして受け入れないため(c ++で使用可能)、pythonでは機能しません。
だから私は別の方法でそれを試しましたが、それを不可能にするレンガの壁にも遭遇しました、ここにコードがあります:
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*
が、それをconstchar*に変換するためのAPIを持たないtorrent_info.hash_for_piece(int)
クラスを返します。big_number
現在、Pythonバインディングを使用している場合、マグネットURIから.torrentファイルを作成することは不可能であるため、いつかこの欠落しているapiバグをlibtorrent開発者に報告します。
torrent_info.orig_files()
Pythonバインディングにもありませんtorrent_info.files()
。十分かどうかはわかりません。
更新2
これに関する問題を作成しました。こちらをご覧ください: http ://code.google.com/p/libtorrent/issues/detail?id = 294
スターを付けて、すばやく修正します。
更新3
現在修正されており、0.16.0のリリースがあります。Windows用のバイナリも利用できます。
最新のlibtorrentPythonパッケージを使用してクイックアップデートを提供したかっただけです。libtorrentparse_magnet_uri
には、トレントハンドルを生成するために使用できるメソッドがあります。
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()
この目的のために特別に構築された関数よりも再開が速くなるとは思えません。