2011-12-31 23 views
17

Tôi đã đọc qua số manual và tôi không thể tìm thấy câu trả lời. Với một liên kết nam châm, tôi muốn tạo một tệp torrent để nó có thể được tải vào lần khởi động tiếp theo để tránh tải xuống lại siêu dữ liệu. Tôi đã thử tính năng tiếp tục nhanh, nhưng tôi vẫn phải tìm nạp dữ liệu meta khi tôi thực hiện và có thể mất khá nhiều thời gian. Ví dụ mà tôi đã thấy là để tạo ra các tập tin torrent cho một torrent mới, nơi mà tôi muốn tạo ra một phù hợp với một uri nam châm.Libtorrent - Với một liên kết nam châm, làm thế nào để bạn tạo ra một tập tin torrent?

+0

Cấp, tài liệu là khủng khiếp (mặc dù kỳ lạ có thể sử dụng). Tuy nhiên, có gì sai với phần mở rộng siêu dữ liệu http://www.rasterbar.com/products/libtorrent/manual.html#add-extension? –

Trả lời

11

Giải pháp tìm thấy ở đây:

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

Xem tạo torrent:

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

Sửa đổi dòng đầu tiên:

file_storage fs; 

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

create_torrent t(fs); 

Để này:

torrent_info ti = handle.get_torrent_info() 

create_torrent t(ti) 

"xử lý" là từ đây:

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

Ngoài ra trước khi tạo torrent bạn phải chắc chắn rằng siêu dữ liệu đã được tải, thực hiện điều này bằng cách gọi handle.has_metadata().

CẬP NHẬT

Có vẻ như libtorrent python api đang thiếu một số c quan trọng ++ api đó là cần thiết để tạo torrent từ nam châm, ví dụ trên sẽ không làm việc trong python nguyên nhân lớp create_torrent python không chấp nhận torrent_info như tham số (C++ có sẵn).

Vì vậy, tôi đã cố gắng nói cách khác, nhưng cũng gặp phải một bức tường gạch mà làm cho nó không thể, đây là mã:

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

Có một lỗi ném vào dòng này:

torfile.set_hash(i, hash) 

Nó hy vọng băm là const char* nhưng torrent_info.hash_for_piece(int) trả về lớp big_number mà không có api để chuyển đổi nó trở lại const char *.

Khi tôi tìm thấy một số thời gian, tôi sẽ báo cáo lỗi api bị thiếu này cho các nhà phát triển libtorrent, vì hiện tại không thể tạo tệp .torrent từ một uri nam châm khi sử dụng các ràng buộc python.

torrent_info.orig_files() cũng bị thiếu trong các ràng buộc python, tôi không chắc liệu torrent_info.files() có đủ hay không.

UPDATE 2

tôi đã tạo ra một vấn đề về vấn đề này, nhìn thấy nó ở đây: http://code.google.com/p/libtorrent/issues/detail?id=294

sao nó để họ sửa chữa nó nhanh.

CẬP NHẬT 3

Nó được cố định bây giờ, có một phiên bản 0.16.0. Binaries cho các cửa sổ cũng có sẵn.

+1

Cảm ơn bạn rất nhiều vì tất cả công việc khó khăn! – ciferkey

0

Nếu lưu resume data không hoạt động cho bạn, bạn có thể tạo tệp torrent mới bằng thông tin từ kết nối hiện có.

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

Tôi nghi ngờ rằng nó sẽ làm cho bản lý lịch nhanh hơn chức năng được tạo riêng cho mục đích này.

3

Chỉ muốn cung cấp một bản cập nhật nhanh chóng bằng cách sử dụng gói libtorrent Python hiện đại: libtorrent bây giờ có parse_magnet_uri phương pháp mà bạn có thể sử dụng để tạo ra một tay cầm torrent:

import libtorrent, os, time 

def magnet_to_torrent(magnet_uri, dst): 
    params = libtorrent.parse_magnet_uri(magnet_uri) 
    session = libtorrent.session() 
    handle = session.add_torrent(params) 
    print "Downloading metadata..." 
    while not handle.has_metadata(): 
     time.sleep(0.1) 
    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 
Các vấn đề liên quan