find and show largest file

This commit is contained in:
Victor Shyba 2020-02-26 12:40:11 -03:00
parent a7c2408c0a
commit 6ad0242617
2 changed files with 22 additions and 0 deletions

View file

@ -64,6 +64,19 @@ class TorrentHandle:
self.total_wanted_done = 0
self.name = ''
self.tasks = []
self.torrent_file: Optional[libtorrent.torrent_info] = None
self._base_path = None
@property
def largest_file(self) -> Optional[str]:
if not self.torrent_file:
return None
largest_size, path = 0, None
for file_num in range(self.torrent_file.num_files()):
if self.torrent_file.file_size(file_num) > largest_size:
largest_size = self.torrent_file.file_size(file_num)
path = self.torrent_file.at(file_num).path
return os.path.join(self._base_path, path)
def stop_tasks(self):
while self.tasks:
@ -79,6 +92,8 @@ class TorrentHandle:
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._base_path = status.save_path
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,
@ -178,6 +193,9 @@ class TorrentSession:
handle = self._handles[btih] = TorrentHandle(self._loop, self._executor, self._session.add_torrent(params))
handle._handle.force_dht_announce()
def full_path(self, btih):
return self._handles[btih].largest_file
async def add_torrent(self, btih, download_path):
await self._loop.run_in_executor(
self._executor, self._add_torrent, btih, download_path

View file

@ -42,6 +42,10 @@ class TorrentSource(ManagedDownloadSource):
rowid, content_fee, analytics_manager, added_on)
self.torrent_session = torrent_session
@property
def full_path(self) -> Optional[str]:
return self.torrent_session.full_path(self.identifier)
async def start(self, timeout: Optional[float] = None, save_now: Optional[bool] = False):
await self.torrent_session.add_torrent(self.identifier, self.download_directory)