mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-02-27 08:50:41 +03:00
Use builtin base64.urlsafe_b64decode and fix docstring
This commit is contained in:
parent
2ca8ddbfb4
commit
3612652f39
|
@ -761,23 +761,18 @@ def _rle_decode(data):
|
||||||
|
|
||||||
def _decode_telegram_base64(string):
|
def _decode_telegram_base64(string):
|
||||||
"""
|
"""
|
||||||
Decodes an url-safe base64-encoded string into its bytes.
|
Decodes an url-safe base64-encoded string into its bytes
|
||||||
|
by first adding the stripped necessary padding characters.
|
||||||
|
|
||||||
This is the way Telegram shares binary data as strings,
|
This is the way Telegram shares binary data as strings,
|
||||||
such as Bot API-style file IDs or invite links.
|
such as Bot API-style file IDs or invite links.
|
||||||
|
|
||||||
|
Returns ``None`` if the input string was not valid.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
data = string.encode('ascii')
|
return base64.urlsafe_b64decode(string + '=' * (len(string) % 4))
|
||||||
except (UnicodeEncodeError, AttributeError):
|
except (binascii.Error, ValueError, TypeError):
|
||||||
return None
|
return None # not valid base64, not valid ascii, not a string
|
||||||
|
|
||||||
data = data.replace(b'-', b'+').replace(
|
|
||||||
b'_', b'/') + b'=' * (len(data) % 4)
|
|
||||||
|
|
||||||
try:
|
|
||||||
return base64.b64decode(data)
|
|
||||||
except binascii.Error:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_bot_file_id(file_id):
|
def resolve_bot_file_id(file_id):
|
||||||
|
@ -850,16 +845,16 @@ def resolve_bot_file_id(file_id):
|
||||||
def resolve_invite_link(link):
|
def resolve_invite_link(link):
|
||||||
"""
|
"""
|
||||||
Resolves the given invite link. Returns a tuple of
|
Resolves the given invite link. Returns a tuple of
|
||||||
``(creator user id, global chat id, random int)``.
|
``(link creator user id, global chat id, random int)``.
|
||||||
|
|
||||||
Note that for broadcast channels, the creator user
|
Note that for broadcast channels, the link creator
|
||||||
ID will be zero to protect their identity. Normal
|
user ID will be zero to protect their identity.
|
||||||
chats and megagroup channels will have creator ID.
|
Normal chats and megagroup channels will have such ID.
|
||||||
|
|
||||||
Note that the chat ID may not be accurate for chats
|
Note that the chat ID may not be accurate for chats
|
||||||
with a link that were upgraded to megagroup, since
|
with a link that were upgraded to megagroup, since
|
||||||
the link can remain the same, but the ID will be
|
the link can remain the same, but the chat ID will
|
||||||
correct once a new link is generated.
|
be correct once a new link is generated.
|
||||||
"""
|
"""
|
||||||
link_hash, is_link = parse_username(link)
|
link_hash, is_link = parse_username(link)
|
||||||
if not is_link:
|
if not is_link:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user