Exemple #1
0
    def _parse_telegram_document_meta(
            evt: Message, file: DBTelegramFile, attrs: DocAttrs,
            thumb_size: TypePhotoSize) -> Tuple[ImageInfo, str]:
        document = evt.media.document
        name = evt.message or attrs.name
        if attrs.is_sticker:
            alt = attrs.sticker_alt
            if len(alt) > 0:
                try:
                    name = f"{alt} ({unicodedata.name(alt[0]).lower()})"
                except ValueError:
                    name = alt

        generic_types = ("text/plain", "application/octet-stream")
        if file.mime_type in generic_types and document.mime_type not in generic_types:
            mime_type = document.mime_type or file.mime_type
        elif file.mime_type == 'application/ogg':
            mime_type = 'audio/ogg'
        else:
            mime_type = file.mime_type or document.mime_type
        info = ImageInfo(size=file.size, mimetype=mime_type)

        if attrs.mime_type and not file.was_converted:
            file.mime_type = attrs.mime_type or file.mime_type
        if file.width and file.height:
            info.width, info.height = file.width, file.height
        elif attrs.width and attrs.height:
            info.width, info.height = attrs.width, attrs.height

        if file.thumbnail:
            if file.thumbnail.decryption_info:
                info.thumbnail_file = file.thumbnail.decryption_info
            else:
                info.thumbnail_url = file.thumbnail.mxc
            info.thumbnail_info = ThumbnailInfo(
                mimetype=file.thumbnail.mime_type,
                height=file.thumbnail.height or thumb_size.h,
                width=file.thumbnail.width or thumb_size.w,
                size=file.thumbnail.size)
        else:
            # This is a hack for bad clients like Riot iOS that require a thumbnail
            if file.decryption_info:
                info.thumbnail_file = file.decryption_info
            else:
                info.thumbnail_url = file.mxc
            info.thumbnail_info = ImageInfo.deserialize(info.serialize())

        return info, name
Exemple #2
0
    async def tex(self, evt: MessageEvent, formula: str) -> None:
        fig = plot.figure(figsize=(0.01, 0.01))
        text = fig.text(0, 0, rf"${formula}$",
                        fontsize=self.config["font_size"],
                        usetex=self.config["use_tex"])
        info = ImageInfo(thumbnail_info=ThumbnailInfo())

        output = BytesIO()
        if self.config["mode"] == "svg":
            fig.savefig(output, format="svg", bbox_inches="tight")
            data = output.getvalue()

            file_name = "tex.svg"
            info.mimetype = "image/svg+xml"
            info.size = len(data)

            bb = text.get_window_extent(fig.canvas.get_renderer())
            info.width, info.height = int(bb.width), int(bb.height)

            uri = await self.client.upload_media(data, info.mimetype, file_name)
        else:
            fig.savefig(output, dpi=300, format="png", bbox_inches="tight")
            data = output.getvalue()

            file_name = "tex.png"
            info.mimetype = "image/png"
            info.size = len(data)

            output.seek(0)
            img = Image.open(output)
            info.width, info.height = img.size

            uri = await self.client.upload_media(data, info.mimetype, file_name)

        output.seek(0)
        output.truncate(0)
        fig.savefig(output, dpi=self.config["thumbnail_dpi"], format="png", bbox_inches="tight")

        data = output.getvalue()
        info.thumbnail_url = await self.client.upload_media(data, "image/png", "tex.thumb.png")
        info.thumbnail_info.size = len(data)
        plot.close(fig)

        output.seek(0)
        img = Image.open(output)
        info.thumbnail_info.width, info.thumbnail_info.height = img.size
        img.close()
        output.close()

        await self.client.send_image(evt.room_id, uri, info=info, file_name=file_name)
Exemple #3
0
 async def _reupload(self,
                     url: URL,
                     title: Optional[str] = None,
                     dimensions: Optional[Tuple[int, int]] = None,
                     external_url: Optional[str] = None,
                     thumbnail_url: Optional[URL] = None,
                     thumbnail_dimensions: Optional[Tuple[int, int]] = None,
                     headers: Optional[Dict[str, str]] = None) -> Image:
     info = ImageInfo()
     if "user_agent" in self.config:
         headers["User-Agent"] = self.config["user_agent"]
     async with self.bot.http.get(url, headers=headers) as resp:
         data = await resp.read()
         info.size = len(data)
         info.mimetype = resp.headers["Content-Type"]
         if not info.mimetype:
             info.mimetype = magic.from_buffer(data, mime=True)
         if not title:
             title = self._get_filename(url, resp, info.mimetype)
     if dimensions:
         info.width, info.height = dimensions
     elif Pillow:
         img = Pillow.open(BytesIO(data))
         info.width, info.height = img.size
     mxc = await self.bot.client.upload_media(data, info.mimetype)
     if thumbnail_url:
         thumbnail = await self._reupload(thumbnail_url,
                                          title=title,
                                          headers=headers,
                                          dimensions=thumbnail_dimensions)
         info.thumbnail_url = thumbnail.url
         info.thumbnail_info = thumbnail.info
     return Image(url=mxc,
                  info=info,
                  title=title,
                  external_url=external_url)