|
| 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