def _wait_for_piece(self, piece_index): if not self.torrent_handle.have_piece(piece_index): end_piece_index = utils.piece_from_offset(self.torrent_handle, self.torrent_file.offset + self.torrent_file.size) if (end_piece_index - piece_index) <= utils.get_preload_buffer_piece_count(self.torrent_handle, self.torrent_file) * 10: self.bus.log('[FileWrapper] Virtual read for piece {0}'.format(piece_index)) self.virtual_read = True return self.bus.log('[FileWrapper] Waiting for piece {0}'.format(piece_index)) while not self.torrent_handle.have_piece(piece_index): time.sleep(0.1) self.bus.log('[FileWrapper] Piece {0} downloaded'.format(piece_index))
def _wait_for_piece(self, piece_index): if not self.torrent_handle.have_piece(piece_index): end_piece_index = utils.piece_from_offset( self.torrent_handle, self.torrent_file.offset + self.torrent_file.size) if (end_piece_index - piece_index) <= utils.get_preload_buffer_piece_count( self.torrent_handle, self.torrent_file) * 2: self.bus.log('[FileWrapper] Virtual read for piece {0}'.format( piece_index)) self.virtual_read = True return self.bus.log( '[FileWrapper] Waiting for piece {0}'.format(piece_index)) while not self.torrent_handle.have_piece(piece_index): time.sleep(0.1) self.bus.log( '[FileWrapper] Piece {0} downloaded'.format(piece_index))
def is_video_file_ready(self, torrent_handle, is_fast, log_enabled=True): if torrent_handle: status = torrent_handle.status() video_file = self._get_video_file_from_torrent(torrent_handle) if int(status.state) >= 3 and video_file: complete_pieces = utils.get_video_file_complete_pieces(torrent_handle, video_file) total_pieces = utils.get_video_file_total_pieces(torrent_handle, video_file) needed_pieces = utils.get_preload_buffer_piece_count(torrent_handle, video_file) if is_fast or complete_pieces >= needed_pieces: return True else: if log_enabled: self.bus.log('[Downloader] Not enough pieces yet: {0}/{1} (total: {2}) @ {3} kB/s'.format(complete_pieces, needed_pieces, total_pieces, status.download_rate / 1024)) else: if log_enabled: self.bus.log('[Downloader] Not ready yet: {0}'.format(str(status.state))) else: if log_enabled: self.bus.log('[Downloader] Not ready yet') return False
def get_status(self): result = {} if self.session: result['session'] = self.torrent_config result['session']['connection_sets'] = [] for info_hash, connection_set in self.bus.connection_monitor.torrent_connections.iteritems(): connection_set_status = { 'info_hash': info_hash, 'timestamp': connection_set['timestamp'], 'connections':[] } for connection in connection_set['set']: connection_status = { 'info_hash': info_hash, 'connection': connection } connection_set_status['connections'].append(connection_status) result['session']['connection_sets'].append(connection_set_status) result['session']['torrents'] = [] for torrent_handle in self.torrent_handles: torrent_valid = torrent_handle.has_metadata() torrent_status = torrent_handle.status() if torrent_valid else None try: torrent = {} torrent['paused'] = torrent_status.paused torrent['state'] = str(torrent_status.state) torrent['state_index'] = int(torrent_status.state) torrent['progress'] = math.trunc(torrent_status.progress * 100.0) / 100.0 torrent['download_rate'] = torrent_status.download_rate / 1024 torrent['upload_rate'] = torrent_status.upload_rate / 1024 torrent['num_seeds'] = torrent_status.num_seeds torrent['total_seeds'] = torrent_status.num_complete torrent['num_peers'] = torrent_status.num_peers torrent['total_peers'] = torrent_status.num_incomplete torrent['info_hash'] = str(torrent_handle.info_hash()) video_file = self._get_video_file_from_torrent(torrent_handle) if video_file: torrent['video_file'] = {} torrent['video_file']['path'] = video_file.path torrent['video_file']['size'] = video_file.size torrent['video_file']['start_piece_index'] = utils.piece_from_offset(torrent_handle, video_file.offset) torrent['video_file']['end_piece_index'] = utils.piece_from_offset(torrent_handle, video_file.offset + video_file.size) torrent['video_file']['total_pieces'] = utils.get_video_file_total_pieces(torrent_handle, video_file) torrent['video_file']['preload_buffer_pieces'] = utils.get_preload_buffer_piece_count(torrent_handle, video_file) torrent['video_file']['is_ready_fast'] = self.is_video_file_ready(torrent_handle, True, False) torrent['video_file']['is_ready_slow'] = self.is_video_file_ready(torrent_handle, False, False) torrent['video_file']['complete_pieces'] = utils.get_video_file_complete_pieces(torrent_handle, video_file) piece_map = '' for piece_index in range(torrent['video_file']['start_piece_index'], torrent['video_file']['end_piece_index'] + 1): if torrent_handle.have_piece(piece_index): piece_map = piece_map + '*' elif torrent_handle.piece_priority(piece_index) == 0: piece_map = piece_map + '0' elif torrent_handle.piece_priority(piece_index) == 7: piece_map = piece_map + '7' else: piece_map = piece_map + '.' torrent['video_file']['piece_map'] = piece_map except: pass result['session']['torrents'].append(torrent) return result