def parse(cls, header: Header, block_type: BlockType, data: bytes): peers: List[Peer] = [] for i in range(0, len(data), 18): ip = data[i:i + 18] peers.append(Peer(ip)) # print(peers) return cls(header, block_type, peers)
def keyboard_normalize(friends: List[PeerDB], observables: List[PeerDB], buttons: List[List[InlineKeyboardButton]], payload: str) -> InlineKeyboardMarkup: if payload == 'pagination': peers_data = [ button.callback_data.split('.')[1:3] for row in buttons for button in row ][:-2:2] elif payload == 'alone_peer': peers_data = [ button.callback_data.split('.')[1:3] for row in buttons for button in row ][:2:2] else: peers_data = [ button.callback_data.split('.')[1:3] for row in buttons for button in row ][::2] peers = [] for peer in peers_data: peers.append(Peer(id=int(peer[0]), login=peer[1])) keyboard = peer_keyboard(peers=peers, friends=friends, observables=observables, payload=payload) if payload == 'pagination': pagination_buttons = buttons[-1:][0] keyboard.row(*pagination_buttons) elif payload == 'alone_peer': alone_peer_buttons = buttons[1:] [keyboard.row(*buttons) for buttons in alone_peer_buttons] return keyboard
def is_completed(self): local_peer = Peer.get_local_peer() for part_num in range(0, self.parts_count): if not self.peer_has_part(local_peer, part_num): return False return True
def get_peers_for_file_part(self, part_num): peers = [] for (peer_str, part_mask) in self.parts_masks.items(): if part_mask.is_available(part_num): peers.append(Peer.peer_from_string(peer_str)) return peers
def get_completed_file_parts_nums(cls, file_id): file = cls.find_file_by_id(file_id) parts = [] if file: for i in range(0, file.parts_count): if file.peer_has_part(Peer.get_local_peer(), i): parts.append(i) return parts else: raise Exception("File %s not found" %file_id)
def load_my_files(cls): peer_me = Peer.get_local_peer() for dirname, dirnames, filenames in os.walk(SHARED_PATH): for filename in filenames: path = dirname + "/" + filename file = File(hashing.generate_file_id(), filename,path) #Set that I have all this file for p in range(0, file.parts_count): file.set_peer_has_part(peer_me, p, True) cls.get_files().append(file)
def get_ordered_parts_number(cls, file_id): file = cls.find_file_by_id(file_id) if file: part_counter = Counter() ordered_parts = [] for p_num in range(0, file.parts_count): if file.parts_mask_for_peer(Peer.get_local_peer()).is_not_started(p_num): peers_count = len(file.get_peers_for_file_part(p_num)) part_counter[p_num] = peers_count for (part_num, frequency) in part_counter.most_common(): ordered_parts.append(part_num) return list(reversed(ordered_parts)) else: raise Exception("File not found: %s" %file_id)
def set_status_part_for_file(cls, file_id, part_num, status): file = cls.find_file_by_id(file_id) if file: file.set_peer_status_for_part(Peer.get_local_peer(), part_num, status) else: raise Exception("File %s not found" %file_id)
async def host_data_compile( self, user: User, host: str, page: int = 0) -> Tuple[str, Union[Peer, None]]: is_wrong = self._is_wrong_name(name=host) if is_wrong: return Config.local.host_not_found.get(user.language, host=is_wrong.replace( "<", "<")), None try: location_records = await Host().get_location_history(host=host) except (UnknownIntraError, TimeoutIntraError) as e: return f'{hbold(host, ":", sep="")} {e}', None except NotFoundIntraError: return Config.local.host_not_found.get(user.language, host=host.replace( "<", "<")), None if not location_records: return Config.local.host_not_found.get(user.language, host=host.replace( "<", "<")), None if page < 3: location = location_records[page] peer, peer_text = await self.peer_data_compile( user=user, login=location.login, is_single=True) last_peer = '' if not peer: peer_text = hcode(location.login) else: if location.end_at and not page: last_peer = Config.local.last_user.get(user.language) else: lines = peer_text.splitlines() if page: peer_text = '\n'.join(lines) else: peer_text = '\n'.join(lines[:-2]).replace('🟢 ', '') campus = await self._get_campus(campus_id=location.campus_id) log_time = self._get_log_time(begin_at_iso=location.begin_at, end_at_iso=location.end_at, time_zone=campus.time_zone, now=Config.local.now.get( user.language)) text = f'🖥 {hbold(campus.name)} {hcode(host)}\n{last_peer}' \ f'🕰 {log_time}\n' \ f'{peer_text}' return text, Peer(id=location.peer_id, login=location.login) texts = [] for location in location_records[3:]: campus = await self._get_campus(campus_id=location.campus_id) log_time = self._get_log_time(begin_at_iso=location.begin_at, end_at_iso=location.end_at, time_zone=campus.time_zone, now=Config.local.now.get( user.language)) text = f'🖥 {hbold(campus.name)} {hcode(host)}\n' \ f'🕰 {log_time}\n' \ f'👤 {hcode(location.login)}' texts.append(text) return f'\n\n'.join(texts), None