def _status_server_handler(manager): torrents = manager.get_torrents() if not torrents: return 'No torrents added' torrents.sort(key=lambda info: info.download_info.suggested_name) return [TorrentState(torrent_info) for torrent_info in torrents]
def _finish_downloading_piece(self, piece_index: int): piece_info = self._download_info.pieces[piece_index] piece_info.mark_as_downloaded() self._download_info.downloaded_piece_count += 1 self._download_info.interesting_pieces.remove(piece_index) peer_data = self._peer_manager.peer_data for peer in piece_info.owners: client = peer_data[peer].client for index in self._download_info.interesting_pieces: if client.piece_owned[index]: break else: client.am_interested = False for data in peer_data.values(): data.client.send_have(piece_index) self._logger.debug('piece %s finished', piece_index) torrent_state = TorrentState(self._torrent_info) self._logger.info('progress %.1lf%% (%s / %s pieces)', floor_to(torrent_state.progress * 100, 1), self._download_info.downloaded_piece_count, torrent_state.selected_piece_count) if pyqtSignal and self._download_info.downloaded_piece_count < torrent_state.selected_piece_count: cur_time = time.time() if self._last_piece_finish_signal_time is None or \ cur_time - self._last_piece_finish_signal_time >= Downloader.PIECE_FINISH_SIGNAL_MIN_INTERVAL: self.progress.emit() self._last_piece_finish_signal_time = time.time()
def _start_torrent_manager(self, torrent_info: TorrentInfo): info_hash = torrent_info.download_info.info_hash manager = TorrentManager(torrent_info, self._our_peer_id, self._server.port) if pyqtSignal: manager.state_changed.connect(lambda: self.torrent_changed.emit(TorrentState(torrent_info))) self._torrent_managers[info_hash] = manager self._torrent_manager_executors[info_hash] = asyncio.ensure_future(manager.run())
def add(self, torrent_info: TorrentInfo): info_hash = torrent_info.download_info.info_hash if info_hash in self._torrents: raise ValueError('This torrent is already added') if not torrent_info.paused: self._start_torrent_manager(torrent_info) self._torrents[info_hash] = torrent_info if pyqtSignal: self.torrent_added.emit(TorrentState(torrent_info))
def resume(self, info_hash: bytes): if info_hash not in self._torrents: raise ValueError('Torrent not found') torrent_info = self._torrents[info_hash] if not torrent_info.paused: raise ValueError('The torrent is already running') self._start_torrent_manager(torrent_info) torrent_info.paused = False if pyqtSignal: self.torrent_changed.emit(TorrentState(torrent_info))
async def pause(self, info_hash: bytes): if info_hash not in self._torrents: raise ValueError('Torrent not found') torrent_info = self._torrents[info_hash] if torrent_info.paused: raise ValueError('The torrent is already paused') await self._stop_torrent_manager(info_hash) torrent_info.paused = True if pyqtSignal: self.torrent_changed.emit(TorrentState(torrent_info))