-
Notifications
You must be signed in to change notification settings - Fork 0
Working With `Torrent` Object
A torrent can be identified using a .torrent file, a magnet link, or an info hash. A .torrent file contains the metainfo needed to validate downloaded content, while an info hash identifies that metainfo. When starting with only an info hash, the client must retrieve the metainfo from a peer that supports ut_metadata.
The library creates torrents from a .torrent file, a TorrentMetaInfo object,
an info hash, or a magnet link.
from torrentlib import Torrent
torrent = Torrent.from_magnet(
"magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567"
"&dn=Example%20File"
"&xl=1234"
"&tr=udp%3A%2F%2Ftracker.example%3A6881%2Fannounce"
"&x.pe=192.0.2.1%3A6881"
)The method also accepts runtime state:
from torrentlib import TorrentStatus
torrent = Torrent.from_magnet(
magnet_uri,
downloaded=1024,
uploaded=256,
event=TorrentStatus.STARTED,
)| Parameter | Meaning | Result |
|---|---|---|
xt |
Exact topic | A urn:btih topic becomes torrent.info_hash. |
dn |
Display name | Becomes the initial torrent.name hint. |
xl |
Exact length in bytes | Becomes torrent.total_size. |
tr |
Tracker URL | Repeated values populate announce and announce-list. |
x.pe |
Peer endpoint | Repeated values populate torrent.peers or torrent.peers6. |
The BTIH value in xt may use either a 40-character hexadecimal hash or a
32-character Base32 hash. Both forms are normalized to lowercase hexadecimal.
x.pe accepts IPv4 addresses and hostnames in host:port form. IPv6 addresses
should use bracketed form, such as [2001:db8::1]:6881. Ports must be between 1
and 65535. Invalid peer hints are ignored because they are optional discovery
hints; their original values remain available in the parsed parameters.
The created torrent contains only the hints supplied by the magnet URI. It does not contain full torrent metainfo until metadata is retrieved from a peer.
Use MagnetParser to inspect a magnet URI without constructing a Torrent:
from torrentlib.MagnetParser import MagnetParser
magnet = MagnetParser.parse(magnet_uri)
print(magnet.info_hash)
print(magnet.display_name)
print(magnet.exact_length)
print(magnet.trackers)
print(magnet.peer_endpoints)MagnetParser.parse() returns an immutable Magnet value object. Parameters
without specialized behavior, such as ws, xs, as, kt, and so, are
preserved in magnet.parameters:
web_seeds = magnet.parameters.get("ws", ())The parser raises ValueError when:
- the URI does not use the
magnetscheme; - no valid
urn:btihexact topic is present; -
xlis not a non-negative integer.
Passing a value other than a string raises TypeError.
from torrentlib import Torrent
torrent = Torrent.from_file("example.torrent")from torrentlib import Torrent, TorrentMetaInfo
metainfo = TorrentMetaInfo.from_file("example.torrent")
torrent = Torrent.from_metainfo(metainfo)Create a minimal Torrent when you only have an info hash:
from torrentlib import Torrent
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,
)print(torrent)
print(torrent.name)
print(torrent.info_hash)
print(torrent.total_size)
print(torrent.piece_length)
print(torrent.num_pieces)
print(torrent.has_metainfo)
print(torrent.metadata) # Bencoded info dictionary, or None.
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"])
same_file = torrent.get_file_by_hash(file_hash)
print(same_file)from torrentlib import TorrentStatus
torrent.update_downloaded(1024)
torrent.update_uploaded(512)
torrent.set_event(TorrentStatus.COMPLETED)-
Torrentowns runtime swarm state such asdownloaded,uploaded,left,event, and peer caches. -
TorrentStatusprovidesCOMPLETED,STARTED, andSTOPPED. -
Torrent.update_from_metadata()verifies that received metadata matches the original info hash.