-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathweb_page.py
More file actions
189 lines (155 loc) · 6.34 KB
/
web_page.py
File metadata and controls
189 lines (155 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Hydrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2023 Dan <https://github.com/delivrance>
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
#
# This file is part of Hydrogram.
#
# Hydrogram 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.
#
# Hydrogram 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 Hydrogram. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import hydrogram
from hydrogram import raw, types
from hydrogram.types.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 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.
title (``str``, *optional*):
Title of this webpage.
description (``str``, *optional*):
Description of this webpage.
audio (:obj:`~hydrogram.types.Audio`, *optional*):
Webpage preview is an audio file, information about the file.
document (:obj:`~hydrogram.types.Document`, *optional*):
Webpage preview is a general file, information about the file.
photo (:obj:`~hydrogram.types.Photo`, *optional*):
Webpage preview is a photo, information about the photo.
animation (:obj:`~hydrogram.types.Animation`, *optional*):
Webpage preview is an animation, information about the animation.
video (:obj:`~hydrogram.types.Video`, *optional*):
Webpage preview is a video, information about the video.
embed_url (``str``, *optional*):
Embedded content URL.
embed_type (``str``, *optional*):
Embedded content type, like `iframe`
embed_width (``int``, *optional*):
Embedded content width.
embed_height (``int``, *optional*):
Embedded content height.
duration (``int``, *optional*):
Unknown at the time of writing.
author (``str``, *optional*):
Author of the webpage, eg the Twitter user for a tweet, or the author in an article.
"""
def __init__(
self,
*,
client: hydrogram.Client = None,
id: str,
url: str,
display_url: str,
type: str | None = None,
site_name: str | None = None,
title: str | None = None,
description: str | None = None,
audio: types.Audio = None,
document: types.Document = None,
photo: types.Photo = None,
animation: types.Animation = None,
video: types.Video = None,
embed_url: str | None = None,
embed_type: str | None = None,
embed_width: int | None = None,
embed_height: int | None = None,
duration: int | None = None,
author: str | None = None,
):
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: raw.types.WebPage) -> WebPage:
audio = None
document = None
photo = None
animation = None
video = None
if isinstance(webpage.photo, raw.types.Photo):
photo = types.Photo._parse(client, webpage.photo)
doc = webpage.document
if isinstance(doc, raw.types.Document):
attributes = {type(i): i for i in doc.attributes}
file_name = getattr(
attributes.get(raw.types.DocumentAttributeFilename),
"file_name",
None,
)
if raw.types.DocumentAttributeAudio in attributes:
audio_attributes = attributes[raw.types.DocumentAttributeAudio]
audio = types.Audio._parse(client, doc, audio_attributes, file_name)
elif raw.types.DocumentAttributeAnimated in attributes:
video_attributes = attributes.get(raw.types.DocumentAttributeVideo)
animation = types.Animation._parse(client, doc, video_attributes, file_name)
elif raw.types.DocumentAttributeVideo in attributes:
video_attributes = attributes[raw.types.DocumentAttributeVideo]
video = types.Video._parse(client, doc, video_attributes, file_name)
else:
document = types.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,
)