-
Notifications
You must be signed in to change notification settings - Fork 0
Working With `Torrent` Object
BitTorrent(bt) can track an individual torrent by the torrent file, the magnet link, or the info hash. The magnet link and info hash only provide the identifier(or some additional information as well) while the torrent file contains all the necessary information for validating the downloading file. Which means if a magnet link or info hash was provided, torrent file will be downloaded from other peers. It is possible that no connected peer has the whole copy of the torrent file which the client will not be able to start downloading.
Currently the lib support adding the torrent through torrent file and info hash. The magnet link support is still under development.
from torrentlib import Torrent
torrent = Torrent.from_file("example.torrent")Create a minimal Torrent when you only have an info hash, for example from a magnet link:
from torrentlib import Torrent, TorrentStatus
torrent = Torrent(
info_hash="1234567890abcdef1234567890abcdef12345678"
)from torrentlib import Torrent, TorrentStatus
torrent = Torrent(
info_hash="1234567890abcdef1234567890abcdef12345678",
total_size=1145141919810,
left=1145141919810,
downloaded=0,
uploaded=0,
event=TorrentStatus.STOPPED,
name="example_file.iso",
piece_length=None,
num_pieces=None,
)
# creating torrent object
print(torrent)
print(torrent.name)
print(torrent.info_hash)
print(torrent.total_size)
print(torrent.piece_length)
print(torrent.num_pieces)
files = torrent.get_files_info() # returns `None` until metadata is available.
if files:
for file_hash, file_info in files.items():
print(file_hash, file_info["name"], file_info["length"])-
Torrentowns runtime swarm state such asdownloaded,uploaded,left,event, and peer caches. -
Torrent.update_from_metadata()verifies that received metadata matches the original info hash.