def test_format(self): file = 'tests/test_torrent.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') size = torrent_formatter.humanize_size( torrent_info.download_info.total_size) self.assertEqual(size, '18.3 Mb') torrent_formatter.join(["123", "456"]) content = torrent_formatter.content_format(torrent_info) self.assertEqual(content, None) status = torrent_formatter.status_format(torrent_info) self.assertEqual(status, status) title = torrent_formatter.title_format(torrent_info) self.assertEqual(title, title) big_size = torrent_formatter.humanize_size(1000000000) self.assertEqual(big_size, '953.7 Mb') speed = torrent_formatter.humanize_speed(1000) self.assertEqual(speed, '1000 bytes/s') file = 'tests/udp.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') content = torrent_formatter.content_format(torrent_info) self.assertEqual(content, content) status = torrent_formatter.status_format(torrent_info) self.assertEqual(status, status) title = torrent_formatter.title_format(torrent_info) self.assertEqual(title, title)
def test_client(self): file = 'tests/udp_torrent.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') loop = asyncio.get_event_loop() control_manager = ControlManager() self.assertEqual(control_manager.torrents, {}) control_client = ControlClient() server = ControlServer(control_client) self.assertEqual(server.HOST, '127.0.0.1') loop.run_until_complete(server.start()) client = ControlClient() server = control_manager.server _reader, _writer = loop.run_until_complete( asyncio.open_connection(host=ControlServer.HOST, port=6995)) loop.run_until_complete(client.__aenter__()) loop.run_until_complete(client.connect()) loop.run_until_complete( client.execute(partial(control_manager.add, torrent_info))) loop.run_until_complete(client.__aexit__("TypeError", "123", "123")) client.close() loop.run_until_complete(server.accept(_reader, _writer))
async def add(args): torrents = [ TorrentInfo.get_info(filename, download_dir=args.directory) for filename in args.filenames ] if args.include: paths = args.include mode = 'whitelist' elif args.exclude: paths = args.exclude mode = 'blacklist' else: paths = None mode = None if mode is not None: if len(torrents) > 1: raise ValueError( "Can't handle --include and --exclude when several files are added" ) torrent_info = torrents[0] paths = [PATH_SPLIT_REGEX.split(path) for path in paths] torrent_info.download_info.select_files(paths, mode) async with ControlClient() as client: for info in torrents: await client.execute(partial(ControlManager.add, torrent_info=info))
async def control_action_handler(args): action = getattr(ControlManager, args.action) torrents = [ TorrentInfo.get_info(filename, download_dir=None) for filename in args.filenames ] for info in torrents: await delegate_to_control( partial(action, info_hash=info.download_info.info_hash))
def test_peer_tcp(): file = 'tests/short.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') loop = asyncio.get_event_loop() control_manager = ControlManager() control_manager.add(torrent_info) loop.run_until_complete(control_manager.start()) loop.run_until_complete(control_manager.stop()) server = control_manager.server torrent = Torrent(torrent_info, server.peer_id, server.port)
def test_class(self): file = 'tests/test_torrent.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') print(torrent_info.download_info) announce = list(['http://t.bitlove.org/announce']) announce_list = list() announce_list.append(announce) dictionary = bencoder.decode(file) download_info = DownloadInfo.get_download_info(dictionary[b'info']) self.assertEqual(torrent_info.announce_list, announce_list) self.assertEqual(torrent_info.download_info.info_hash, download_info.info_hash)
def test_file_structure(self): file = 'tests/test_torrent.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') pieces = torrent_info.download_info.pieces piece = pieces[0] piece.reset_content() piece.reset_run_state() piece.are_all_blocks_downloaded() piece.mark_as_downloaded() file_structure = FileStructure('download', torrent_info.download_info) self.assertEqual(file_structure.download_info.files[0].length, 19211729) self.assertEqual(file_structure.download_info.suggested_name, "bl001-introduction.webm") loop = asyncio.get_event_loop() file_structure.iterate_files(10, 10) loop.run_until_complete(file_structure.read(10, 10)) loop.run_until_complete(file_structure.write(10, b'hello')) file_structure.close()
def test_download(self): file = 'tests/udp_torrent.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') info_hash_to_test = torrent_info.download_info.info_hash loop = asyncio.get_event_loop() control_manager = ControlManager() control_manager.add(torrent_info) loop.run_until_complete( control_manager.pause(torrent_info.download_info.info_hash)) loop.run_until_complete( control_manager.priority(torrent_info.download_info.info_hash)) control_manager.resume(torrent_info.download_info.info_hash) self.assertEqual(info_hash_to_test, torrent_info.download_info.info_hash) with open("test.bin", 'wb') as file: control_manager.dump(file) loop.run_until_complete( control_manager.remove(torrent_info.download_info.info_hash)) with open("test.bin", 'rb') as file: control_manager.load(file) loop.run_until_complete(control_manager.start()) loop.run_until_complete(control_manager.stop())
def test_control_client(self): file = 'tests/test_torrent.torrent' torrent_info = TorrentInfo.get_info(file, download_dir='downloads') pass
def show_torrent_handler(args): torrent_info = TorrentInfo.get_info(args.filename, download_dir=None) content = torrent_formatter.join( torrent_formatter.title_format(torrent_info) + torrent_formatter.content_format(torrent_info)) print(content, end='')