From 17a25898912a484ade13e227d46c2037421caf66 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Mon, 1 Jul 2019 01:28:29 -0400 Subject: [PATCH 1/6] Add message.web_page attributes --- .../types/messages_and_media/__init__.py | 3 +- .../types/messages_and_media/message.py | 6 +- .../types/messages_and_media/webpage.py | 191 ++++++++++++++++++ 3 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 pyrogram/client/types/messages_and_media/webpage.py diff --git a/pyrogram/client/types/messages_and_media/__init__.py b/pyrogram/client/types/messages_and_media/__init__.py index b9bcb460..f5db82e5 100644 --- a/pyrogram/client/types/messages_and_media/__init__.py +++ b/pyrogram/client/types/messages_and_media/__init__.py @@ -34,8 +34,9 @@ from .venue import Venue from .video import Video from .video_note import VideoNote from .voice import Voice +from .webpage import WebPage __all__ = [ "Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail", - "StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice" + "StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage" ] diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 39fd24fa..37b41082 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -575,10 +575,12 @@ class Message(Object, Update): else: document = pyrogram.Document._parse(client, doc, file_name) elif isinstance(media, types.MessageMediaWebPage): - web_page = True - media = None + if isinstance(media.webpage, types.WebPage): + web_page = pyrogram.WebPage._parse(client, media.webpage) + elif isinstance(media, types.MessageMediaPoll): poll = pyrogram.Poll._parse(client, media) + else: media = None diff --git a/pyrogram/client/types/messages_and_media/webpage.py b/pyrogram/client/types/messages_and_media/webpage.py new file mode 100644 index 00000000..c8a14b04 --- /dev/null +++ b/pyrogram/client/types/messages_and_media/webpage.py @@ -0,0 +1,191 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 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 . + +import pyrogram +from pyrogram.api import types +from .photo import Photo +from ..object import Object + + +class WebPage(Object): + #TODO: hash, cached_page + """A webpage preview + + Parameters: + id (``str``): + Unique identifier for this webpage. + + url (``str``): + Full URL for this webpage. + + display_url (``str``): + Display URL for this webpage. + + type (``str``, *optional*): + Type of webpage, can be `article`, `photo`, `gif`, `video` or `document` afaik. #TODO + + site_name (``str``, *optional*): + Site name. + + title (``str``, *optional*): + Title of this webpage. + + description (``str``, *optional*): + Description of this webpage. + + audio (:obj:`Audio`, *optional*): + Webpage is an audio file, information about the file. + + document (:obj:`Document`, *optional*): + Webpage is a general file, information about the file. + + photo (:obj:`Photo`, *optional*): + Webpage is a photo, information about the photo. + + animation (:obj:`Animation`, *optional*): + Webpage is an animation, information about the animation. + + video (:obj:`Video`, *optional*): + Webpage is a video, information about the video.abs + + embed_url (``str``, *optional*): + Embedded content URL. + + embed_type (``str``, *optional*): + Embedded content type, can be `iframe` #TODO + + embed_width (``int``, *optional*): + Embedded content width. + + embed_height (``int``, *optional*): + Embedded content height. + + duration (``int``, *optional*): + :shrug: + + author (``str``, *optional*): + Author of the webpage, eg the Twitter user. + """ + + __slots__ = [ + "id", "url", "display_url", "type", "site_name", "title", "description", + "audio", "document", "photo", "animation", "video", + "embed_url", "embed_type", "embed_width", "embed_height", "duration", "author" + ] + + def __init__( + self, + *, + client: "pyrogram.BaseClient" = None, + id: str, + url: str, + display_url: str, + type: str = None, + site_name: str = None, + title: str = None, + description: str = None, + audio: "pyrogram.Audio" = None, + document: "pyrogram.Document" = None, + photo: "pyrogram.Photo" = None, + animation: "pyrogram.Animation" = None, + video: "pyrogram.Video" = None, + embed_url: str = None, + embed_type: str = None, + embed_width: int = None, + embed_height: int = None, + duration: int = None, + author: str = None + ) -> "pyrogram.WebPage" : + super().__init__(client) + + self.id = id + self.url = url + self.display_url = display_url + self.type = type + self.site_name = site_name + self.title = title + self.description = description + self.audio = audio + self.document = document + self.photo = photo + self.animation = animation + self.video = video + self.embed_url = embed_url + self.embed_type = embed_type + self.embed_width = embed_width + self.embed_height = embed_height + self.duration = duration + self.author = author + + @staticmethod + def _parse(client, webpage: types.WebPage) -> "WebPage": + audio = None + document = None + photo = None + animation = None + video = None + + if isinstance(webpage.photo, types.Photo): + photo = pyrogram.Photo._parse(client, webpage.photo) + + doc = webpage.document + + if isinstance(doc, types.Document): + attributes = {type(i): i for i in doc.attributes} + + file_name = getattr( + attributes.get( + types.DocumentAttributeFilename, None + ), "file_name", None + ) + + if types.DocumentAttributeAudio in attributes: + audio_attributes = attributes[types.DocumentAttributeAudio] + audio = pyrogram.Audio._parse(client, doc, audio_attributes, file_name) + + elif types.DocumentAttributeAnimated in attributes: + video_attributes = attributes.get(types.DocumentAttributeVideo, None) + animation = pyrogram.Animation._parse(client, doc, video_attributes, file_name) + + elif types.DocumentAttributeVideo in attributes: + video_attributes = attributes[types.DocumentAttributeVideo] + video = pyrogram.Video._parse(client, doc, video_attributes, file_name) + + else: + document = pyrogram.Document._parse(client, doc, file_name) + + return WebPage( + id=str(webpage.id), + url=webpage.url, + display_url=webpage.display_url, + type=webpage.type, + site_name=webpage.site_name, + title=webpage.title, + description=webpage.description, + audio=audio, + document=document, + photo=photo, + animation=animation, + video=video, + embed_url=webpage.embed_url, + embed_type=webpage.embed_type, + embed_width=webpage.embed_width, + embed_height=webpage.embed_height, + duration=webpage.duration, + author=webpage.author + ) From 9687ef2820248951087482acd7be465ec35f1f80 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Tue, 2 Jul 2019 10:00:03 -0400 Subject: [PATCH 2/6] flake8 errors --- pyrogram/client/types/messages_and_media/webpage.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/webpage.py b/pyrogram/client/types/messages_and_media/webpage.py index c8a14b04..df19e7c8 100644 --- a/pyrogram/client/types/messages_and_media/webpage.py +++ b/pyrogram/client/types/messages_and_media/webpage.py @@ -18,12 +18,11 @@ import pyrogram from pyrogram.api import types -from .photo import Photo from ..object import Object class WebPage(Object): - #TODO: hash, cached_page + # TODO: hash, cached_page """A webpage preview Parameters: @@ -81,7 +80,7 @@ class WebPage(Object): author (``str``, *optional*): Author of the webpage, eg the Twitter user. """ - + __slots__ = [ "id", "url", "display_url", "type", "site_name", "title", "description", "audio", "document", "photo", "animation", "video", @@ -110,9 +109,9 @@ class WebPage(Object): embed_height: int = None, duration: int = None, author: str = None - ) -> "pyrogram.WebPage" : + ) -> "pyrogram.WebPage": super().__init__(client) - + self.id = id self.url = url self.display_url = display_url @@ -131,7 +130,7 @@ class WebPage(Object): self.embed_height = embed_height self.duration = duration self.author = author - + @staticmethod def _parse(client, webpage: types.WebPage) -> "WebPage": audio = None From d363a18e8450f0a7fbcaf6588d48bc949043066c Mon Sep 17 00:00:00 2001 From: Mendel E Date: Tue, 2 Jul 2019 10:46:08 -0400 Subject: [PATCH 3/6] Initial docs for WebPage --- docs/source/api/types.rst | 2 ++ .../types/messages_and_media/message.py | 5 +---- .../types/messages_and_media/webpage.py | 20 +++++++++---------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/source/api/types.rst b/docs/source/api/types.rst index f91e4f58..2147996d 100644 --- a/docs/source/api/types.rst +++ b/docs/source/api/types.rst @@ -54,6 +54,7 @@ Messages & Media - :class:`Venue` - :class:`Sticker` - :class:`Game` + - :class:`WebPage` - :class:`Poll` - :class:`PollOption` @@ -137,6 +138,7 @@ Details .. autoclass:: Venue() .. autoclass:: Sticker() .. autoclass:: Game() +.. autoclass:: WebPage() .. autoclass:: Poll() .. autoclass:: PollOption() diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 37b41082..323693c5 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -177,11 +177,8 @@ class Message(Object, Update): venue (:obj:`Venue`, *optional*): Message is a venue, information about the venue. - web_page (``bool``, *optional*): + web_page (:obj:`WebPage`, *optional*): Message was sent with a webpage preview. - **Note:** Support for web pages is still basic; a simple boolean is set in case the message contains a - web page preview. In future versions this property could turn into a full web page object that contains - more details. poll (:obj:`Poll`, *optional*): Message is a native poll, information about the poll. diff --git a/pyrogram/client/types/messages_and_media/webpage.py b/pyrogram/client/types/messages_and_media/webpage.py index df19e7c8..7785e754 100644 --- a/pyrogram/client/types/messages_and_media/webpage.py +++ b/pyrogram/client/types/messages_and_media/webpage.py @@ -36,10 +36,10 @@ class WebPage(Object): Display URL for this webpage. type (``str``, *optional*): - Type of webpage, can be `article`, `photo`, `gif`, `video` or `document` afaik. #TODO + Type of webpage preview, known types (at the time of writing) are `article`, `photo`, `gif`, `video` and `document`. site_name (``str``, *optional*): - Site name. + Webpage site name. title (``str``, *optional*): Title of this webpage. @@ -48,25 +48,25 @@ class WebPage(Object): Description of this webpage. audio (:obj:`Audio`, *optional*): - Webpage is an audio file, information about the file. + Webpage preview is an audio file, information about the file. document (:obj:`Document`, *optional*): - Webpage is a general file, information about the file. + Webpage preview is a general file, information about the file. photo (:obj:`Photo`, *optional*): - Webpage is a photo, information about the photo. + Webpage preview is a photo, information about the photo. animation (:obj:`Animation`, *optional*): - Webpage is an animation, information about the animation. + Webpage preview is an animation, information about the animation. video (:obj:`Video`, *optional*): - Webpage is a video, information about the video.abs + Webpage preview is a video, information about the video. embed_url (``str``, *optional*): Embedded content URL. embed_type (``str``, *optional*): - Embedded content type, can be `iframe` #TODO + Embedded content type, like `iframe` embed_width (``int``, *optional*): Embedded content width. @@ -75,10 +75,10 @@ class WebPage(Object): Embedded content height. duration (``int``, *optional*): - :shrug: + Uknown at the time of writing. author (``str``, *optional*): - Author of the webpage, eg the Twitter user. + Author of the webpage, eg the Twitter user for a tweet, or the author in an article. """ __slots__ = [ From dfffa9510a52cf9c114e7956586e49e6eb733580 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Tue, 2 Jul 2019 21:04:08 -0400 Subject: [PATCH 4/6] Add more types in WebPage docs --- pyrogram/client/types/messages_and_media/webpage.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/types/messages_and_media/webpage.py b/pyrogram/client/types/messages_and_media/webpage.py index 7785e754..9baf9de5 100644 --- a/pyrogram/client/types/messages_and_media/webpage.py +++ b/pyrogram/client/types/messages_and_media/webpage.py @@ -36,7 +36,9 @@ class WebPage(Object): Display URL for this webpage. type (``str``, *optional*): - Type of webpage preview, known types (at the time of writing) are `article`, `photo`, `gif`, `video` and `document`. + Type of webpage preview, known types (at the time of writing) are: + `article`, `photo`, `gif`, `video` and `document`, + `telegram_user`, `telegram_bot`, `telegram_channel`, `telegram_megagroup`. site_name (``str``, *optional*): Webpage site name. From f7bfd5597ec97fc0cddfbcb9c86c062e53481c07 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Wed, 3 Jul 2019 09:27:06 -0400 Subject: [PATCH 5/6] Make optional values italics --- pyrogram/client/types/messages_and_media/webpage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/webpage.py b/pyrogram/client/types/messages_and_media/webpage.py index 9baf9de5..9ddc7c6c 100644 --- a/pyrogram/client/types/messages_and_media/webpage.py +++ b/pyrogram/client/types/messages_and_media/webpage.py @@ -37,8 +37,8 @@ class WebPage(Object): type (``str``, *optional*): Type of webpage preview, known types (at the time of writing) are: - `article`, `photo`, `gif`, `video` and `document`, - `telegram_user`, `telegram_bot`, `telegram_channel`, `telegram_megagroup`. + *"article"*, *"photo"*, *"gif"*, *"video"* and *"document"*, + *"telegram_user"*, *"telegram_bot"*, *"telegram_channel"*, *"telegram_megagroup"*. site_name (``str``, *optional*): Webpage site name. From b4a8763452d9073baa09731f2778bd6a0c38cb70 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Wed, 3 Jul 2019 10:59:11 -0400 Subject: [PATCH 6/6] Message: media = None if WebPage is empty --- pyrogram/client/types/messages_and_media/message.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 323693c5..a1d42b79 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -574,6 +574,8 @@ class Message(Object, Update): elif isinstance(media, types.MessageMediaWebPage): if isinstance(media.webpage, types.WebPage): web_page = pyrogram.WebPage._parse(client, media.webpage) + else: + media = None elif isinstance(media, types.MessageMediaPoll): poll = pyrogram.Poll._parse(client, media)