From 6ab1c87050b990fee7250486c5fe190fbcd161ca Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 16 Dec 2018 15:24:51 +0100 Subject: [PATCH] Add PyrogramType --- .../types/messages_and_media/animation.py | 6 +-- .../client/types/messages_and_media/audio.py | 6 +-- .../types/messages_and_media/contact.py | 6 +-- .../types/messages_and_media/document.py | 6 +-- .../types/messages_and_media/location.py | 6 +-- .../types/messages_and_media/message.py | 7 ++- .../messages_and_media/message_entity.py | 6 +-- .../types/messages_and_media/messages.py | 6 +-- .../client/types/messages_and_media/photo.py | 6 +-- .../types/messages_and_media/photo_size.py | 6 +-- .../types/messages_and_media/sticker.py | 5 +-- .../messages_and_media/user_profile_photos.py | 6 +-- .../client/types/messages_and_media/venue.py | 6 +-- .../client/types/messages_and_media/video.py | 6 +-- .../types/messages_and_media/video_note.py | 6 +-- .../client/types/messages_and_media/voice.py | 11 +++-- pyrogram/client/types/pyrogram_type.py | 44 +++++++++++++++++++ pyrogram/client/types/user_and_chats/chat.py | 6 +-- .../client/types/user_and_chats/chat_photo.py | 2 +- pyrogram/client/types/user_and_chats/user.py | 8 ++-- .../types/user_and_chats/user_status.py | 6 +-- 21 files changed, 88 insertions(+), 79 deletions(-) create mode 100644 pyrogram/client/types/pyrogram_type.py diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index 0e36d5cf..7447f4ab 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Animation(Object): +class Animation(PyrogramType): """This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound). Args: @@ -56,8 +56,6 @@ class Animation(Object): Date the animation was sent in Unix time. """ - ID = 0xb0700025 - def __init__(self, file_id: str, width: int, height: int, duration: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 3f2b3c20..2cd3a1bd 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Audio(Object): +class Audio(PyrogramType): """This object represents an audio file to be treated as music by the Telegram clients. Args: @@ -56,8 +56,6 @@ class Audio(Object): Title of the audio as defined by sender or by audio tags. """ - ID = 0xb0700006 - def __init__(self, file_id: str, duration: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, performer: str = None, title: str = None, diff --git a/pyrogram/client/types/messages_and_media/contact.py b/pyrogram/client/types/messages_and_media/contact.py index 2c686a5f..2a3f8a21 100644 --- a/pyrogram/client/types/messages_and_media/contact.py +++ b/pyrogram/client/types/messages_and_media/contact.py @@ -17,10 +17,10 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Contact(Object): +class Contact(PyrogramType): """This object represents a phone contact. Args: @@ -40,8 +40,6 @@ class Contact(Object): Additional data about the contact in the form of a vCard. """ - ID = 0xb0700011 - def __init__(self, phone_number: str, first_name: str, *, last_name: str = None, user_id: int = None, vcard: str = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 5aa047cf..412954ef 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Document(Object): +class Document(PyrogramType): """This object represents a general file (as opposed to photos, voice messages, audio files, ...). Args: @@ -47,8 +47,6 @@ class Document(Object): Date the document was sent in Unix time. """ - ID = 0xb0700007 - def __init__(self, file_id: str, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/location.py b/pyrogram/client/types/messages_and_media/location.py index 3ab9bfa7..1276a783 100644 --- a/pyrogram/client/types/messages_and_media/location.py +++ b/pyrogram/client/types/messages_and_media/location.py @@ -17,10 +17,10 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Location(Object): +class Location(PyrogramType): """This object represents a point on the map. Args: @@ -31,8 +31,6 @@ class Location(Object): Latitude as defined by sender. """ - ID = 0xb0700012 - def __init__(self, longitude: float, latitude: float, *, client=None, raw=None): self.longitude = longitude diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 0829c2db..8d84a513 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -18,18 +18,18 @@ import pyrogram from pyrogram.api import types, functions -from pyrogram.api.core import Object from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid from .contact import Contact from .location import Location from .message_entity import MessageEntity from ..messages_and_media.photo import Photo +from ..pyrogram_type import PyrogramType from ..user_and_chats.chat import Chat from ..user_and_chats.user import User from ...ext.utils import Str -class Message(Object): +class Message(PyrogramType): """This object represents a message. Args: @@ -224,7 +224,6 @@ class Message(Object): """ # TODO: Add game missing field. Also invoice, successful_payment, connected_website - ID = 0xb0700003 def __init__(self, message_id: int, *, date: int = None, chat=None, from_user=None, forward_from=None, forward_from_chat=None, @@ -461,7 +460,7 @@ class Message(Object): set_name = None sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes, - set_name, sticker_attribute, file_name) + set_name, sticker_attribute, file_name) else: document = pyrogram.Document.parse(client, doc, file_name) elif isinstance(media, types.MessageMediaWebPage): diff --git a/pyrogram/client/types/messages_and_media/message_entity.py b/pyrogram/client/types/messages_and_media/message_entity.py index 9752bd28..67f32806 100644 --- a/pyrogram/client/types/messages_and_media/message_entity.py +++ b/pyrogram/client/types/messages_and_media/message_entity.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType from ..user_and_chats.user import User -class MessageEntity(Object): +class MessageEntity(PyrogramType): """This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc. @@ -45,8 +45,6 @@ class MessageEntity(Object): For "text_mention" only, the mentioned user. """ - ID = 0xb0700004 - ENTITIES = { types.MessageEntityMention.ID: "mention", types.MessageEntityHashtag.ID: "hashtag", diff --git a/pyrogram/client/types/messages_and_media/messages.py b/pyrogram/client/types/messages_and_media/messages.py index 7a2546a9..ef32f0b2 100644 --- a/pyrogram/client/types/messages_and_media/messages.py +++ b/pyrogram/client/types/messages_and_media/messages.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class Messages(Object): +class Messages(PyrogramType): """This object represents a chat's messages. Args: @@ -30,8 +30,6 @@ class Messages(Object): Requested messages. """ - ID = 0xb0700026 - def __init__(self, total_count: int, messages: list): self.total_count = total_count self.messages = messages diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index b1021d93..970579af 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -20,12 +20,12 @@ from base64 import b64encode from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Photo(Object): +class Photo(PyrogramType): """This object represents a Photo. Args: @@ -39,8 +39,6 @@ class Photo(Object): Available sizes of this photo. """ - ID = 0xb0700027 - def __init__(self, id: str, date: int, sizes: list, *, client=None, raw=None): self.id = id diff --git a/pyrogram/client/types/messages_and_media/photo_size.py b/pyrogram/client/types/messages_and_media/photo_size.py index 39b9f756..cb94a6e6 100644 --- a/pyrogram/client/types/messages_and_media/photo_size.py +++ b/pyrogram/client/types/messages_and_media/photo_size.py @@ -19,11 +19,11 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from pyrogram.client.ext.utils import encode +from ..pyrogram_type import PyrogramType -class PhotoSize(Object): +class PhotoSize(PyrogramType): """This object represents one size of a photo or a file/sticker thumbnail. Args: @@ -40,8 +40,6 @@ class PhotoSize(Object): File size. """ - ID = 0xb0700005 - def __init__(self, file_id: str, width: int, height: int, file_size: int, *, client=None, raw=None): self.file_id = file_id diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index a3dda2d5..7b5bfa70 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Sticker(Object): +class Sticker(PyrogramType): """This object represents a sticker. Args: @@ -60,7 +60,6 @@ class Sticker(Object): """ # TODO: Add mask position - ID = 0xb0700017 def __init__(self, file_id: str, width: int, height: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, diff --git a/pyrogram/client/types/messages_and_media/user_profile_photos.py b/pyrogram/client/types/messages_and_media/user_profile_photos.py index c8ca9e39..bf53b13e 100644 --- a/pyrogram/client/types/messages_and_media/user_profile_photos.py +++ b/pyrogram/client/types/messages_and_media/user_profile_photos.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class UserProfilePhotos(Object): +class UserProfilePhotos(PyrogramType): """This object represents a user's profile pictures. Args: @@ -30,8 +30,6 @@ class UserProfilePhotos(Object): Requested profile pictures. """ - ID = 0xb0700014 - def __init__(self, total_count: int, photos: list): self.total_count = total_count self.photos = photos diff --git a/pyrogram/client/types/messages_and_media/venue.py b/pyrogram/client/types/messages_and_media/venue.py index 8225c36b..662a416f 100644 --- a/pyrogram/client/types/messages_and_media/venue.py +++ b/pyrogram/client/types/messages_and_media/venue.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object from .location import Location +from ..pyrogram_type import PyrogramType -class Venue(Object): +class Venue(PyrogramType): """This object represents a venue. Args: @@ -43,8 +43,6 @@ class Venue(Object): """ - ID = 0xb0700013 - def __init__(self, location, title: str, address: str, *, foursquare_id: str = None, foursquare_type: str = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index 4873730c..05d28f45 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class Video(Object): +class Video(PyrogramType): """This object represents a video file. Args: @@ -56,8 +56,6 @@ class Video(Object): Date the video was sent in Unix time. """ - ID = 0xb0700008 - def __init__(self, file_id: str, width: int, height: int, duration: int, *, thumb=None, file_name: str = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index 0b07c00b..ddcd74f3 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -19,12 +19,12 @@ from struct import pack from pyrogram.api import types -from pyrogram.api.core import Object from .photo_size import PhotoSize +from ..pyrogram_type import PyrogramType from ...ext.utils import encode -class VideoNote(Object): +class VideoNote(PyrogramType): """This object represents a video message (available in Telegram apps as of v.4.0). Args: @@ -50,8 +50,6 @@ class VideoNote(Object): Date the video note was sent in Unix time. """ - ID = 0xb0700010 - def __init__(self, file_id: str, length: int, duration: int, *, thumb=None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index da9d6d51..c993ea5c 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -16,13 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api import types -from pyrogram.api.core import Object -from ...ext.utils import encode from struct import pack +from pyrogram.api import types +from ..pyrogram_type import PyrogramType +from ...ext.utils import encode -class Voice(Object): + +class Voice(PyrogramType): """This object represents a voice note. Args: @@ -45,8 +46,6 @@ class Voice(Object): Date the voice was sent in Unix time. """ - ID = 0xb0700009 - def __init__(self, file_id: str, duration: int, *, waveform: bytes = None, mime_type: str = None, file_size: int = None, date: int = None, client=None, raw=None): diff --git a/pyrogram/client/types/pyrogram_type.py b/pyrogram/client/types/pyrogram_type.py new file mode 100644 index 00000000..20b476a2 --- /dev/null +++ b/pyrogram/client/types/pyrogram_type.py @@ -0,0 +1,44 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from collections import OrderedDict +from json import dumps, JSONEncoder + + +class PyrogramType: + def __str__(self): + return dumps(self, cls=Encoder, indent=4) + + +def remove_none(obj): + if isinstance(obj, (list, tuple, set)): + return type(obj)(remove_none(x) for x in obj if x is not None) + elif isinstance(obj, dict): + return type(obj)((remove_none(k), remove_none(v)) for k, v in obj.items() if k is not None and v is not None) + else: + return obj + + +class Encoder(JSONEncoder): + def default(self, o: PyrogramType): + content = {i: getattr(o, i) for i in filter(lambda x: not x.startswith("_"), o.__dict__)} + + return OrderedDict( + [("_", "pyrogram:{}".format(o.__class__.__name__))] + + [i for i in remove_none(content).items()] + ) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 75aa5eac..6e1c675e 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object from .chat_photo import ChatPhoto +from ..pyrogram_type import PyrogramType -class Chat(Object): +class Chat(PyrogramType): """This object represents a chat. Args: @@ -76,8 +76,6 @@ class Chat(Object): The reason why this chat might be unavailable to some users. """ - ID = 0xb0700002 - def __init__(self, id: int, type: str, *, title: str = None, username: str = None, first_name: str = None, last_name: str = None, all_members_are_administrators: bool = None, photo=None, description: str = None, diff --git a/pyrogram/client/types/user_and_chats/chat_photo.py b/pyrogram/client/types/user_and_chats/chat_photo.py index 485777e5..c7ef4c80 100644 --- a/pyrogram/client/types/user_and_chats/chat_photo.py +++ b/pyrogram/client/types/user_and_chats/chat_photo.py @@ -42,7 +42,7 @@ class ChatPhoto(Object): self.big_file_id = big_file_id self._client = client - self.raw = raw + self._raw = raw @staticmethod def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto): diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index e460522b..80660bcc 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -17,12 +17,12 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object -from .user_status import UserStatus from .chat_photo import ChatPhoto +from .user_status import UserStatus +from ..pyrogram_type import PyrogramType -class User(Object): +class User(PyrogramType): """This object represents a Telegram user or bot. Args: @@ -69,8 +69,6 @@ class User(Object): The reason why this bot might be unavailable to some users. """ - ID = 0xb0700001 - def __init__(self, id: int, is_self: bool, is_contact: bool, is_mutual_contact: bool, is_deleted: bool, is_bot: bool, first_name: str, *, last_name: str = None, status=None, username: str = None, language_code: str = None, diff --git a/pyrogram/client/types/user_and_chats/user_status.py b/pyrogram/client/types/user_and_chats/user_status.py index 651d8db6..f99d356d 100644 --- a/pyrogram/client/types/user_and_chats/user_status.py +++ b/pyrogram/client/types/user_and_chats/user_status.py @@ -17,10 +17,10 @@ # along with Pyrogram. If not, see . from pyrogram.api import types -from pyrogram.api.core import Object +from ..pyrogram_type import PyrogramType -class UserStatus(Object): +class UserStatus(PyrogramType): """This object represents a User status (Last Seen privacy). .. note:: @@ -62,8 +62,6 @@ class UserStatus(Object): always shown to blocked users), None otherwise. """ - ID = 0xb0700031 - def __init__(self, user_id: int, *, online: bool = None, offline: bool = None, date: int = None, recently: bool = None, within_week: bool = None, within_month: bool = None, long_time_ago: bool = None,