def dir_to_torrent(dir_path, announce, announce_list = None,\ piece_length = _PIECE_LENGTH, comment = None, is_private = False): if dir_path.endswith(os.sep): dir_path = dir_path[0:-1] torrent = {'creation date': round(time.time()), 'announce': announce} if announce_list: torrent['announce-list'] = announce_list if comment: torrent['comment'] = comment dir_name = path.basename(dir_path) def create_paths(dirname=[dir_path]): paths = [] file_list = os.listdir(path.join(*dirname)) for name in file_list: full_path = copy.copy(dirname) full_path.append(name) ospath = path.join(*full_path) if path.isfile(ospath): paths.append(full_path[1:]) else: files = create_paths(full_path) paths = paths + files return paths paths = create_paths() info = { 'piece length': piece_length, 'name': dir_name, 'pieces': hash_file_pieces(\ [path.join(dir_path, *p) for p in paths], piece_length) } if is_private: torrent['private'] = 1 files = [] for p in paths: files.append({ 'length': path.getsize(path.join(dir_path, *p)), 'path': p }) info['files'] = files torrent['info'] = info return bencoder.encode(torrent)
def dir_to_torrent(dir_path, announce, announce_list=None, piece_length=_PIECE_LENGTH, comment=None, is_private=False): if dir_path.endswith(os.sep): dir_path = dir_path[0:-1] torrent = {"creation date": round(time.time()), "announce": announce} if announce_list: torrent["announce-list"] = announce_list if comment: torrent["comment"] = comment dir_name = path.basename(dir_path) def create_paths(dirname=[dir_path]): paths = [] file_list = os.listdir(path.join(*dirname)) for name in file_list: full_path = copy.copy(dirname) full_path.append(name) ospath = path.join(*full_path) if path.isfile(ospath): paths.append(full_path[1:]) else: files = create_paths(full_path) paths = paths + files return paths paths = create_paths() info = { "piece length": piece_length, "name": dir_name, "pieces": hash_file_pieces([path.join(dir_path, *p) for p in paths], piece_length), } if is_private: torrent["private"] = 1 files = [] for p in paths: files.append({"length": path.getsize(path.join(dir_path, *p)), "path": p}) info["files"] = files torrent["info"] = info return bencoder.encode(torrent)
def file_to_torrent( file_path, announce, announce_list=None, piece_length=_PIECE_LENGTH, comment=None, is_private=False ): torrent = {"creation date": round(time.time()), "announce": announce} if announce_list: torrent["announce-list"] = announce_list if comment: torrent["comment"] = comment info = {"piece length": piece_length, "name": path.basename(file_path), "length": path.getsize(file_path)} if is_private: torrent["private"] = 1 pieces = hash_file_pieces([file_path], piece_length) info["pieces"] = pieces torrent["info"] = info return bencoder.encode(torrent)
def file_to_torrent(file_path, announce, announce_list = None,\ piece_length = _PIECE_LENGTH, comment = None, is_private = False): torrent = {'creation date': round(time.time()), 'announce': announce} if announce_list: torrent['announce-list'] = announce_list if comment: torrent['comment'] = comment info = { 'piece length': piece_length, 'name': path.basename(file_path), 'length': path.getsize(file_path) } if is_private: torrent['private'] = 1 pieces = hash_file_pieces([file_path], piece_length) info['pieces'] = pieces torrent['info'] = info return bencoder.encode(torrent)