Library

The dottorrent library can be used in a Python script or program to create .torrent files.

class dottorrent.Torrent(path, trackers=None, web_seeds=None, piece_size=None, private=False, source=None, creation_date=None, comment=None, created_by=None, include_md5=False, exclude=None)
Parameters:
  • path – path to a file or directory from which to create the torrent
  • trackers – list/iterable of tracker URLs
  • web_seeds – list/iterable of HTTP/FTP seed URLs
  • piece_size – Piece size in bytes. Must be >= 16 KB and a power of 2. If None, get_info() will be used to automatically select a piece size.
  • private – The private flag. If True, DHT/PEX will be disabled.
  • source – An optional source string for the torrent.
  • exclude – A list of filename patterns that should be excluded from the torrent.
  • creation_date – An optional datetime object representing the torrent creation date.
  • comment – An optional comment string for the torrent.
  • created_by – name/version of the program used to create the .torrent. If None, defaults to the value of DEFAULT_CREATOR.
  • include_md5 – If True, also computes and stores MD5 hashes for each file.
data

Returns the data dictionary for the torrent.

Note

generate() must be called first.

dump()
Returns:The bencoded torrent data as a byte string.

Note

generate() must be called first.

generate(callback=None)

Computes and stores piece data. Returns True on success, False otherwise.

Parameters:callback – progress/cancellation callable with method signature (filename, pieces_completed, pieces_total). Useful for reporting progress if dottorrent is used in a GUI/threaded context, and if torrent generation needs to be cancelled. The callable’s return value should evaluate to True to trigger cancellation.
get_info()

Scans the input path and automatically determines the optimal piece size based on ~1500 pieces (up to MAX_PIECE_SIZE) along with other basic info, including total size (in bytes), the total number of files, piece size (in bytes), and resulting number of pieces. If piece_size has already been set, the custom value will be used instead.

Returns:(total_size, total_files, piece_size, num_pieces)
info_hash
Returns:The SHA-1 info hash of the torrent. Useful for generating magnet links.

Note

generate() must be called first.

info_hash_base32

Returns the base32 info hash of the torrent. Useful for generating magnet links.

Note

generate() must be called first.

piece_size
save(fp)

Saves the torrent to fp, a file(-like) object opened in binary writing (wb) mode.

Note

generate() must be called first.

trackers
web_seeds

Example Usage

from dottorrent import Torrent

t = Torrent('/my/data/', trackers=['http://tracker.openbittorrent.com:80/announce'])
t.generate()
with open('mydata.torrent', 'wb') as f:
        t.save(f)