From 9997ec4a31370e3a02308bfee7cf323d1123d4fd Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt <3165388245@qq.com> Date: Sat, 14 Oct 2023 17:35:45 +0800 Subject: [PATCH] :sparkles: version 0.5.0 support file/bytes for media element --- nonebot/adapters/satori/message.py | 88 +++++++++++++++++++++++++++--- pyproject.toml | 2 +- 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/nonebot/adapters/satori/message.py b/nonebot/adapters/satori/message.py index 3cb1831..9ff0fb7 100644 --- a/nonebot/adapters/satori/message.py +++ b/nonebot/adapters/satori/message.py @@ -1,3 +1,6 @@ +from io import BytesIO +from pathlib import Path +from base64 import b64encode from dataclasses import field, dataclass from typing_extensions import NotRequired, override from typing import Any, List, Type, Union, Iterable, Optional, TypedDict @@ -8,6 +11,11 @@ from .utils import Element, parse, escape +class RawData(TypedDict): + data: Union[bytes, BytesIO] + mime: str + + class MessageSegment(BaseMessageSegment["Message"]): def __str__(self) -> str: def _attr(key: str, value: Any): @@ -68,9 +76,25 @@ def link(href: str) -> "Link": @staticmethod def image( - src: str, cache: Optional[bool] = None, timeout: Optional[str] = None + url: Optional[str] = None, + path: Optional[Union[str, Path]] = None, + raw: Optional[RawData] = None, + cache: Optional[bool] = None, + timeout: Optional[str] = None, ) -> "Image": - data = {"src": src} + if url: + data = {"src": url} + elif path: + data = {"src": Path(path).as_uri()} + elif raw: + bd = ( + raw["data"] + if isinstance(raw["data"], bytes) + else raw["data"].getvalue() + ) + data = {"src": f"data:{raw['mime']};base64,{b64encode(bd).decode()}"} + else: + raise ValueError("image need at least one of url, path and raw") if cache is not None: data["cache"] = cache if timeout is not None: @@ -79,9 +103,25 @@ def image( @staticmethod def audio( - src: str, cache: Optional[bool] = None, timeout: Optional[str] = None + url: Optional[str] = None, + path: Optional[Union[str, Path]] = None, + raw: Optional[RawData] = None, + cache: Optional[bool] = None, + timeout: Optional[str] = None, ) -> "Audio": - data = {"src": src} + if url: + data = {"src": url} + elif path: + data = {"src": Path(path).as_uri()} + elif raw: + bd = ( + raw["data"] + if isinstance(raw["data"], bytes) + else raw["data"].getvalue() + ) + data = {"src": f"data:{raw['mime']};base64,{b64encode(bd).decode()}"} + else: + raise ValueError("audio need at least one of url, path and raw") if cache is not None: data["cache"] = cache if timeout is not None: @@ -90,9 +130,25 @@ def audio( @staticmethod def video( - src: str, cache: Optional[bool] = None, timeout: Optional[str] = None + url: Optional[str] = None, + path: Optional[Union[str, Path]] = None, + raw: Optional[RawData] = None, + cache: Optional[bool] = None, + timeout: Optional[str] = None, ) -> "Video": - data = {"src": src} + if url: + data = {"src": url} + elif path: + data = {"src": Path(path).as_uri()} + elif raw: + bd = ( + raw["data"] + if isinstance(raw["data"], bytes) + else raw["data"].getvalue() + ) + data = {"src": f"data:{raw['mime']};base64,{b64encode(bd).decode()}"} + else: + raise ValueError("video need at least one of url, path and raw") if cache is not None: data["cache"] = cache if timeout is not None: @@ -101,9 +157,25 @@ def video( @staticmethod def file( - src: str, cache: Optional[bool] = None, timeout: Optional[str] = None + url: Optional[str] = None, + path: Optional[Union[str, Path]] = None, + raw: Optional[RawData] = None, + cache: Optional[bool] = None, + timeout: Optional[str] = None, ) -> "File": - data = {"src": src} + if url: + data = {"src": url} + elif path: + data = {"src": Path(path).as_uri()} + elif raw: + bd = ( + raw["data"] + if isinstance(raw["data"], bytes) + else raw["data"].getvalue() + ) + data = {"src": f"data:{raw['mime']};base64,{b64encode(bd).decode()}"} + else: + raise ValueError("file need at least one of url, path and raw") if cache is not None: data["cache"] = cache if timeout is not None: diff --git a/pyproject.toml b/pyproject.toml index e627cc4..c3dfb84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-adapter-satori" -version = "0.4.0" +version = "0.5.0" description = "Satori Protocol Adapter for Nonebot2" authors = [ {name = "RF-Tar-Railt",email = "rf_tar_railt@qq.com"},