mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-05 20:50:22 +03:00
Merge branch 'master' into asyncio
This commit is contained in:
commit
abcd09e7d0
6
setup.py
6
setup.py
|
@ -127,6 +127,11 @@ def main():
|
||||||
|
|
||||||
license='MIT',
|
license='MIT',
|
||||||
|
|
||||||
|
# See https://stackoverflow.com/a/40300957/4759433
|
||||||
|
# -> https://www.python.org/dev/peps/pep-0345/#requires-python
|
||||||
|
# -> http://setuptools.readthedocs.io/en/latest/setuptools.html
|
||||||
|
python_requires='>=3.4',
|
||||||
|
|
||||||
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
||||||
classifiers=[
|
classifiers=[
|
||||||
# 3 - Alpha
|
# 3 - Alpha
|
||||||
|
@ -140,7 +145,6 @@ def main():
|
||||||
'License :: OSI Approved :: MIT License',
|
'License :: OSI Approved :: MIT License',
|
||||||
|
|
||||||
'Programming Language :: Python :: 3',
|
'Programming Language :: Python :: 3',
|
||||||
'Programming Language :: Python :: 3.3',
|
|
||||||
'Programming Language :: Python :: 3.4',
|
'Programming Language :: Python :: 3.4',
|
||||||
'Programming Language :: Python :: 3.5',
|
'Programming Language :: Python :: 3.5',
|
||||||
'Programming Language :: Python :: 3.6'
|
'Programming Language :: Python :: 3.6'
|
||||||
|
|
|
@ -81,7 +81,7 @@ def parse(message, delimiters=None, url_re=None):
|
||||||
))
|
))
|
||||||
|
|
||||||
result.append(MessageEntityTextUrl(
|
result.append(MessageEntityTextUrl(
|
||||||
offset=i, length=len(url_match.group(1)),
|
offset=url_match.start(), length=len(url_match.group(1)),
|
||||||
url=_del_surrogate(url_match.group(2))
|
url=_del_surrogate(url_match.group(2))
|
||||||
))
|
))
|
||||||
i += len(url_match.group(1))
|
i += len(url_match.group(1))
|
||||||
|
|
|
@ -196,14 +196,15 @@ class MemorySession(Session):
|
||||||
result = self.get_entity_rows_by_name(key)
|
result = self.get_entity_rows_by_name(key)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
i, h = result # unpack resulting tuple
|
entity_id, entity_hash = result # unpack resulting tuple
|
||||||
i, k = utils.resolve_id(i) # removes the mark and returns kind
|
entity_id, kind = utils.resolve_id(entity_id)
|
||||||
if k == PeerUser:
|
# removes the mark and returns type of entity
|
||||||
return InputPeerUser(i, h)
|
if kind == PeerUser:
|
||||||
elif k == PeerChat:
|
return InputPeerUser(entity_id, entity_hash)
|
||||||
return InputPeerChat(i)
|
elif kind == PeerChat:
|
||||||
elif k == PeerChannel:
|
return InputPeerChat(entity_id)
|
||||||
return InputPeerChannel(i, h)
|
elif kind == PeerChannel:
|
||||||
|
return InputPeerChannel(entity_id, entity_hash)
|
||||||
else:
|
else:
|
||||||
raise ValueError('Could not find input entity with key ', key)
|
raise ValueError('Could not find input entity with key ', key)
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,8 @@ from .errors import (
|
||||||
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
|
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
|
||||||
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError,
|
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError,
|
||||||
SessionPasswordNeededError, FileMigrateError, PhoneNumberUnoccupiedError,
|
SessionPasswordNeededError, FileMigrateError, PhoneNumberUnoccupiedError,
|
||||||
PhoneNumberOccupiedError, EmailUnconfirmedError, PasswordEmptyError
|
PhoneNumberOccupiedError, EmailUnconfirmedError, PasswordEmptyError,
|
||||||
|
UsernameNotOccupiedError
|
||||||
)
|
)
|
||||||
from .network import ConnectionMode
|
from .network import ConnectionMode
|
||||||
from .tl.custom import Draft, Dialog
|
from .tl.custom import Draft, Dialog
|
||||||
|
@ -190,9 +191,6 @@ 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
|
||||||
|
|
||||||
# Don't call .get_dialogs() every time a .get_entity() fails
|
|
||||||
self._called_get_dialogs = False
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Telegram requests functions
|
# region Telegram requests functions
|
||||||
|
@ -679,13 +677,18 @@ class TelegramClient(TelegramBareClient):
|
||||||
if not parse_mode:
|
if not parse_mode:
|
||||||
return message, []
|
return message, []
|
||||||
|
|
||||||
parse_mode = parse_mode.lower()
|
if isinstance(parse_mode, str):
|
||||||
if parse_mode in {'md', 'markdown'}:
|
parse_mode = parse_mode.lower()
|
||||||
message, msg_entities = markdown.parse(message)
|
if parse_mode in {'md', 'markdown'}:
|
||||||
elif parse_mode.startswith('htm'):
|
message, msg_entities = markdown.parse(message)
|
||||||
message, msg_entities = html.parse(message)
|
elif parse_mode.startswith('htm'):
|
||||||
|
message, msg_entities = html.parse(message)
|
||||||
|
else:
|
||||||
|
raise ValueError('Unknown parsing mode: {}'.format(parse_mode))
|
||||||
|
elif callable(parse_mode):
|
||||||
|
message, msg_entities = parse_mode(message)
|
||||||
else:
|
else:
|
||||||
raise ValueError('Unknown parsing mode: {}'.format(parse_mode))
|
raise TypeError('Invalid parsing mode type: {}'.format(parse_mode))
|
||||||
|
|
||||||
for i, e in enumerate(msg_entities):
|
for i, e in enumerate(msg_entities):
|
||||||
if isinstance(e, MessageEntityTextUrl):
|
if isinstance(e, MessageEntityTextUrl):
|
||||||
|
@ -2377,15 +2380,21 @@ class TelegramClient(TelegramBareClient):
|
||||||
invite = await self(CheckChatInviteRequest(username))
|
invite = await self(CheckChatInviteRequest(username))
|
||||||
if isinstance(invite, ChatInvite):
|
if isinstance(invite, ChatInvite):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'Cannot get entity from a channel '
|
'Cannot get entity from a channel (or group) '
|
||||||
'(or group) that you are not part of'
|
'that you are not part of. Join the group and retry'
|
||||||
)
|
)
|
||||||
elif isinstance(invite, ChatInviteAlready):
|
elif isinstance(invite, ChatInviteAlready):
|
||||||
return invite.chat
|
return invite.chat
|
||||||
elif username:
|
elif username:
|
||||||
if username in ('me', 'self'):
|
if username in ('me', 'self'):
|
||||||
return await self.get_me()
|
return await self.get_me()
|
||||||
result = await self(ResolveUsernameRequest(username))
|
|
||||||
|
try:
|
||||||
|
result = await self(ResolveUsernameRequest(username))
|
||||||
|
except UsernameNotOccupiedError as e:
|
||||||
|
raise ValueError('No user has "{}" as username'
|
||||||
|
.format(username)) from e
|
||||||
|
|
||||||
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 getattr(entity, 'username', None) or ''\
|
||||||
.lower() == username:
|
.lower() == username:
|
||||||
|
@ -2397,8 +2406,8 @@ class TelegramClient(TelegramBareClient):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
raise TypeError(
|
raise ValueError(
|
||||||
'Cannot turn "{}" into any entity (user or chat)'.format(string)
|
'Cannot find any entity corresponding to "{}"'.format(string)
|
||||||
)
|
)
|
||||||
|
|
||||||
async def get_input_entity(self, peer):
|
async def get_input_entity(self, peer):
|
||||||
|
@ -2444,25 +2453,12 @@ class TelegramClient(TelegramBareClient):
|
||||||
'Cannot turn "{}" into an input entity.'.format(original_peer)
|
'Cannot turn "{}" into an input entity.'.format(original_peer)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add the mark to the peers if the user passed a Peer (not an int),
|
raise ValueError(
|
||||||
# or said ID is negative. If it's negative it's been marked already.
|
|
||||||
# Look in the dialogs with the hope to find it.
|
|
||||||
if not self._called_get_dialogs:
|
|
||||||
self._called_get_dialogs = True
|
|
||||||
mark = not isinstance(peer, int) or peer < 0
|
|
||||||
target_id = utils.get_peer_id(peer)
|
|
||||||
if mark:
|
|
||||||
async for dialog in self.iter_dialogs(100):
|
|
||||||
if utils.get_peer_id(dialog.entity) == target_id:
|
|
||||||
return utils.get_input_peer(dialog.entity)
|
|
||||||
else:
|
|
||||||
async for dialog in self.iter_dialogs(100):
|
|
||||||
if dialog.entity.id == target_id:
|
|
||||||
return utils.get_input_peer(dialog.entity)
|
|
||||||
|
|
||||||
raise TypeError(
|
|
||||||
'Could not find the input entity corresponding to "{}". '
|
'Could not find the input entity corresponding to "{}". '
|
||||||
'Make sure you have encountered this peer before.'.format(peer)
|
'Make sure you have encountered this user/chat/channel before. '
|
||||||
|
'If the peer is in your dialogs call client.get_dialogs().'
|
||||||
|
'If the peer belongs to a chat call client.get_participants().'
|
||||||
|
.format(peer)
|
||||||
)
|
)
|
||||||
|
|
||||||
async def edit_2fa(self, current_password=None, new_password=None, hint='',
|
async def edit_2fa(self, current_password=None, new_password=None, hint='',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user