Skip to content

Commit 9059bab

Browse files
authored
Merge pull request pyrogram#268 from mendelmaleh/develop
Add message.web_page attributes
2 parents 18b581f + b4a8763 commit 9059bab

File tree

4 files changed

+203
-7
lines changed

4 files changed

+203
-7
lines changed

docs/source/api/types.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Messages & Media
5454
- :class:`Venue`
5555
- :class:`Sticker`
5656
- :class:`Game`
57+
- :class:`WebPage`
5758
- :class:`Poll`
5859
- :class:`PollOption`
5960

@@ -137,6 +138,7 @@ Details
137138
.. autoclass:: Venue()
138139
.. autoclass:: Sticker()
139140
.. autoclass:: Game()
141+
.. autoclass:: WebPage()
140142
.. autoclass:: Poll()
141143
.. autoclass:: PollOption()
142144

pyrogram/client/types/messages_and_media/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
from .video import Video
3535
from .video_note import VideoNote
3636
from .voice import Voice
37+
from .webpage import WebPage
3738

3839
__all__ = [
3940
"Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
40-
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice"
41+
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage"
4142
]

pyrogram/client/types/messages_and_media/message.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,8 @@ class Message(Object, Update):
177177
venue (:obj:`Venue`, *optional*):
178178
Message is a venue, information about the venue.
179179
180-
web_page (``bool``, *optional*):
180+
web_page (:obj:`WebPage`, *optional*):
181181
Message was sent with a webpage preview.
182-
**Note:** Support for web pages is still basic; a simple boolean is set in case the message contains a
183-
web page preview. In future versions this property could turn into a full web page object that contains
184-
more details.
185182
186183
poll (:obj:`Poll`, *optional*):
187184
Message is a native poll, information about the poll.
@@ -575,10 +572,14 @@ def _parse(client, message: types.Message or types.MessageService or types.Messa
575572
else:
576573
document = pyrogram.Document._parse(client, doc, file_name)
577574
elif isinstance(media, types.MessageMediaWebPage):
578-
web_page = True
579-
media = None
575+
if isinstance(media.webpage, types.WebPage):
576+
web_page = pyrogram.WebPage._parse(client, media.webpage)
577+
else:
578+
media = None
579+
580580
elif isinstance(media, types.MessageMediaPoll):
581581
poll = pyrogram.Poll._parse(client, media)
582+
582583
else:
583584
media = None
584585

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import pyrogram
20+
from pyrogram.api import types
21+
from ..object import Object
22+
23+
24+
class WebPage(Object):
25+
# TODO: hash, cached_page
26+
"""A webpage preview
27+
28+
Parameters:
29+
id (``str``):
30+
Unique identifier for this webpage.
31+
32+
url (``str``):
33+
Full URL for this webpage.
34+
35+
display_url (``str``):
36+
Display URL for this webpage.
37+
38+
type (``str``, *optional*):
39+
Type of webpage preview, known types (at the time of writing) are:
40+
*"article"*, *"photo"*, *"gif"*, *"video"* and *"document"*,
41+
*"telegram_user"*, *"telegram_bot"*, *"telegram_channel"*, *"telegram_megagroup"*.
42+
43+
site_name (``str``, *optional*):
44+
Webpage site name.
45+
46+
title (``str``, *optional*):
47+
Title of this webpage.
48+
49+
description (``str``, *optional*):
50+
Description of this webpage.
51+
52+
audio (:obj:`Audio`, *optional*):
53+
Webpage preview is an audio file, information about the file.
54+
55+
document (:obj:`Document`, *optional*):
56+
Webpage preview is a general file, information about the file.
57+
58+
photo (:obj:`Photo`, *optional*):
59+
Webpage preview is a photo, information about the photo.
60+
61+
animation (:obj:`Animation`, *optional*):
62+
Webpage preview is an animation, information about the animation.
63+
64+
video (:obj:`Video`, *optional*):
65+
Webpage preview is a video, information about the video.
66+
67+
embed_url (``str``, *optional*):
68+
Embedded content URL.
69+
70+
embed_type (``str``, *optional*):
71+
Embedded content type, like `iframe`
72+
73+
embed_width (``int``, *optional*):
74+
Embedded content width.
75+
76+
embed_height (``int``, *optional*):
77+
Embedded content height.
78+
79+
duration (``int``, *optional*):
80+
Uknown at the time of writing.
81+
82+
author (``str``, *optional*):
83+
Author of the webpage, eg the Twitter user for a tweet, or the author in an article.
84+
"""
85+
86+
__slots__ = [
87+
"id", "url", "display_url", "type", "site_name", "title", "description",
88+
"audio", "document", "photo", "animation", "video",
89+
"embed_url", "embed_type", "embed_width", "embed_height", "duration", "author"
90+
]
91+
92+
def __init__(
93+
self,
94+
*,
95+
client: "pyrogram.BaseClient" = None,
96+
id: str,
97+
url: str,
98+
display_url: str,
99+
type: str = None,
100+
site_name: str = None,
101+
title: str = None,
102+
description: str = None,
103+
audio: "pyrogram.Audio" = None,
104+
document: "pyrogram.Document" = None,
105+
photo: "pyrogram.Photo" = None,
106+
animation: "pyrogram.Animation" = None,
107+
video: "pyrogram.Video" = None,
108+
embed_url: str = None,
109+
embed_type: str = None,
110+
embed_width: int = None,
111+
embed_height: int = None,
112+
duration: int = None,
113+
author: str = None
114+
) -> "pyrogram.WebPage":
115+
super().__init__(client)
116+
117+
self.id = id
118+
self.url = url
119+
self.display_url = display_url
120+
self.type = type
121+
self.site_name = site_name
122+
self.title = title
123+
self.description = description
124+
self.audio = audio
125+
self.document = document
126+
self.photo = photo
127+
self.animation = animation
128+
self.video = video
129+
self.embed_url = embed_url
130+
self.embed_type = embed_type
131+
self.embed_width = embed_width
132+
self.embed_height = embed_height
133+
self.duration = duration
134+
self.author = author
135+
136+
@staticmethod
137+
def _parse(client, webpage: types.WebPage) -> "WebPage":
138+
audio = None
139+
document = None
140+
photo = None
141+
animation = None
142+
video = None
143+
144+
if isinstance(webpage.photo, types.Photo):
145+
photo = pyrogram.Photo._parse(client, webpage.photo)
146+
147+
doc = webpage.document
148+
149+
if isinstance(doc, types.Document):
150+
attributes = {type(i): i for i in doc.attributes}
151+
152+
file_name = getattr(
153+
attributes.get(
154+
types.DocumentAttributeFilename, None
155+
), "file_name", None
156+
)
157+
158+
if types.DocumentAttributeAudio in attributes:
159+
audio_attributes = attributes[types.DocumentAttributeAudio]
160+
audio = pyrogram.Audio._parse(client, doc, audio_attributes, file_name)
161+
162+
elif types.DocumentAttributeAnimated in attributes:
163+
video_attributes = attributes.get(types.DocumentAttributeVideo, None)
164+
animation = pyrogram.Animation._parse(client, doc, video_attributes, file_name)
165+
166+
elif types.DocumentAttributeVideo in attributes:
167+
video_attributes = attributes[types.DocumentAttributeVideo]
168+
video = pyrogram.Video._parse(client, doc, video_attributes, file_name)
169+
170+
else:
171+
document = pyrogram.Document._parse(client, doc, file_name)
172+
173+
return WebPage(
174+
id=str(webpage.id),
175+
url=webpage.url,
176+
display_url=webpage.display_url,
177+
type=webpage.type,
178+
site_name=webpage.site_name,
179+
title=webpage.title,
180+
description=webpage.description,
181+
audio=audio,
182+
document=document,
183+
photo=photo,
184+
animation=animation,
185+
video=video,
186+
embed_url=webpage.embed_url,
187+
embed_type=webpage.embed_type,
188+
embed_width=webpage.embed_width,
189+
embed_height=webpage.embed_height,
190+
duration=webpage.duration,
191+
author=webpage.author
192+
)

0 commit comments

Comments
 (0)