Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- # # Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> # # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. #
""" Raised when an invalid path is supplied """
""" Raised when an invalid piece size is set. Piece sizes must be multiples of 16KiB. """
""" This class is used to create .torrent files.
** Usage **
>>> t = TorrentMetadata() >>> t.data_path = "/tmp/torrent" >>> t.comment = "My Test Torrent" >>> t.trackers = [["http://tracker.openbittorent.com"]] >>> t.save("/tmp/test.torrent")
"""
""" Creates and saves the torrent file to `path`.
:param torrent_path: where to save the torrent file :type torrent_path: string
:param progress: a function to be called when a piece is hashed :type progress: function(num_completed, num_pieces)
:raises InvalidPath: if the data_path has not been set
""" raise InvalidPath("Need to set a data_path!")
"info": {} }
torrent["comment"] = self.comment.encode("UTF-8")
torrent["info"]["private"] = True
torrent["announce"] = self.trackers[0][0] torrent["announce-list"] = self.trackers else:
httpseeds = [] webseeds = [] for w in self.webseeds: if w.endswith(".php"): httpseeds.append(w) else: webseeds.append(w)
if httpseeds: torrent["httpseeds"] = httpseeds if webseeds: torrent["url-list"] = webseeds
piece_size = self.piece_size * 1024 else: # We need to calculate a piece size piece_size *= 2
# Calculate the number of pieces we will require for the data
# Create the info # Collect a list of file paths and add padding files if necessary p += [filename] else: # Add a padding file if necessary
# Run the progress function with 0 completed pieces progress(0, num_pieces)
# Create the piece hashes else: # Run the progress function if necessary progress(len(pieces), num_pieces) else:
progress(len(pieces), num_pieces)
progress(len(pieces), num_pieces)
# Write out the torrent file
""" The path to the files that the torrent will contain. It can be either a file or a folder. This property needs to be set before the torrent file can be created and saved. """
""" :param path: the path to the data :type path: string
:raises InvalidPath: if the path is not found
""" else: raise InvalidPath("No such file or directory: %s" % path)
""" The size of pieces in bytes. The size must be a multiple of 16KiB. If you don't set a piece size, one will be automatically selected to produce a torrent with less than 1024 pieces or the smallest possible with a 8192KiB piece size.
"""
""" :param size: the desired piece size in KiBs :type size: int
:raises InvalidPieceSize: if the piece size is not a multiple of 16 KiB
""" if size % 16 and size: raise InvalidPieceSize("Piece size must be a multiple of 16 KiB") self.__piece_size = size
""" Comment is some extra info to be stored in the torrent. This is typically an informational string. """
""" :param comment: an informational string :type comment: string """ self.__comment = comment
""" Private torrents only announce to the tracker and will not use DHT or Peer Exchange.
See: http://bittorrent.org/beps/bep_0027.html
"""
""" :param private: True if the torrent is to be private :type private: bool """ self.__private = private
""" The announce trackers is a list of lists.
See: http://bittorrent.org/beps/bep_0012.html
"""
""" :param trackers: a list of lists of trackers, each list is a tier :type trackers: list of list of strings """ self.__trackers = trackers
""" The web seeds can either be: Hoffman-style: http://bittorrent.org/beps/bep_0017.html or, GetRight-style: http://bittorrent.org/beps/bep_0019.html
If the url ends in '.php' then it will be considered Hoffman-style, if not it will be considered GetRight-style. """
""" :param webseeds: the webseeds which can be either Hoffman or GetRight style :type webseeds: list of urls """ self.__webseeds = webseeds
""" If this is True, padding files will be added to align files on piece boundaries. """
""" :param pad: set True to align files on piece boundaries :type pad: bool """
|