From 4ca393f267f2a2583f08c7a699e8b654a3bdc0e6 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 5 Sep 2022 10:52:42 -0300 Subject: [PATCH] disable upnp, fix save path, fix prio, update deprecated calls --- lbry/torrent/session.py | 37 ++++++++++++++++++++------------- lbry/torrent/torrent_manager.py | 2 +- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lbry/torrent/session.py b/lbry/torrent/session.py index a9bc0e0a50..f7b959a340 100644 --- a/lbry/torrent/session.py +++ b/lbry/torrent/session.py @@ -70,10 +70,14 @@ def __init__(self, loop, executor, handle): @property def largest_file(self) -> Optional[str]: - if not self.torrent_file: + if self.torrent_file is None: return None index = self.largest_file_index - return os.path.join(self._base_path, self.torrent_file.at(index).path) + return os.path.join(self._base_path, self.torrent_file.file_path(index)) + + @property + def save_path(self) -> Optional[str]: + return self._base_path @property def largest_file_index(self): @@ -100,15 +104,18 @@ def _show_status(self): if not self.metadata_completed.is_set(): self.metadata_completed.set() log.info("Metadata completed for btih:%s - %s", status.info_hash, self.name) - self.torrent_file = self._handle.get_torrent_info().files() + self.torrent_file = self._handle.torrent_file().files() self._base_path = status.save_path - first_piece = self.torrent_file.at(self.largest_file_index).offset + first_piece = self.torrent_file.piece_index_at_file(self.largest_file_index) if not self.started.is_set(): if self._handle.have_piece(first_piece): self.started.set() else: # prioritize it self._handle.set_piece_deadline(first_piece, 100) + prios = self._handle.piece_priorities() + prios[first_piece] = 7 + self._handle.prioritize_pieces(prios) if not status.is_seeding: log.debug('%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d seeds: %d) %s - %s', status.progress * 100, status.download_rate / 1000, status.upload_rate / 1000, @@ -157,10 +164,8 @@ async def add_fake_torrent(self): async def bind(self, interface: str = '0.0.0.0', port: int = 10889): settings = { 'listen_interfaces': f"{interface}:{port}", - 'enable_outgoing_utp': True, - 'enable_incoming_utp': True, - 'enable_outgoing_tcp': False, - 'enable_incoming_tcp': False + 'enable_upnp': False, + 'enable_natpmp': False } self._session = await self._loop.run_in_executor( self._executor, libtorrent.session, settings # pylint: disable=c-extension-no-member @@ -213,6 +218,9 @@ def _add_torrent(self, btih: str, download_directory: Optional[str]): def full_path(self, btih): return self._handles[btih].largest_file + def save_path(self, btih): + return self._handles[btih].save_path + async def add_torrent(self, btih, download_path): await self._loop.run_in_executor( self._executor, self._add_torrent, btih, download_path @@ -275,17 +283,16 @@ async def main(): executor = None session = TorrentSession(asyncio.get_event_loop(), executor) - session2 = TorrentSession(asyncio.get_event_loop(), executor) - await session.bind('localhost', port=4040) - await session2.bind('localhost', port=4041) - btih = await session.add_fake_torrent() - session2._session.add_dht_node(('localhost', 4040)) - await session2.add_torrent(btih, "/tmp/down") + await session.bind() + await session.add_torrent(btih, os.path.expanduser("~/Downloads")) while True: - await asyncio.sleep(100) + session.full_path(btih) + await asyncio.sleep(1) await session.pause() executor.shutdown() if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)-4s %(name)s:%(lineno)d: %(message)s") + log = logging.getLogger(__name__) asyncio.run(main()) diff --git a/lbry/torrent/torrent_manager.py b/lbry/torrent/torrent_manager.py index 208392b76e..a884caf9ac 100644 --- a/lbry/torrent/torrent_manager.py +++ b/lbry/torrent/torrent_manager.py @@ -48,7 +48,7 @@ def __init__(self, loop: asyncio.AbstractEventLoop, config: 'Config', storage: ' @property def full_path(self) -> Optional[str]: full_path = self.torrent_session.full_path(self.identifier) - self.download_directory = os.path.dirname(full_path) + self.download_directory = self.torrent_session.save_path(self.identifier) return full_path @property