From 4b96d236212b1212976f4c3c60479e7aaed866cb Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 25 Mar 2025 20:06:31 +0200 Subject: [PATCH] Add sticker-download-thumbnails for adding thumbnails to an existing pack --- setup.py | 1 + sticker/download_thumbnails.py | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 sticker/download_thumbnails.py diff --git a/setup.py b/setup.py index d747b76..ab0964d 100644 --- a/setup.py +++ b/setup.py @@ -50,5 +50,6 @@ setuptools.setup( entry_points={"console_scripts": [ "sticker-import=sticker.stickerimport:cmd", "sticker-pack=sticker.pack:cmd", + "sticker-download-thumbnails=sticker.download_thumbnails:cmd", ]}, ) diff --git a/sticker/download_thumbnails.py b/sticker/download_thumbnails.py new file mode 100644 index 0000000..e082914 --- /dev/null +++ b/sticker/download_thumbnails.py @@ -0,0 +1,58 @@ +# maunium-stickerpicker - A fast and simple Matrix sticker picker widget. +# Copyright (C) 2025 Tulir Asokan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from pathlib import Path +from typing import Dict +import argparse +import asyncio +import json + +from aiohttp import ClientSession +from yarl import URL + +from .lib import matrix, util + +parser = argparse.ArgumentParser() +parser.add_argument("--config", + help="Path to JSON file with Matrix homeserver and access_token", + type=str, default="config.json", metavar="file") +parser.add_argument("path", help="Path to the sticker pack JSON file", type=str) + + +async def main(args: argparse.Namespace) -> None: + await matrix.load_config(args.config) + with util.open_utf8(args.path) as pack_file: + pack = json.load(pack_file) + print(f"Loaded existing pack meta from {args.path}") + + stickers_data: Dict[str, bytes] = {} + async with ClientSession() as sess: + for sticker in pack["stickers"]: + dl_url = URL(matrix.homeserver_url) / "_matrix/client/v1/media/download" / sticker["url"].removeprefix("mxc://") + print("Downloading", sticker["url"]) + async with sess.get(dl_url, headers={"Authorization": f"Bearer {matrix.access_token}"}) as resp: + resp.raise_for_status() + stickers_data[sticker["url"]] = await resp.read() + + print("All stickers downloaded, generating thumbnails...") + util.add_thumbnails(pack["stickers"], stickers_data, Path(args.path).parent) + print("Done!") + +def cmd(): + asyncio.run(main(parser.parse_args())) + + +if __name__ == "__main__": + cmd()