From be477874e3b66cc5f5432b1fb1c684db543c70c8 Mon Sep 17 00:00:00 2001 From: xz-dev Date: Tue, 16 Jul 2024 18:15:49 +0800 Subject: [PATCH] feat: support animated(lottie) sticker --- requirements.txt | 1 + sticker/lib/util.py | 32 +++++++++++++++++++++++++++++--- sticker/pack.py | 2 +- sticker/stickerimport.py | 2 +- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index bd4cfb0..2b3edb0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ telethon cryptg python-magic moviepy +lottie[all] diff --git a/sticker/lib/util.py b/sticker/lib/util.py index 43e01d3..ec96a2b 100644 --- a/sticker/lib/util.py +++ b/sticker/lib/util.py @@ -76,16 +76,42 @@ def _convert_image(data: bytes) -> (bytes, int, int): return new_file.getvalue(), w, h -def convert_image(data: bytes) -> (bytes, str, int, int): +def _convert_sticker(data: bytes) -> (bytes, str, int, int): mimetype = guess_mime(data) if mimetype.startswith("video/"): data = video_to_gif(data, mimetype) print(".", end="", flush=True) mimetype = "image/gif" + elif mimetype.startswith("application/gzip"): + print(".", end="", flush=True) + # unzip file + import gzip + with gzip.open(BytesIO(data), "rb") as f: + data = f.read() + mimetype = guess_mime(data) + suffix = mimetypes.guess_extension(mimetype) + with tempfile.NamedTemporaryFile(suffix=suffix) as temp: + temp.write(data) + with tempfile.NamedTemporaryFile(suffix=".gif") as gif: + # run lottie_convert.py input output + print(".", end="", flush=True) + import subprocess + cmd = ["lottie_convert.py", temp.name, gif.name] + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode != 0: + raise RuntimeError(f"Run {cmd} failed with code {retcode}, Error occurred:\n{result.stderr}") + gif.seek(0) + data = gif.read() + mimetype = "image/gif" + rlt = _convert_image(data) + return rlt[0], mimetype, rlt[1], rlt[2] + + +def convert_sticker(data: bytes) -> (bytes, str, int, int): try: - rlt = _convert_image(data) - return rlt[0], mimetype, rlt[1], rlt[2] + return _convert_sticker(data) except Exception as e: + mimetype = guess_mime(data) print(f"Error converting image, mimetype: {mimetype}") ext = mimetypes.guess_extension(mimetype) with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as temp: diff --git a/sticker/pack.py b/sticker/pack.py index 6b1a646..4815bc5 100644 --- a/sticker/pack.py +++ b/sticker/pack.py @@ -77,7 +77,7 @@ async def upload_sticker(file: str, directory: str, old_stickers: Dict[str, matr } print(f".. using existing upload") else: - image_data, mimetype, width, height = util.convert_image(image_data) + image_data, mimetype, width, height = util.convert_sticker(image_data) print(".", end="", flush=True) mxc = await matrix.upload(image_data, mimetype, file) print(".", end="", flush=True) diff --git a/sticker/stickerimport.py b/sticker/stickerimport.py index 6b12961..6d1e7be 100644 --- a/sticker/stickerimport.py +++ b/sticker/stickerimport.py @@ -33,7 +33,7 @@ async def reupload_document(client: TelegramClient, document: Document) -> matri print(f"Reuploading {document.id}", end="", flush=True) data = await client.download_media(document, file=bytes) print(".", end="", flush=True) - data, mimetype, width, height = util.convert_image(data) + data, mimetype, width, height = util.convert_sticker(data) print(".", end="", flush=True) mxc = await matrix.upload(data, mimetype, f"{document.id}.png") print(".", flush=True)