def test_create_torrent(self): torrent_content = TORRENT_PATH self.obj = Torrent(source_type="TORRENT", source=torrent_content, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info)
def _create_torrent_handler(self, source_type, source, source_path, label, session): id = self._get_torrent_uniqueness( source_path if source_type == "TORRENT" else source) t_obj = Torrent(source_type, source, label) # TODO: make it asynchronous t_obj.set_source(source, session) self.torrents[id]=t_obj return t_obj
def _create_torrent_handler(self, source_type, source, source_path, label, session): id = self._get_torrent_uniqueness(source_path if source_type == "TORRENT" else source) t_obj = Torrent(source_type, source, label) # TODO: make it asynchronous t_obj.set_source(source, session) self.torrents[id] = t_obj return t_obj
def test_create_magnet_torrent(self): self.obj = Torrent(source_type="MAGNET", source=MAGNET, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) # we need to wait to download metadata self.assertRaises(TorrentHasNotMetadataYet, self.obj.get_torrent_info) count = 0 while not self.obj.has_torrent_info(): time.sleep(0.2) count += 1 if count > 300: raise Exception("counter exceeded") self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info)
def test_create_torrent(self): torrent_content = TORRENT_PATH self.obj = Torrent( source_type="TORRENT", source=torrent_content, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info)
def test_create_magnet_torrent(self): self.obj = Torrent( source_type="MAGNET", source=MAGNET, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) # we need to wait to download metadata self.assertRaises(TorrentHasNotMetadataYet, self.obj.get_torrent_info) count = 0 while not self.obj.has_torrent_info(): time.sleep(0.2) count += 1 if count > 300: raise Exception("counter exceeded") self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info)
def select_movie(self, torrent: Torrent) -> Movie: movies = torrent.get_movies() if len(movies) == 0: return # TODO: show dialog with movie selecting instead of doing it automatically return max(movies, key=lambda x: x.size)
# -*- coding:utf-8 -*- from torrent.torrent import Torrent t = Torrent("demo", ["http://121.14.98.151:9090/announce"]) t.create()
class TorrentObjectTestCase(AbstractTorrentTestCase): @classmethod def setUpClass(cls): cls.run_tracker() cls.run_seeder() @classmethod def tearDownClass(cls): cls.stop_tracker() shutil.rmtree(settings.DOWNLOAD_DIR, True) def setUp(self): shutil.rmtree(settings.DOWNLOAD_DIR, True) self.configure_session() try: os.makedirs(settings.DOWNLOAD_DIR) except OSError: pass def tearDown(self): self.stop_tracker() def test_create_torrent(self): torrent_content = TORRENT_PATH self.obj = Torrent( source_type="TORRENT", source=torrent_content, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info) def test_create_magnet_torrent(self): self.obj = Torrent( source_type="MAGNET", source=MAGNET, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) # we need to wait to download metadata self.assertRaises(TorrentHasNotMetadataYet, self.obj.get_torrent_info) count = 0 while not self.obj.has_torrent_info(): time.sleep(0.2) count += 1 if count > 300: raise Exception("counter exceeded") self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info) def test_get_filelist(self): self.test_create_torrent() files = self.obj.get_filelist() self.assertEqual(len(files), 3) self.assertEqual(files[0], "movie/movie.mp4") self.assertEqual(files[1], "movie/music.mp3") self.assertEqual(files[2], "movie/subs.txt") def test_get_movie_filelist(self): self.test_create_torrent() files = self.obj.get_movies() self.assertEqual(len(files), 1) self.assertEqual(list(files)[0].path, "movie/movie.mp4") def test_download_file(self): # it works! but i have no idea why it not work with localhost tracker return self.test_create_torrent() print("created. Go further") time.sleep(0.5) filename = "movie/movie.mp4" # print(self.obj.get_movies()[0].__dict__) self.obj.download_file(filename) while True: time.sleep(0.5) # print(self.obj.get_movies()[0].cur_first_piece) # print(self.obj.get_torrent_info().files()[0].size) # print(self.__class__.h.status().state) # print(self.__class__.h.status().progress) # print(self.obj.get_status()) # print(self.obj.torrent_handler.status().state) print(self.obj.torrent_handler.status().progress * 100)
class TorrentObjectTestCase(AbstractTorrentTestCase): @classmethod def setUpClass(cls): cls.run_tracker() cls.run_seeder() @classmethod def tearDownClass(cls): cls.stop_tracker() shutil.rmtree(settings.DOWNLOAD_DIR, True) def setUp(self): shutil.rmtree(settings.DOWNLOAD_DIR, True) self.configure_session() try: os.makedirs(settings.DOWNLOAD_DIR) except OSError: pass def tearDown(self): self.stop_tracker() def test_create_torrent(self): torrent_content = TORRENT_PATH self.obj = Torrent(source_type="TORRENT", source=torrent_content, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info) def test_create_magnet_torrent(self): self.obj = Torrent(source_type="MAGNET", source=MAGNET, name="test movie") self.obj.bind_session(self.session) self.assertIsInstance(self.obj, Torrent) # we need to wait to download metadata self.assertRaises(TorrentHasNotMetadataYet, self.obj.get_torrent_info) count = 0 while not self.obj.has_torrent_info(): time.sleep(0.2) count += 1 if count > 300: raise Exception("counter exceeded") self.assertIsInstance(self.obj.get_torrent_info(), lt.torrent_info) def test_get_filelist(self): self.test_create_torrent() files = self.obj.get_filelist() self.assertEqual(len(files), 3) self.assertEqual(files[0], "movie/movie.mp4") self.assertEqual(files[1], "movie/music.mp3") self.assertEqual(files[2], "movie/subs.txt") def test_get_movie_filelist(self): self.test_create_torrent() files = self.obj.get_movies() self.assertEqual(len(files), 1) self.assertEqual(list(files)[0].path, "movie/movie.mp4") def test_download_file(self): # it works! but i have no idea why it not work with localhost tracker return self.test_create_torrent() print("created. Go further") time.sleep(0.5) filename = "movie/movie.mp4" # print(self.obj.get_movies()[0].__dict__) self.obj.download_file(filename) while True: time.sleep(0.5) # print(self.obj.get_movies()[0].cur_first_piece) # print(self.obj.get_torrent_info().files()[0].size) # print(self.__class__.h.status().state) # print(self.__class__.h.status().progress) # print(self.obj.get_status()) # print(self.obj.torrent_handler.status().state) print(self.obj.torrent_handler.status().progress * 100)