mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-10 19:46:36 +03:00
Use types. namespace in utils
This commit is contained in:
parent
ace7254344
commit
52292d77fb
|
@ -14,26 +14,6 @@ from types import GeneratorType
|
||||||
from .extensions import markdown, html
|
from .extensions import markdown, html
|
||||||
from .helpers import add_surrogate, del_surrogate
|
from .helpers import add_surrogate, del_surrogate
|
||||||
from .tl import types
|
from .tl import types
|
||||||
from .tl.types import (
|
|
||||||
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
|
||||||
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
|
|
||||||
MessageMediaDocument, MessageMediaPhoto, PeerChannel, InputChannel,
|
|
||||||
UserEmpty, InputUser, InputUserEmpty, InputUserSelf, InputPeerSelf,
|
|
||||||
PeerChat, PeerUser, User, UserFull, UserProfilePhoto, Document,
|
|
||||||
MessageMediaContact, MessageMediaEmpty, MessageMediaGame, MessageMediaGeo,
|
|
||||||
MessageMediaUnsupported, MessageMediaVenue, InputMediaContact,
|
|
||||||
InputMediaDocument, InputMediaEmpty, InputMediaGame,
|
|
||||||
InputMediaGeoPoint, InputMediaPhoto, InputMediaVenue, InputDocument,
|
|
||||||
DocumentEmpty, InputDocumentEmpty, GeoPoint, InputGeoPoint,
|
|
||||||
GeoPointEmpty, InputGeoPointEmpty, Photo, InputPhoto, PhotoEmpty,
|
|
||||||
InputPhotoEmpty, FileLocation, ChatPhotoEmpty, UserProfilePhotoEmpty,
|
|
||||||
FileLocationUnavailable, InputMediaUploadedDocument, ChannelFull,
|
|
||||||
InputMediaUploadedPhoto, DocumentAttributeFilename, photos,
|
|
||||||
TopPeer, InputNotifyPeer, InputMessageID, InputFileLocation,
|
|
||||||
InputDocumentFileLocation, PhotoSizeEmpty, InputDialogPeer,
|
|
||||||
DocumentAttributeAudio, DocumentAttributeVideo
|
|
||||||
)
|
|
||||||
from .tl.types.contacts import ResolvedPeer
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import hachoir
|
import hachoir
|
||||||
|
@ -82,7 +62,7 @@ def get_display_name(entity):
|
||||||
Gets the display name for the given entity, if it's an :tl:`User`,
|
Gets the display name for the given entity, if it's an :tl:`User`,
|
||||||
:tl:`Chat` or :tl:`Channel`. Returns an empty string otherwise.
|
:tl:`Chat` or :tl:`Channel`. Returns an empty string otherwise.
|
||||||
"""
|
"""
|
||||||
if isinstance(entity, User):
|
if isinstance(entity, types.User):
|
||||||
if entity.last_name and entity.first_name:
|
if entity.last_name and entity.first_name:
|
||||||
return '{} {}'.format(entity.first_name, entity.last_name)
|
return '{} {}'.format(entity.first_name, entity.last_name)
|
||||||
elif entity.first_name:
|
elif entity.first_name:
|
||||||
|
@ -92,7 +72,7 @@ def get_display_name(entity):
|
||||||
else:
|
else:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
elif isinstance(entity, (Chat, Channel)):
|
elif isinstance(entity, (types.Chat, types.Channel)):
|
||||||
return entity.title
|
return entity.title
|
||||||
|
|
||||||
return ''
|
return ''
|
||||||
|
@ -102,13 +82,14 @@ def get_extension(media):
|
||||||
"""Gets the corresponding extension for any Telegram media."""
|
"""Gets the corresponding extension for any Telegram media."""
|
||||||
|
|
||||||
# Photos are always compressed as .jpg by Telegram
|
# Photos are always compressed as .jpg by Telegram
|
||||||
if isinstance(media, (UserProfilePhoto, ChatPhoto, MessageMediaPhoto)):
|
if isinstance(media, (types.UserProfilePhoto,
|
||||||
|
types.ChatPhoto, types.MessageMediaPhoto)):
|
||||||
return '.jpg'
|
return '.jpg'
|
||||||
|
|
||||||
# Documents will come with a mime type
|
# Documents will come with a mime type
|
||||||
if isinstance(media, MessageMediaDocument):
|
if isinstance(media, types.MessageMediaDocument):
|
||||||
media = media.document
|
media = media.document
|
||||||
if isinstance(media, Document):
|
if isinstance(media, types.Document):
|
||||||
if media.mime_type == 'application/octet-stream':
|
if media.mime_type == 'application/octet-stream':
|
||||||
# Octet stream are just bytes, which have no default extension
|
# Octet stream are just bytes, which have no default extension
|
||||||
return ''
|
return ''
|
||||||
|
@ -140,38 +121,38 @@ def get_input_peer(entity, allow_self=True):
|
||||||
else:
|
else:
|
||||||
_raise_cast_fail(entity, 'InputPeer')
|
_raise_cast_fail(entity, 'InputPeer')
|
||||||
|
|
||||||
if isinstance(entity, User):
|
if isinstance(entity, types.User):
|
||||||
if entity.is_self and allow_self:
|
if entity.is_self and allow_self:
|
||||||
return InputPeerSelf()
|
return types.InputPeerSelf()
|
||||||
else:
|
else:
|
||||||
return InputPeerUser(entity.id, entity.access_hash or 0)
|
return types.InputPeerUser(entity.id, entity.access_hash or 0)
|
||||||
|
|
||||||
if isinstance(entity, (Chat, ChatEmpty, ChatForbidden)):
|
if isinstance(entity, (types.Chat, types.ChatEmpty, types.ChatForbidden)):
|
||||||
return InputPeerChat(entity.id)
|
return types.InputPeerChat(entity.id)
|
||||||
|
|
||||||
if isinstance(entity, (Channel, ChannelForbidden)):
|
if isinstance(entity, (types.Channel, types.ChannelForbidden)):
|
||||||
return InputPeerChannel(entity.id, entity.access_hash or 0)
|
return types.InputPeerChannel(entity.id, entity.access_hash or 0)
|
||||||
|
|
||||||
if isinstance(entity, InputUser):
|
if isinstance(entity, types.InputUser):
|
||||||
return InputPeerUser(entity.user_id, entity.access_hash)
|
return types.InputPeerUser(entity.user_id, entity.access_hash)
|
||||||
|
|
||||||
if isinstance(entity, InputChannel):
|
if isinstance(entity, types.InputChannel):
|
||||||
return InputPeerChannel(entity.channel_id, entity.access_hash)
|
return types.InputPeerChannel(entity.channel_id, entity.access_hash)
|
||||||
|
|
||||||
if isinstance(entity, InputUserSelf):
|
if isinstance(entity, types.InputUserSelf):
|
||||||
return InputPeerSelf()
|
return types.InputPeerSelf()
|
||||||
|
|
||||||
if isinstance(entity, UserEmpty):
|
if isinstance(entity, types.UserEmpty):
|
||||||
return InputPeerEmpty()
|
return types.InputPeerEmpty()
|
||||||
|
|
||||||
if isinstance(entity, UserFull):
|
if isinstance(entity, types.UserFull):
|
||||||
return get_input_peer(entity.user)
|
return get_input_peer(entity.user)
|
||||||
|
|
||||||
if isinstance(entity, ChatFull):
|
if isinstance(entity, types.ChatFull):
|
||||||
return InputPeerChat(entity.id)
|
return types.InputPeerChat(entity.id)
|
||||||
|
|
||||||
if isinstance(entity, PeerChat):
|
if isinstance(entity, types.PeerChat):
|
||||||
return InputPeerChat(entity.chat_id)
|
return types.InputPeerChat(entity.chat_id)
|
||||||
|
|
||||||
_raise_cast_fail(entity, 'InputPeer')
|
_raise_cast_fail(entity, 'InputPeer')
|
||||||
|
|
||||||
|
@ -184,11 +165,11 @@ def get_input_channel(entity):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(entity, 'InputChannel')
|
_raise_cast_fail(entity, 'InputChannel')
|
||||||
|
|
||||||
if isinstance(entity, (Channel, ChannelForbidden)):
|
if isinstance(entity, (types.Channel, types.ChannelForbidden)):
|
||||||
return InputChannel(entity.id, entity.access_hash or 0)
|
return types.InputChannel(entity.id, entity.access_hash or 0)
|
||||||
|
|
||||||
if isinstance(entity, InputPeerChannel):
|
if isinstance(entity, types.InputPeerChannel):
|
||||||
return InputChannel(entity.channel_id, entity.access_hash)
|
return types.InputChannel(entity.channel_id, entity.access_hash)
|
||||||
|
|
||||||
_raise_cast_fail(entity, 'InputChannel')
|
_raise_cast_fail(entity, 'InputChannel')
|
||||||
|
|
||||||
|
@ -201,23 +182,23 @@ def get_input_user(entity):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(entity, 'InputUser')
|
_raise_cast_fail(entity, 'InputUser')
|
||||||
|
|
||||||
if isinstance(entity, User):
|
if isinstance(entity, types.User):
|
||||||
if entity.is_self:
|
if entity.is_self:
|
||||||
return InputUserSelf()
|
return types.InputUserSelf()
|
||||||
else:
|
else:
|
||||||
return InputUser(entity.id, entity.access_hash or 0)
|
return types.InputUser(entity.id, entity.access_hash or 0)
|
||||||
|
|
||||||
if isinstance(entity, InputPeerSelf):
|
if isinstance(entity, types.InputPeerSelf):
|
||||||
return InputUserSelf()
|
return types.InputUserSelf()
|
||||||
|
|
||||||
if isinstance(entity, (UserEmpty, InputPeerEmpty)):
|
if isinstance(entity, (types.UserEmpty, types.InputPeerEmpty)):
|
||||||
return InputUserEmpty()
|
return types.InputUserEmpty()
|
||||||
|
|
||||||
if isinstance(entity, UserFull):
|
if isinstance(entity, types.UserFull):
|
||||||
return get_input_user(entity.user)
|
return get_input_user(entity.user)
|
||||||
|
|
||||||
if isinstance(entity, InputPeerUser):
|
if isinstance(entity, types.InputPeerUser):
|
||||||
return InputUser(entity.user_id, entity.access_hash)
|
return types.InputUser(entity.user_id, entity.access_hash)
|
||||||
|
|
||||||
_raise_cast_fail(entity, 'InputUser')
|
_raise_cast_fail(entity, 'InputUser')
|
||||||
|
|
||||||
|
@ -228,12 +209,12 @@ def get_input_dialog(dialog):
|
||||||
if dialog.SUBCLASS_OF_ID == 0xa21c9795: # crc32(b'InputDialogPeer')
|
if dialog.SUBCLASS_OF_ID == 0xa21c9795: # crc32(b'InputDialogPeer')
|
||||||
return dialog
|
return dialog
|
||||||
if dialog.SUBCLASS_OF_ID == 0xc91c90b6: # crc32(b'InputPeer')
|
if dialog.SUBCLASS_OF_ID == 0xc91c90b6: # crc32(b'InputPeer')
|
||||||
return InputDialogPeer(dialog)
|
return types.InputDialogPeer(dialog)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(dialog, 'InputDialogPeer')
|
_raise_cast_fail(dialog, 'InputDialogPeer')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return InputDialogPeer(get_input_peer(dialog))
|
return types.InputDialogPeer(get_input_peer(dialog))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -248,13 +229,13 @@ def get_input_document(document):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(document, 'InputDocument')
|
_raise_cast_fail(document, 'InputDocument')
|
||||||
|
|
||||||
if isinstance(document, Document):
|
if isinstance(document, types.Document):
|
||||||
return InputDocument(id=document.id, access_hash=document.access_hash)
|
return types.InputDocument(id=document.id, access_hash=document.access_hash)
|
||||||
|
|
||||||
if isinstance(document, DocumentEmpty):
|
if isinstance(document, types.DocumentEmpty):
|
||||||
return InputDocumentEmpty()
|
return types.InputDocumentEmpty()
|
||||||
|
|
||||||
if isinstance(document, MessageMediaDocument):
|
if isinstance(document, types.MessageMediaDocument):
|
||||||
return get_input_document(document.document)
|
return get_input_document(document.document)
|
||||||
|
|
||||||
if isinstance(document, types.Message):
|
if isinstance(document, types.Message):
|
||||||
|
@ -271,14 +252,14 @@ def get_input_photo(photo):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(photo, 'InputPhoto')
|
_raise_cast_fail(photo, 'InputPhoto')
|
||||||
|
|
||||||
if isinstance(photo, photos.Photo):
|
if isinstance(photo, types.photos.Photo):
|
||||||
photo = photo.photo
|
photo = photo.photo
|
||||||
|
|
||||||
if isinstance(photo, Photo):
|
if isinstance(photo, types.Photo):
|
||||||
return InputPhoto(id=photo.id, access_hash=photo.access_hash)
|
return types.InputPhoto(id=photo.id, access_hash=photo.access_hash)
|
||||||
|
|
||||||
if isinstance(photo, PhotoEmpty):
|
if isinstance(photo, types.PhotoEmpty):
|
||||||
return InputPhotoEmpty()
|
return types.InputPhotoEmpty()
|
||||||
|
|
||||||
_raise_cast_fail(photo, 'InputPhoto')
|
_raise_cast_fail(photo, 'InputPhoto')
|
||||||
|
|
||||||
|
@ -291,13 +272,13 @@ def get_input_geo(geo):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(geo, 'InputGeoPoint')
|
_raise_cast_fail(geo, 'InputGeoPoint')
|
||||||
|
|
||||||
if isinstance(geo, GeoPoint):
|
if isinstance(geo, types.GeoPoint):
|
||||||
return InputGeoPoint(lat=geo.lat, long=geo.long)
|
return types.InputGeoPoint(lat=geo.lat, long=geo.long)
|
||||||
|
|
||||||
if isinstance(geo, GeoPointEmpty):
|
if isinstance(geo, types.GeoPointEmpty):
|
||||||
return InputGeoPointEmpty()
|
return types.InputGeoPointEmpty()
|
||||||
|
|
||||||
if isinstance(geo, MessageMediaGeo):
|
if isinstance(geo, types.MessageMediaGeo):
|
||||||
return get_input_geo(geo.geo)
|
return get_input_geo(geo.geo)
|
||||||
|
|
||||||
if isinstance(geo, types.Message):
|
if isinstance(geo, types.Message):
|
||||||
|
@ -317,67 +298,67 @@ def get_input_media(media, is_photo=False):
|
||||||
if media.SUBCLASS_OF_ID == 0xfaf846f4: # crc32(b'InputMedia')
|
if media.SUBCLASS_OF_ID == 0xfaf846f4: # crc32(b'InputMedia')
|
||||||
return media
|
return media
|
||||||
elif media.SUBCLASS_OF_ID == 0x846363e0: # crc32(b'InputPhoto')
|
elif media.SUBCLASS_OF_ID == 0x846363e0: # crc32(b'InputPhoto')
|
||||||
return InputMediaPhoto(media)
|
return types.InputMediaPhoto(media)
|
||||||
elif media.SUBCLASS_OF_ID == 0xf33fdb68: # crc32(b'InputDocument')
|
elif media.SUBCLASS_OF_ID == 0xf33fdb68: # crc32(b'InputDocument')
|
||||||
return InputMediaDocument(media)
|
return types.InputMediaDocument(media)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_raise_cast_fail(media, 'InputMedia')
|
_raise_cast_fail(media, 'InputMedia')
|
||||||
|
|
||||||
if isinstance(media, MessageMediaPhoto):
|
if isinstance(media, types.MessageMediaPhoto):
|
||||||
return InputMediaPhoto(
|
return types.InputMediaPhoto(
|
||||||
id=get_input_photo(media.photo),
|
id=get_input_photo(media.photo),
|
||||||
ttl_seconds=media.ttl_seconds
|
ttl_seconds=media.ttl_seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, (Photo, photos.Photo, PhotoEmpty)):
|
if isinstance(media, (types.Photo, types.photos.Photo, types.PhotoEmpty)):
|
||||||
return InputMediaPhoto(
|
return types.InputMediaPhoto(
|
||||||
id=get_input_photo(media)
|
id=get_input_photo(media)
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, MessageMediaDocument):
|
if isinstance(media, types.MessageMediaDocument):
|
||||||
return InputMediaDocument(
|
return types.InputMediaDocument(
|
||||||
id=get_input_document(media.document),
|
id=get_input_document(media.document),
|
||||||
ttl_seconds=media.ttl_seconds
|
ttl_seconds=media.ttl_seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, (Document, DocumentEmpty)):
|
if isinstance(media, (types.Document, types.DocumentEmpty)):
|
||||||
return InputMediaDocument(
|
return types.InputMediaDocument(
|
||||||
id=get_input_document(media)
|
id=get_input_document(media)
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, FileLocation):
|
if isinstance(media, types.FileLocation):
|
||||||
if is_photo:
|
if is_photo:
|
||||||
return InputMediaUploadedPhoto(file=media)
|
return types.InputMediaUploadedPhoto(file=media)
|
||||||
else:
|
else:
|
||||||
return InputMediaUploadedDocument(
|
return types.InputMediaUploadedDocument(
|
||||||
file=media,
|
file=media,
|
||||||
mime_type='application/octet-stream', # unknown, assume bytes
|
mime_type='application/octet-stream', # unknown, assume bytes
|
||||||
attributes=[DocumentAttributeFilename('unnamed')]
|
attributes=[types.DocumentAttributeFilename('unnamed')]
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, MessageMediaGame):
|
if isinstance(media, types.MessageMediaGame):
|
||||||
return InputMediaGame(id=media.game.id)
|
return types.InputMediaGame(id=media.game.id)
|
||||||
|
|
||||||
if isinstance(media, (ChatPhoto, UserProfilePhoto)):
|
if isinstance(media, (types.ChatPhoto, types.UserProfilePhoto)):
|
||||||
if isinstance(media.photo_big, FileLocationUnavailable):
|
if isinstance(media.photo_big, types.FileLocationUnavailable):
|
||||||
media = media.photo_small
|
media = media.photo_small
|
||||||
else:
|
else:
|
||||||
media = media.photo_big
|
media = media.photo_big
|
||||||
return get_input_media(media, is_photo=True)
|
return get_input_media(media, is_photo=True)
|
||||||
|
|
||||||
if isinstance(media, MessageMediaContact):
|
if isinstance(media, types.MessageMediaContact):
|
||||||
return InputMediaContact(
|
return types.InputMediaContact(
|
||||||
phone_number=media.phone_number,
|
phone_number=media.phone_number,
|
||||||
first_name=media.first_name,
|
first_name=media.first_name,
|
||||||
last_name=media.last_name,
|
last_name=media.last_name,
|
||||||
vcard=''
|
vcard=''
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, MessageMediaGeo):
|
if isinstance(media, types.MessageMediaGeo):
|
||||||
return InputMediaGeoPoint(geo_point=get_input_geo(media.geo))
|
return types.InputMediaGeoPoint(geo_point=get_input_geo(media.geo))
|
||||||
|
|
||||||
if isinstance(media, MessageMediaVenue):
|
if isinstance(media, types.MessageMediaVenue):
|
||||||
return InputMediaVenue(
|
return types.InputMediaVenue(
|
||||||
geo_point=get_input_geo(media.geo),
|
geo_point=get_input_geo(media.geo),
|
||||||
title=media.title,
|
title=media.title,
|
||||||
address=media.address,
|
address=media.address,
|
||||||
|
@ -387,9 +368,10 @@ def get_input_media(media, is_photo=False):
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(media, (
|
if isinstance(media, (
|
||||||
MessageMediaEmpty, MessageMediaUnsupported,
|
types.MessageMediaEmpty, types.MessageMediaUnsupported,
|
||||||
ChatPhotoEmpty, UserProfilePhotoEmpty, FileLocationUnavailable)):
|
types.ChatPhotoEmpty, types.UserProfilePhotoEmpty,
|
||||||
return InputMediaEmpty()
|
types.FileLocationUnavailable)):
|
||||||
|
return types.InputMediaEmpty()
|
||||||
|
|
||||||
if isinstance(media, types.Message):
|
if isinstance(media, types.Message):
|
||||||
return get_input_media(media.media, is_photo=is_photo)
|
return get_input_media(media.media, is_photo=is_photo)
|
||||||
|
@ -401,11 +383,11 @@ def get_input_message(message):
|
||||||
"""Similar to :meth:`get_input_peer`, but for input messages."""
|
"""Similar to :meth:`get_input_peer`, but for input messages."""
|
||||||
try:
|
try:
|
||||||
if isinstance(message, int): # This case is really common too
|
if isinstance(message, int): # This case is really common too
|
||||||
return InputMessageID(message)
|
return types.InputMessageID(message)
|
||||||
elif message.SUBCLASS_OF_ID == 0x54b6bcc5: # crc32(b'InputMessage'):
|
elif message.SUBCLASS_OF_ID == 0x54b6bcc5: # crc32(b'InputMessage'):
|
||||||
return message
|
return message
|
||||||
elif message.SUBCLASS_OF_ID == 0x790009e3: # crc32(b'Message'):
|
elif message.SUBCLASS_OF_ID == 0x790009e3: # crc32(b'Message'):
|
||||||
return InputMessageID(message.id)
|
return types.InputMessageID(message.id)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -445,14 +427,14 @@ def get_attributes(file, *, attributes=None, mime_type=None,
|
||||||
if mime_type is None:
|
if mime_type is None:
|
||||||
mime_type = mimetypes.guess_type(file)[0]
|
mime_type = mimetypes.guess_type(file)[0]
|
||||||
|
|
||||||
attr_dict = {DocumentAttributeFilename:
|
attr_dict = {types.DocumentAttributeFilename:
|
||||||
DocumentAttributeFilename(os.path.basename(file))}
|
types.DocumentAttributeFilename(os.path.basename(file))}
|
||||||
|
|
||||||
if is_audio(file) and hachoir is not None:
|
if is_audio(file) and hachoir is not None:
|
||||||
with hachoir.parser.createParser(file) as parser:
|
with hachoir.parser.createParser(file) as parser:
|
||||||
m = hachoir.metadata.extractMetadata(parser)
|
m = hachoir.metadata.extractMetadata(parser)
|
||||||
attr_dict[DocumentAttributeAudio] = \
|
attr_dict[types.DocumentAttributeAudio] = \
|
||||||
DocumentAttributeAudio(
|
types.DocumentAttributeAudio(
|
||||||
voice=voice_note,
|
voice=voice_note,
|
||||||
title=m.get('title') if m.has('title') else None,
|
title=m.get('title') if m.has('title') else None,
|
||||||
performer=m.get('author') if m.has('author') else None,
|
performer=m.get('author') if m.has('author') else None,
|
||||||
|
@ -464,7 +446,7 @@ def get_attributes(file, *, attributes=None, mime_type=None,
|
||||||
if hachoir:
|
if hachoir:
|
||||||
with hachoir.parser.createParser(file) as parser:
|
with hachoir.parser.createParser(file) as parser:
|
||||||
m = hachoir.metadata.extractMetadata(parser)
|
m = hachoir.metadata.extractMetadata(parser)
|
||||||
doc = DocumentAttributeVideo(
|
doc = types.DocumentAttributeVideo(
|
||||||
round_message=video_note,
|
round_message=video_note,
|
||||||
w=m.get('width') if m.has('width') else 0,
|
w=m.get('width') if m.has('width') else 0,
|
||||||
h=m.get('height') if m.has('height') else 0,
|
h=m.get('height') if m.has('height') else 0,
|
||||||
|
@ -472,21 +454,21 @@ def get_attributes(file, *, attributes=None, mime_type=None,
|
||||||
if m.has('duration') else 0)
|
if m.has('duration') else 0)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
doc = DocumentAttributeVideo(
|
doc = types.DocumentAttributeVideo(
|
||||||
0, 1, 1, round_message=video_note)
|
0, 1, 1, round_message=video_note)
|
||||||
|
|
||||||
attr_dict[DocumentAttributeVideo] = doc
|
attr_dict[types.DocumentAttributeVideo] = doc
|
||||||
else:
|
else:
|
||||||
attr_dict = {DocumentAttributeFilename:
|
attr_dict = {types.DocumentAttributeFilename:
|
||||||
DocumentAttributeFilename(
|
types.DocumentAttributeFilename(
|
||||||
os.path.basename(getattr(file, 'name', None) or 'unnamed'))}
|
os.path.basename(getattr(file, 'name', None) or 'unnamed'))}
|
||||||
|
|
||||||
if voice_note:
|
if voice_note:
|
||||||
if DocumentAttributeAudio in attr_dict:
|
if types.DocumentAttributeAudio in attr_dict:
|
||||||
attr_dict[DocumentAttributeAudio].voice = True
|
attr_dict[types.DocumentAttributeAudio].voice = True
|
||||||
else:
|
else:
|
||||||
attr_dict[DocumentAttributeAudio] = \
|
attr_dict[types.DocumentAttributeAudio] = \
|
||||||
DocumentAttributeAudio(0, voice=True)
|
types.DocumentAttributeAudio(0, voice=True)
|
||||||
|
|
||||||
# Now override the attributes if any. As we have a dict of
|
# Now override the attributes if any. As we have a dict of
|
||||||
# {cls: instance}, we can override any class with the list
|
# {cls: instance}, we can override any class with the list
|
||||||
|
@ -553,23 +535,26 @@ def get_input_location(location):
|
||||||
if isinstance(location, types.Message):
|
if isinstance(location, types.Message):
|
||||||
location = location.media
|
location = location.media
|
||||||
|
|
||||||
if isinstance(location, MessageMediaDocument):
|
if isinstance(location, types.MessageMediaDocument):
|
||||||
location = location.document
|
location = location.document
|
||||||
elif isinstance(location, MessageMediaPhoto):
|
elif isinstance(location, types.MessageMediaPhoto):
|
||||||
location = location.photo
|
location = location.photo
|
||||||
|
|
||||||
if isinstance(location, Document):
|
if isinstance(location, types.Document):
|
||||||
return (location.dc_id, InputDocumentFileLocation(
|
return (location.dc_id, types.InputDocumentFileLocation(
|
||||||
location.id, location.access_hash, location.version))
|
location.id, location.access_hash, location.version))
|
||||||
elif isinstance(location, Photo):
|
elif isinstance(location, types.Photo):
|
||||||
try:
|
try:
|
||||||
location = next(x for x in reversed(location.sizes)
|
location = next(
|
||||||
if not isinstance(x, PhotoSizeEmpty)).location
|
x for x in reversed(location.sizes)
|
||||||
|
if not isinstance(x, types.PhotoSizeEmpty)
|
||||||
|
).location
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if isinstance(location, (FileLocation, FileLocationUnavailable)):
|
if isinstance(location, (
|
||||||
return (getattr(location, 'dc_id', None), InputFileLocation(
|
types.FileLocation, types.FileLocationUnavailable)):
|
||||||
|
return (getattr(location, 'dc_id', None), types.InputFileLocation(
|
||||||
location.volume_id, location.local_id, location.secret))
|
location.volume_id, location.local_id, location.secret))
|
||||||
|
|
||||||
_raise_cast_fail(location, 'InputFileLocation')
|
_raise_cast_fail(location, 'InputFileLocation')
|
||||||
|
@ -694,7 +679,8 @@ def get_peer_id(peer, add_mark=True):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if peer.SUBCLASS_OF_ID not in (0x2d45687, 0xc91c90b6):
|
if peer.SUBCLASS_OF_ID not in (0x2d45687, 0xc91c90b6):
|
||||||
if isinstance(peer, (ResolvedPeer, InputNotifyPeer, TopPeer)):
|
if isinstance(peer, (
|
||||||
|
types.ResolvedPeer, types.InputNotifyPeer, types.TopPeer)):
|
||||||
peer = peer.peer
|
peer = peer.peer
|
||||||
else:
|
else:
|
||||||
# Not a Peer or an InputPeer, so first get its Input version
|
# Not a Peer or an InputPeer, so first get its Input version
|
||||||
|
@ -703,16 +689,17 @@ def get_peer_id(peer, add_mark=True):
|
||||||
_raise_cast_fail(peer, 'int')
|
_raise_cast_fail(peer, 'int')
|
||||||
|
|
||||||
# Set the right ID/kind, or raise if the TLObject is not recognised
|
# Set the right ID/kind, or raise if the TLObject is not recognised
|
||||||
if isinstance(peer, (PeerUser, InputPeerUser)):
|
if isinstance(peer, (types.PeerUser, types.InputPeerUser)):
|
||||||
return peer.user_id
|
return peer.user_id
|
||||||
elif isinstance(peer, (PeerChat, InputPeerChat)):
|
elif isinstance(peer, (types.PeerChat, types.InputPeerChat)):
|
||||||
# Check in case the user mixed things up to avoid blowing up
|
# Check in case the user mixed things up to avoid blowing up
|
||||||
if not (0 < peer.chat_id <= 0x7fffffff):
|
if not (0 < peer.chat_id <= 0x7fffffff):
|
||||||
peer.chat_id = resolve_id(peer.chat_id)[0]
|
peer.chat_id = resolve_id(peer.chat_id)[0]
|
||||||
|
|
||||||
return -peer.chat_id if add_mark else peer.chat_id
|
return -peer.chat_id if add_mark else peer.chat_id
|
||||||
elif isinstance(peer, (PeerChannel, InputPeerChannel, ChannelFull)):
|
elif isinstance(peer, (
|
||||||
if isinstance(peer, ChannelFull):
|
types.PeerChannel, types.InputPeerChannel, types.ChannelFull)):
|
||||||
|
if isinstance(peer, types.ChannelFull):
|
||||||
# Special case: .get_input_peer can't return InputChannel from
|
# Special case: .get_input_peer can't return InputChannel from
|
||||||
# ChannelFull since it doesn't have an .access_hash attribute.
|
# ChannelFull since it doesn't have an .access_hash attribute.
|
||||||
i = peer.id
|
i = peer.id
|
||||||
|
@ -722,7 +709,7 @@ def get_peer_id(peer, add_mark=True):
|
||||||
# Check in case the user mixed things up to avoid blowing up
|
# Check in case the user mixed things up to avoid blowing up
|
||||||
if not (0 < i <= 0x7fffffff):
|
if not (0 < i <= 0x7fffffff):
|
||||||
i = resolve_id(i)[0]
|
i = resolve_id(i)[0]
|
||||||
if isinstance(peer, ChannelFull):
|
if isinstance(peer, types.ChannelFull):
|
||||||
peer.id = i
|
peer.id = i
|
||||||
else:
|
else:
|
||||||
peer.channel_id = i
|
peer.channel_id = i
|
||||||
|
@ -740,7 +727,7 @@ def get_peer_id(peer, add_mark=True):
|
||||||
def resolve_id(marked_id):
|
def resolve_id(marked_id):
|
||||||
"""Given a marked ID, returns the original ID and its :tl:`Peer` type."""
|
"""Given a marked ID, returns the original ID and its :tl:`Peer` type."""
|
||||||
if marked_id >= 0:
|
if marked_id >= 0:
|
||||||
return marked_id, PeerUser
|
return marked_id, types.PeerUser
|
||||||
|
|
||||||
# There have been report of chat IDs being 10000xyz, which means their
|
# There have been report of chat IDs being 10000xyz, which means their
|
||||||
# marked version is -10000xyz, which in turn looks like a channel but
|
# marked version is -10000xyz, which in turn looks like a channel but
|
||||||
|
@ -748,9 +735,9 @@ def resolve_id(marked_id):
|
||||||
# two zeroes.
|
# two zeroes.
|
||||||
m = re.match(r'-100([^0]\d*)', str(marked_id))
|
m = re.match(r'-100([^0]\d*)', str(marked_id))
|
||||||
if m:
|
if m:
|
||||||
return int(m.group(1)), PeerChannel
|
return int(m.group(1)), types.PeerChannel
|
||||||
|
|
||||||
return -marked_id, PeerChat
|
return -marked_id, types.PeerChat
|
||||||
|
|
||||||
|
|
||||||
def get_appropriated_part_size(file_size):
|
def get_appropriated_part_size(file_size):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user