#!/usr/bin/env python3 import libtorrent as lt import time def create_torrent(directory, torrent_file_name): # Create a new file storage object fs = lt.file_storage() # Add files from the directory to the file storage lt.add_files(fs, directory) # Create a new create_torrent object t = lt.create_torrent(fs, flags=lt.create_torrent_flags_t.v2_only) # Add tracker # t.add_tracker("http://your-tracker.com/announce") # Set comment or other properties (optional) t.set_comment("Cool Test") t.set_creator("My Torrent Creator") # Generate the torrent lt.set_piece_hashes(t, ".") # Second argument is the root directory for the files torrent = t.generate() # Write the torrent file with open(torrent_file_name, "wb") as f: f.write(lt.bencode(torrent)) def read_torrent_v2(file_path): # Create a torrent_info object from the file path info = lt.torrent_info(file_path) # Accessing various properties of the torrent print("Torrent Name:", info.name()) print("Number of Files:", info.num_files()) print("Total Size:", info.total_size()) # Loop through each file in the torrent for i in range(info.num_files()): file = info.files().file_path(i) size = info.files().file_size(i) print(f"File {i}: {file}, Size: {size}") # Example usage create_torrent("readme.md","my.torrent") read_torrent_v2("existing.torrent") read_torrent_v2("my.torrent")