async def get_input_entity(self, key): try: if key.SUBCLASS_OF_ID in (0xc91c90b6, 0xe669bf46, 0x40f202fd): # hex(crc32(b'InputPeer', b'InputUser' and b'InputChannel')) # We already have an Input version, so nothing else required return key # Try to early return if this key can be casted as input peer return utils.get_input_peer(key) except (AttributeError, TypeError): # Not a TLObject or can't be cast into InputPeer if isinstance(key, types.TLObject): key = utils.get_peer_id(key) exact = True else: exact = not isinstance(key, int) or key < 0 result = None if isinstance(key, str): phone = utils.parse_phone(key) if phone: result = await self.get_entity_rows_by_phone(phone) else: username, invite = utils.parse_username(key) if username and not invite: result = await self.get_entity_rows_by_username(username) else: tup = utils.resolve_invite_link(key)[1] if tup: result = await self.get_entity_rows_by_id(tup, exact=False) elif isinstance(key, int): result = await self.get_entity_rows_by_id(key, exact) if not result and isinstance(key, str): result = await self.get_entity_rows_by_name(key) if result: entity_id, entity_hash = result # unpack resulting tuple entity_id, kind = utils.resolve_id(entity_id) # removes the mark and returns type of entity if kind == types.PeerUser: return types.InputPeerUser(entity_id, entity_hash) elif kind == types.PeerChat: return types.InputPeerChat(entity_id) elif kind == types.PeerChannel: return types.InputPeerChannel(entity_id, entity_hash) else: raise ValueError('Could not find input entity with key ', key)
def iter_resume_entities(self, context_id): """ Returns an iterator over the entities that need resuming for the given context_id. Note that the entities are *removed* once the iterator is consumed completely. """ c = self.conn.execute("SELECT ID, AccessHash FROM ResumeEntity " "WHERE ContextID = ?", (context_id,)) row = c.fetchone() while row: kind = resolve_id(row[0])[1] if kind == types.PeerUser: yield types.InputPeerUser(row[0], row[1]) elif kind == types.PeerChat: yield types.InputPeerChat(row[0]) elif kind == types.PeerChannel: yield types.InputPeerChannel(row[0], row[1]) row = c.fetchone() c.execute("DELETE FROM ResumeEntity WHERE ContextID = ?", (context_id,))
async def get_resume_entities(self, context_id): """ Returns an iterator over the entities that need resuming for the given context_id. Note that the entities are *removed* once the iterator is consumed completely. """ rows = db_resume_entity.find({'context_id': context_id}) count = await db_resume_entity.count_documents( {'context_id': context_id}) logger.info(f'加载了【{count}】条待恢复数据') result = [] async for row in rows: kind = resolve_id(row['id'])[1] if kind == types.PeerUser: result.append( types.InputPeerUser(row['id'], row['access_hash'])) elif kind == types.PeerChat: result.append(types.InputPeerChat(row['id'])) elif kind == types.PeerChannel: result.append( types.InputPeerChannel(row['id'], row['access_hash'])) await db_resume_entity.delete_many({'context_id': context_id}) return result