Skip to content

Commit

Permalink
✨ version 0.5.0
Browse files Browse the repository at this point in the history
support file/bytes for media element
  • Loading branch information
RF-Tar-Railt committed Oct 14, 2023
1 parent 15dfdba commit 9997ec4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
88 changes: 80 additions & 8 deletions nonebot/adapters/satori/message.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"},
Expand Down

0 comments on commit 9997ec4

Please sign in to comment.