Trung Thanh Tran
2016-07-05 15:55:11 UTC
Hi,
I have met problem with pad file in libtorrent-python. I did my test in 1.0.6, 1.0.7, 1.1.0.
Use case:
Share a directory containing 5 files
- a-0000.rs size ~501MB
- a-0001.rs size ~501MB
- a-0002.rs size ~501MB
- a-0003.rs size ~501MB
- a-info.txt size ~48 bytes
Now, I need to create torrent which allows me to download a file with specific index e.g. a-0001.rs and a-info.text
I have implement the following snippet to create torrent
# Start generate torrent
def generate_torrent(shared_directory, piece_size=0,
pad_size_limit=16 * 1024, flags=1):
…
file_storage = lt.file_storage()
if piece_size % 16384 is not 0:
self.logger.warn('Torrent piece size must be 0 or a multiple of 16 kiB.')
piece_size = 16384 # 16 kib
directory = os.path.abspath(sharing_resource)
parent_directory = os.path.split(directory)[0]
for root, dirs, files in os.walk(directory):
if os.path.split(root)[1][0] == '.':
continue
for f in files:
if f[0] in ['.', 'Thumbs.db']:
continue
filename = os.path.join(root[len(parent_directory) + 1:], f)
size = os.path.getsize(os.path.join(parent_directory, filename))
file_storage.add_file(filename, size)
torrent = lt.create_torrent(file_storage, piece_size, pad_size_limit, flags)
lt.set_piece_hashes(torrent, parent_directory)
torrent = torrent.generate()
file_to_store.write(lt.bencode(torrent))
file_to_store.close()
# End generate torrent
And following code to download to download specific index
# Start download torrent
num_file = info.num_files()
file_priorities = list()
file_list = dict()
info_index = -1
for index in range(num_file):
current_path = info.file_at(index).path
self.logger.debug("Include " + current_path + " at index = " + str(index) + " to get")
if "-info.txt" in current_path:
file_priorities.append(7)
info_index = index
elif ".rs" in current_path:
file_list[index] = current_path
file_priorities.append(7)
else:
file_priorities.append(0)
add_torrent_params = dict()
add_torrent_params['ti'] = info
add_torrent_params['save_path'] = destination_directory
add_torrent_params['storage_mode'] = lt.storage_mode_t.storage_mode_sparse
try:
resume_path = ''.join([destination_directory, info.name(), '.fastresume'])
add_torrent_params['resume_data'] = open(os.path.join(resume_path), 'rb').read()
except:
pass
torrent_handler = libtorrent_session.add_torrent(add_torrent_params)
torrent_handler.prioritize_files(file_priorities)
# End download torrent
When I try to use 2 snippets above, the result is unstable.
- Try to download index =0, I see that a-0000.rs, a-info.txt and a-0001.rs are in target.
Afterthat, when varying the size of rs file, sometimes all files are in the target although only index 1 is marked to be downloaded.
Please correct me if I implement incorrectly pad files.
Thanks and best regards,
Thanh,
I have met problem with pad file in libtorrent-python. I did my test in 1.0.6, 1.0.7, 1.1.0.
Use case:
Share a directory containing 5 files
- a-0000.rs size ~501MB
- a-0001.rs size ~501MB
- a-0002.rs size ~501MB
- a-0003.rs size ~501MB
- a-info.txt size ~48 bytes
Now, I need to create torrent which allows me to download a file with specific index e.g. a-0001.rs and a-info.text
I have implement the following snippet to create torrent
# Start generate torrent
def generate_torrent(shared_directory, piece_size=0,
pad_size_limit=16 * 1024, flags=1):
…
file_storage = lt.file_storage()
if piece_size % 16384 is not 0:
self.logger.warn('Torrent piece size must be 0 or a multiple of 16 kiB.')
piece_size = 16384 # 16 kib
directory = os.path.abspath(sharing_resource)
parent_directory = os.path.split(directory)[0]
for root, dirs, files in os.walk(directory):
if os.path.split(root)[1][0] == '.':
continue
for f in files:
if f[0] in ['.', 'Thumbs.db']:
continue
filename = os.path.join(root[len(parent_directory) + 1:], f)
size = os.path.getsize(os.path.join(parent_directory, filename))
file_storage.add_file(filename, size)
torrent = lt.create_torrent(file_storage, piece_size, pad_size_limit, flags)
lt.set_piece_hashes(torrent, parent_directory)
torrent = torrent.generate()
file_to_store.write(lt.bencode(torrent))
file_to_store.close()
# End generate torrent
And following code to download to download specific index
# Start download torrent
num_file = info.num_files()
file_priorities = list()
file_list = dict()
info_index = -1
for index in range(num_file):
current_path = info.file_at(index).path
self.logger.debug("Include " + current_path + " at index = " + str(index) + " to get")
if "-info.txt" in current_path:
file_priorities.append(7)
info_index = index
elif ".rs" in current_path:
file_list[index] = current_path
file_priorities.append(7)
else:
file_priorities.append(0)
add_torrent_params = dict()
add_torrent_params['ti'] = info
add_torrent_params['save_path'] = destination_directory
add_torrent_params['storage_mode'] = lt.storage_mode_t.storage_mode_sparse
try:
resume_path = ''.join([destination_directory, info.name(), '.fastresume'])
add_torrent_params['resume_data'] = open(os.path.join(resume_path), 'rb').read()
except:
pass
torrent_handler = libtorrent_session.add_torrent(add_torrent_params)
torrent_handler.prioritize_files(file_priorities)
# End download torrent
When I try to use 2 snippets above, the result is unstable.
- Try to download index =0, I see that a-0000.rs, a-info.txt and a-0001.rs are in target.
Afterthat, when varying the size of rs file, sometimes all files are in the target although only index 1 is marked to be downloaded.
Please correct me if I implement incorrectly pad files.
Thanks and best regards,
Thanh,