# Generated by Netbeans # Author: Eramde # Date: 09.2019 import tempfile import logging import gzip import subprocess import os from io import BytesIO from typing import Optional, Tuple LOG: logging.Logger = logging.getLogger("mau.util.tgs") TYPE_TO_MIME = {"image": "image/png", "gif": "image/gif", "video": "video/mp4"} TYPE_TO_FORMAT = {"image": ".png", "gif": ".gif", "video": ".mp4"} def convert_tgs_to(file: bytes, convert_to: str, width: int = 200, height: int = 200) \ -> Tuple[str, bytes, Optional[int], Optional[int]]: if convert_to in TYPE_TO_FORMAT: file_ext = TYPE_TO_FORMAT[convert_to] mime = TYPE_TO_MIME[convert_to] lottie = gzip.open(BytesIO(file)) with tempfile.NamedTemporaryFile(mode="w+b", suffix=".json") as json_out: with tempfile.NamedTemporaryFile(mode="r+b", suffix=file_ext) as tmp: tmp_output_file = tmp.name json_out.write(lottie.read()) json_out.flush() subprocess.run(["puppeteer-lottie", "-q", "-i", json_out.name, "-o", tmp_output_file, "-w", str(width), "-h", str(height)], capture_output=True) with open(tmp_output_file, mode="r+b") as out_file: out = out_file.read() os.remove(tmp_output_file) return mime, out, width, height else: LOG.warning(f"Unable to convert animated sticker, type {convert_to} not supported") return "application/gzip", file, None, None