Package Tribler :: Package Core :: Module TorrentDef :: Class TorrentDef

Class TorrentDef

Base.Serializable --+
                    |
    Base.Copyable --+
                    |
                   TorrentDef

Definition of a torrent, that is, all params required for a torrent file, plus optional params such as thumbnail, playtime, etc. Note: to add fields to the torrent definition which are not supported by its API, first create the torrent def, finalize it, then add the fields to the metainfo, and create a new torrent def from that upgraded metainfo using TorrentDef.load_from_dict() cf. libtorrent torrent_info
Instance Methods
 
__init__(self, input=None, metainfo=None, infohash=None)
Normal constructor for TorrentDef (The input, metainfo and infohash...
 
add_content(self, inpath, outpath=None, playtime=None)
Add a file or directory to this torrent definition.
 
remove_content(self, inpath)
Remove a file or directory from this torrent definition
 
create_live(self, name, bitrate, playtime='1:00:00', authconfig=None)
Create a live streaming multimedia torrent with a specific bitrate.
 
set_encoding(self, enc)
Set the character encoding for e.g.
 
get_encoding(self)
 
set_thumbnail(self, thumbfilename)
Reads image from file and turns it into a torrent thumbnail The file should contain an image in JPEG format, preferably 171x96.
 
get_thumbnail(self)
Returns (MIME type,thumbnail data) if present or (None,None)
 
set_tracker(self, url)
Sets the tracker (i.e.
 
get_tracker(self)
Returns the announce URL.
 
set_tracker_hierarchy(self, hier)
Set hierarchy of trackers (announce-list) following the spec...
 
get_tracker_hierarchy(self)
Returns the hierarchy of trackers.
 
set_dht_nodes(self, nodes)
Sets the DHT nodes required by the mainline DHT support,...
 
get_dht_nodes(self)
Returns the DHT nodes set.
 
set_comment(self, value)
Set comment field.
 
get_comment(self)
Returns the comment field of the def.
 
set_created_by(self, value)
Set 'created by' field.
 
get_created_by(self)
Returns the 'created by' field.
 
set_httpseeds(self, value)
Set list of HTTP seeds following the spec at...
 
get_httpseeds(self)
Returns the list of HTTP seeds.
 
set_piece_length(self, value)
Set the size of the pieces in which the content is traded.
 
get_piece_length(self)
Returns the piece size.
 
set_add_md5hash(self, value)
Whether to add an end-to-end MD5 checksum to the def.
 
get_add_md5hash(self)
Returns whether to add an MD5 checksum.
 
set_add_crc32(self, value)
Whether to add an end-to-end CRC32 checksum to the def.
 
get_add_crc32(self)
Returns whether to add an end-to-end CRC32 checksum to the def.
 
set_add_sha1hash(self, value)
Whether to add end-to-end SHA1 checksum to the def.
 
get_add_sha1hash(self)
Returns whether to add an end-to-end SHA1 checksum to the def.
 
set_create_merkle_torrent(self, value)
Create a Merkle torrent instead of a regular BT torrent.
 
get_create_merkle_torrent(self)
Returns whether to create a Merkle torrent.
 
set_signature_keypair_filename(self, value)
Set absolute filename of keypair to be used for signature.
 
get_signature_keypair_filename(self)
Returns the filename containing the signing keypair or None.
 
get_live(self)
Returns whether this definition is for a live torrent.
 
get_live_authmethod(self)
Returns the method for authenticating the source.
 
get_live_pubkey(self)
Returns the public key used for authenticating packets from the source.
 
finalize(self, userabortflag=None, userprogresscallback=None)
Create BT torrent file by reading the files added with add_content() and calculate the torrent file's infohash.
 
is_finalized(self)
Returns whether the TorrentDef is finalized or not.
 
get_infohash(self)
Returns the infohash of the torrent.
 
get_metainfo(self)
Returns the torrent definition as a dictionary that follows the BT spec for torrent files.
 
get_name(self)
Returns the info['name'] field as raw string of bytes.
 
set_name(self, name)
Set the name of this torrent
 
get_name_as_unicode(self)
Returns the info['name'] field as Unicode string.
 
verify_torrent_signature(self)
Verify the signature on the finalized torrent definition.
 
save(self, filename)
Finalizes the torrent def and writes a torrent file i.e., bencoded dict following BT spec) to the specified filename.
 
get_bitrate(self, file=None)
Returns the bitrate of the specified file.
 
get_files(self, exts=None)
The list of files in the finalized torrent def.
 
get_length(self, selectedfiles=None)
Returns the total size of the content in the torrent.
 
is_multifile_torrent(self)
Returns whether this TorrentDef is a multi-file torrent.
 
get_index_of_file_in_files(self, file)
 
copy(self)
Copies the instance.
Static Methods
 
load(filename)
Load a BT .torrent or Tribler .tribe file from disk and convert it into a finalized TorrentDef.
 
load_from_url(url)
Load a BT .torrent or Tribler .tribe file from the URL and convert it into a TorrentDef.
 
load_from_dict(metainfo)
Load a BT .torrent or Tribler .tribe file from the metainfo dictionary...
Method Details

__init__(self, input=None, metainfo=None, infohash=None)
(Constructor)

 
Normal constructor for TorrentDef (The input, metainfo and infohash parameters are used internally to make this a copy constructor)
Overrides: Base.Serializable.__init__

load(filename)
Static Method

 
Load a BT .torrent or Tribler .tribe file from disk and convert it into a finalized TorrentDef.
Parameters:
  • filename - An absolute Unicode filename
Returns:
TorrentDef

load_from_url(url)
Static Method

 
Load a BT .torrent or Tribler .tribe file from the URL and convert it into a TorrentDef.
Parameters:
  • url - URL
Returns:
TorrentDef.

load_from_dict(metainfo)
Static Method

 
Load a BT .torrent or Tribler .tribe file from the metainfo dictionary it into a TorrentDef
Parameters:
  • metainfo - A dictionary following the BT torrent file spec.
Returns:
TorrentDef.

add_content(self, inpath, outpath=None, playtime=None)

 
Add a file or directory to this torrent definition. When adding a directory, all files in that directory will be added to the torrent. One can add multiple files and directories to a torrent definition. In that case the "outpath" parameter must be used to indicate how the files/dirs should be named in the torrent. The outpaths used must start with a common prefix which will become the "name" field of the torrent. To seed the torrent via the core (as opposed to e.g. HTTP) you will need to start the download with the dest_dir set to the top-level directory containing the files and directories to seed. For example, a file "c:\Videos ile.avi" is seeded as follows:
    tdef = TorrentDef()
    tdef.add_content("c:\Videosile.avi",playtime="1:59:20")
    tdef.set_tracker(s.get_internal_tracker_url())
    tdef.finalize()
    dscfg = DownloadStartupConfig()
    dscfg.set_dest_dir("c:\Video")
    s.start_download(tdef,dscfg)
Parameters:
  • inpath - Absolute name of file or directory on local filesystem, as Unicode string.
  • outpath - (optional) Name of the content to use in the torrent def as Unicode string.
  • playtime - (optional) String representing the duration of the multimedia file when played, in [hh:]mm:ss format.

remove_content(self, inpath)

 
Remove a file or directory from this torrent definition
Parameters:
  • inpath - Absolute name of file or directory on local filesystem, as Unicode string.

create_live(self, name, bitrate, playtime='1:00:00', authconfig=None)

 
Create a live streaming multimedia torrent with a specific bitrate. The authconfig is a subclass LiveSourceAuthConfig with the key information required to allow authentication of packets from the source, or None. In the latter case there is no source authentication. The other current legal value is an instance of ECDSALiveSourceAuthConfig. When using this method, a sequence number, real-time timestamp and an ECDSA signature of 64 bytesis put in each piece. As a result, the content in each packet is get_piece_length()-81, so that this into account when selecting the bitrate. The info from the authconfig is stored in the 'info' part of the torrent file when finalized, so changing the authentication info changes the identity (infohash) of the torrent.
Parameters:
  • name - The name of the stream.
  • bitrate - The desired bitrate in bytes per second.
  • playtime - The virtual playtime of the stream as a string in [hh:]mm:ss format.
  • authconfig - Parameters for the authentication of the source

set_encoding(self, enc)

 
Set the character encoding for e.g. the 'name' field

set_thumbnail(self, thumbfilename)

 
Reads image from file and turns it into a torrent thumbnail The file should contain an image in JPEG format, preferably 171x96.
Parameters:
  • thumbfilename - Absolute name of image file, as Unicode string.

get_thumbnail(self)

 
Returns (MIME type,thumbnail data) if present or (None,None)
Returns:
A tuple.

set_tracker(self, url)

 
Sets the tracker (i.e. the torrent file's 'announce' field).
Parameters:
  • url - The announce URL.

get_tracker(self)

 
Returns the announce URL.
Returns:
URL

set_tracker_hierarchy(self, hier)

 
Set hierarchy of trackers (announce-list) following the spec at http://www.bittornado.com/docs/multitracker-spec.txt
Parameters:
  • hier - A hierarchy of trackers as a list of lists.

get_tracker_hierarchy(self)

 
Returns the hierarchy of trackers.
Returns:
A list of lists.

set_dht_nodes(self, nodes)

 
Sets the DHT nodes required by the mainline DHT support, See http://www.bittorrent.org/DHT_protocol.html
Parameters:
  • nodes - A list of [hostname,port] lists.

get_dht_nodes(self)

 
Returns the DHT nodes set.
Returns:
A list of [hostname,port] lists.

set_comment(self, value)

 
Set comment field.
Parameters:
  • value - A Unicode string.

get_comment(self)

 
Returns the comment field of the def.
Returns:
A Unicode string.

set_created_by(self, value)

 
Set 'created by' field.
Parameters:
  • value - A Unicode string.

get_created_by(self)

 
Returns the 'created by' field.
Returns:
Unicode string.

set_httpseeds(self, value)

 
Set list of HTTP seeds following the spec at http://www.bittornado.com/docs/webseed-spec.txt
Parameters:
  • value - A list of URLs.

get_httpseeds(self)

 
Returns the list of HTTP seeds.
Returns:
A list of URLs.

set_piece_length(self, value)

 
Set the size of the pieces in which the content is traded. The piece size must be a multiple of the chunk size, the unit in which it is transmitted, which is 16K by default (see DownloadConfig.set_download_slice_size()). The default is automatic (value 0).
Parameters:
  • value - A number of bytes as per the text.

get_piece_length(self)

 
Returns the piece size.
Returns:
A number of bytes.

set_add_md5hash(self, value)

 
Whether to add an end-to-end MD5 checksum to the def.
Parameters:
  • value - Boolean.

set_add_crc32(self, value)

 
Whether to add an end-to-end CRC32 checksum to the def.
Parameters:
  • value - Boolean.

get_add_crc32(self)

 
Returns whether to add an end-to-end CRC32 checksum to the def.
Returns:
Boolean.

set_add_sha1hash(self, value)

 
Whether to add end-to-end SHA1 checksum to the def.
Parameters:
  • value - Boolean.

get_add_sha1hash(self)

 
Returns whether to add an end-to-end SHA1 checksum to the def.
Returns:
Boolean.

set_create_merkle_torrent(self, value)

 
Create a Merkle torrent instead of a regular BT torrent. A Merkle torrent uses a hash tree for checking the integrity of the content received. As such it creates much smaller torrent files than the regular method. Tribler-specific feature.

get_create_merkle_torrent(self)

 
Returns whether to create a Merkle torrent.
Returns:
Boolean.

set_signature_keypair_filename(self, value)

 
Set absolute filename of keypair to be used for signature. When set, a signature will be added.
Parameters:
  • value - A filename containing an Elliptic Curve keypair.

get_signature_keypair_filename(self)

 
Returns the filename containing the signing keypair or None.
Returns:
Unicode String or None.

get_live(self)

 
Returns whether this definition is for a live torrent.
Returns:
Boolean.

get_live_authmethod(self)

 
Returns the method for authenticating the source.
LIVE_AUTHMETHOD_ECDSA
Returns:
String

get_live_pubkey(self)

 
Returns the public key used for authenticating packets from the source.
Returns:
A public key in DER.

finalize(self, userabortflag=None, userprogresscallback=None)

 
Create BT torrent file by reading the files added with add_content() and calculate the torrent file's infohash. Creating the torrent file can take a long time and will be carried out by the calling thread. The process can be made interruptable by passing a threading.Event() object via the userabortflag and setting it when the process should be aborted. The also optional userprogresscallback will be called by the calling thread periodically, with a progress percentage as argument. The userprogresscallback function will be called by the calling thread.
Parameters:
  • userabortflag - threading.Event() object
  • userprogresscallback - Function accepting a fraction as first argument.

is_finalized(self)

 
Returns whether the TorrentDef is finalized or not.
Returns:
Boolean.

get_infohash(self)

 
Returns the infohash of the torrent.
Returns:
A string of length 20.

get_metainfo(self)

 
Returns the torrent definition as a dictionary that follows the BT spec for torrent files.
Returns:
dict

get_name(self)

 
Returns the info['name'] field as raw string of bytes.
Returns:
String

set_name(self, name)

 
Set the name of this torrent
Parameters:
  • name - name of torrent as String

get_name_as_unicode(self)

 
Returns the info['name'] field as Unicode string.
Returns:
Unicode string.

verify_torrent_signature(self)

 
Verify the signature on the finalized torrent definition. Returns whether the signature was valid.
Returns:
Boolean.

save(self, filename)

 
Finalizes the torrent def and writes a torrent file i.e., bencoded dict following BT spec) to the specified filename. Note this make take a long time when the torrent def is not yet finalized.
Parameters:
  • filename - An absolute Unicode path name.

get_bitrate(self, file=None)

 
Returns the bitrate of the specified file. If no file is specified, we assume this is a single-file torrent.
Parameters:
  • file - (Optional) the file in the torrent to retrieve the bitrate of.
Returns:
The bitrate in bytes per second.

get_files(self, exts=None)

 
The list of files in the finalized torrent def.
Parameters:
  • exts - (Optional) list of filename extensions (without leading .) to search for.
Returns:
A list of filenames.

get_length(self, selectedfiles=None)

 
Returns the total size of the content in the torrent. If the optional selectedfiles argument is specified, the method returns the total size of only those files.
Returns:
A length (long)

is_multifile_torrent(self)

 
Returns whether this TorrentDef is a multi-file torrent.
Returns:
Boolean

copy(self)

 
Copies the instance.
Parameters:
  • self - an unbound instance of the class
Returns:
Returns a copy of "self"
Overrides: Base.Copyable.copy
(inherited documentation)