Added .me to client instance

This commit is contained in:
Konstantin M 2018-03-14 01:49:18 +03:00
parent af9c904fb9
commit a6be799ac8

View File

@ -192,6 +192,9 @@ class TelegramClient(TelegramBareClient):
# Sometimes we need to know who we are, cache the self peer # Sometimes we need to know who we are, cache the self peer
self._self_input_peer = None self._self_input_peer = None
# Store get_me() after successful sign in
self.me = None
# endregion # endregion
# region Telegram requests functions # region Telegram requests functions
@ -298,10 +301,12 @@ class TelegramClient(TelegramBareClient):
if self.is_user_authorized(): if self.is_user_authorized():
self._check_events_pending_resolve() self._check_events_pending_resolve()
self.me = self.get_me()
return self return self
if bot_token: if bot_token:
self.sign_in(bot_token=bot_token) self.sign_in(bot_token=bot_token)
self.me = self.get_me()
return self return self
# Turn the callable into a valid phone number # Turn the callable into a valid phone number
@ -355,6 +360,7 @@ class TelegramClient(TelegramBareClient):
# We won't reach here if any step failed (exit by exception) # We won't reach here if any step failed (exit by exception)
print('Signed in successfully as', utils.get_display_name(me)) print('Signed in successfully as', utils.get_display_name(me))
self._check_events_pending_resolve() self._check_events_pending_resolve()
self.me = self.get_me()
return self return self
def sign_in(self, phone=None, code=None, def sign_in(self, phone=None, code=None,
@ -1152,9 +1158,9 @@ class TelegramClient(TelegramBareClient):
raise TypeError('Invalid message type: {}'.format(type(message))) raise TypeError('Invalid message type: {}'.format(type(message)))
def iter_participants(self, entity, limit=None, search='', def iter_participants(self, entity, limit=None, search='',
filter=None, aggressive=False, _total_box=None): aggressive=False, _total_box=None):
""" """
Iterator over the participants belonging to the specified chat. Gets the list of participants from the specified entity.
Args: Args:
entity (:obj:`entity`): entity (:obj:`entity`):
@ -1166,12 +1172,6 @@ class TelegramClient(TelegramBareClient):
search (:obj:`str`, optional): search (:obj:`str`, optional):
Look for participants with this string in name/username. Look for participants with this string in name/username.
filter (:obj:`ChannelParticipantsFilter`, optional):
The filter to be used, if you want e.g. only admins. See
https://lonamiwebs.github.io/Telethon/types/channel_participants_filter.html.
Note that you might not have permissions for some filter.
This has no effect for normal chats or users.
aggressive (:obj:`bool`, optional): aggressive (:obj:`bool`, optional):
Aggressively looks for all participants in the chat in Aggressively looks for all participants in the chat in
order to get more than 10,000 members (a hard limit order to get more than 10,000 members (a hard limit
@ -1180,32 +1180,16 @@ class TelegramClient(TelegramBareClient):
participants on groups with 100,000 members. participants on groups with 100,000 members.
This has no effect for groups or channels with less than This has no effect for groups or channels with less than
10,000 members, or if a ``filter`` is given. 10,000 members.
_total_box (:obj:`_Box`, optional): _total_box (:obj:`_Box`, optional):
A _Box instance to pass the total parameter by reference. A _Box instance to pass the total parameter by reference.
Yields: Returns:
The ``User`` objects returned by ``GetParticipantsRequest`` A list of participants with an additional .total variable on the
with an additional ``.participant`` attribute which is the list indicating the total amount of members in this group/channel.
matched ``ChannelParticipant`` type for channels/megagroups
or ``ChatParticipants`` for normal chats.
""" """
if isinstance(filter, type):
filter = filter()
entity = self.get_input_entity(entity) entity = self.get_input_entity(entity)
if search and (filter or not isinstance(entity, InputPeerChannel)):
# We need to 'search' ourselves unless we have a PeerChannel
search = search.lower()
def filter_entity(ent):
return search in utils.get_display_name(ent).lower() or\
search in (getattr(ent, 'username', '') or None).lower()
else:
def filter_entity(ent):
return True
limit = float('inf') if limit is None else int(limit) limit = float('inf') if limit is None else int(limit)
if isinstance(entity, InputPeerChannel): if isinstance(entity, InputPeerChannel):
total = self(GetFullChannelRequest( total = self(GetFullChannelRequest(
@ -1218,7 +1202,7 @@ class TelegramClient(TelegramBareClient):
return return
seen = set() seen = set()
if total > 10000 and aggressive and not filter: if total > 10000 and aggressive:
requests = [GetParticipantsRequest( requests = [GetParticipantsRequest(
channel=entity, channel=entity,
filter=ChannelParticipantsSearch(search + chr(x)), filter=ChannelParticipantsSearch(search + chr(x)),
@ -1229,7 +1213,7 @@ class TelegramClient(TelegramBareClient):
else: else:
requests = [GetParticipantsRequest( requests = [GetParticipantsRequest(
channel=entity, channel=entity,
filter=filter or ChannelParticipantsSearch(search), filter=ChannelParticipantsSearch(search),
offset=0, offset=0,
limit=200, limit=200,
hash=0 hash=0
@ -1255,47 +1239,31 @@ class TelegramClient(TelegramBareClient):
if not participants.users: if not participants.users:
requests.pop(i) requests.pop(i)
else: else:
requests[i].offset += len(participants.participants) requests[i].offset += len(participants.users)
users = {user.id: user for user in participants.users} for user in participants.users:
for participant in participants.participants: if user.id not in seen:
user = users[participant.user_id] seen.add(user.id)
if not filter_entity(user) or user.id in seen: yield user
continue if len(seen) >= limit:
return
seen.add(participant.user_id)
user = users[participant.user_id]
user.participant = participant
yield user
if len(seen) >= limit:
return
elif isinstance(entity, InputPeerChat): elif isinstance(entity, InputPeerChat):
# TODO We *could* apply the `filter` here ourselves users = self(GetFullChatRequest(entity.chat_id)).users
full = self(GetFullChatRequest(entity.chat_id))
if _total_box: if _total_box:
_total_box.x = len(full.full_chat.participants.participants) _total_box.x = len(users)
have = 0 have = 0
users = {user.id: user for user in full.users} for user in users:
for participant in full.full_chat.participants.participants:
user = users[participant.user_id]
if not filter_entity(user):
continue
have += 1 have += 1
if have > limit: if have > limit:
break break
else: else:
user = users[participant.user_id]
user.participant = participant
yield user yield user
else: else:
if _total_box: if _total_box:
_total_box.x = 1 _total_box.x = 1
if limit != 0: if limit != 0:
user = self.get_entity(entity) yield self.get_entity(entity)
if filter_entity(user):
user.participant = None
yield user
def get_participants(self, *args, **kwargs): def get_participants(self, *args, **kwargs):
""" """
@ -1304,9 +1272,9 @@ class TelegramClient(TelegramBareClient):
""" """
total_box = _Box(0) total_box = _Box(0)
kwargs['_total_box'] = total_box kwargs['_total_box'] = total_box
participants = UserList(self.iter_participants(*args, **kwargs)) dialogs = UserList(self.iter_participants(*args, **kwargs))
participants.total = total_box.x dialogs.total = total_box.x
return participants return dialogs
# endregion # endregion
@ -2340,8 +2308,7 @@ class TelegramClient(TelegramBareClient):
return self.get_me() return self.get_me()
result = self(ResolveUsernameRequest(username)) result = self(ResolveUsernameRequest(username))
for entity in itertools.chain(result.users, result.chats): for entity in itertools.chain(result.users, result.chats):
if getattr(entity, 'username', None) or ''\ if entity.username.lower() == username:
.lower() == username:
return entity return entity
try: try:
# Nobody with this username, maybe it's an exact name/title # Nobody with this username, maybe it's an exact name/title