2022-07-26 10:07:31 +00:00
|
|
|
import imghdr
|
2023-02-18 07:41:10 +00:00
|
|
|
from typing import List, Any, Union
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-10-08 00:59:08 +00:00
|
|
|
from pydantic import BaseModel, PrivateAttr
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-12-10 12:37:43 +00:00
|
|
|
__all__ = ("ArtworkImage", "PostInfo")
|
|
|
|
|
2023-02-18 07:41:10 +00:00
|
|
|
from telegram import InputMediaPhoto, InputMediaVideo, InputMediaDocument
|
|
|
|
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-10-08 00:59:08 +00:00
|
|
|
class ArtworkImage(BaseModel):
|
|
|
|
art_id: int
|
|
|
|
page: int = 0
|
|
|
|
data: bytes = b""
|
2023-02-18 07:41:10 +00:00
|
|
|
ext: str = "jpg"
|
2022-10-08 00:59:08 +00:00
|
|
|
is_error: bool = False
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-10-08 00:59:08 +00:00
|
|
|
@property
|
|
|
|
def format(self) -> str:
|
2023-02-18 07:41:10 +00:00
|
|
|
return "" if self.is_error else (imghdr.what(None, self.data) or self.ext)
|
|
|
|
|
|
|
|
def input_media(self, *args, **kwargs) -> Union[None, InputMediaDocument, InputMediaPhoto, InputMediaVideo]:
|
|
|
|
file_type = self.format
|
|
|
|
if file_type in {"jpg", "jpeg", "png", "webp"}:
|
|
|
|
return InputMediaPhoto(self.data, *args, **kwargs)
|
|
|
|
if file_type in {"gif", "mp4", "mov", "avi", "mkv", "webm", "flv"}:
|
|
|
|
return InputMediaVideo(self.data, *args, **kwargs)
|
|
|
|
return InputMediaDocument(self.data, *args, **kwargs)
|
2022-10-08 00:59:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PostInfo(BaseModel):
|
|
|
|
_data: dict = PrivateAttr()
|
|
|
|
post_id: int
|
|
|
|
user_uid: int
|
|
|
|
subject: str
|
|
|
|
image_urls: List[str]
|
2023-02-18 07:41:10 +00:00
|
|
|
video_urls: List[str]
|
2022-10-08 00:59:08 +00:00
|
|
|
created_at: int
|
|
|
|
|
|
|
|
def __init__(self, _data: dict, **data: Any):
|
|
|
|
super().__init__(**data)
|
|
|
|
self._data = _data
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def paste_data(cls, data: dict) -> "PostInfo":
|
|
|
|
_data_post = data["post"]
|
|
|
|
post = _data_post["post"]
|
|
|
|
post_id = post["post_id"]
|
|
|
|
subject = post["subject"]
|
|
|
|
image_list = _data_post["image_list"]
|
2023-02-18 07:41:10 +00:00
|
|
|
image_urls = [image["url"] for image in image_list]
|
|
|
|
vod_list = _data_post["vod_list"]
|
|
|
|
video_urls = [vod["resolutions"][-1]["url"] for vod in vod_list]
|
2022-10-08 00:59:08 +00:00
|
|
|
created_at = post["created_at"]
|
|
|
|
user = _data_post["user"] # 用户数据
|
|
|
|
user_uid = user["uid"] # 用户ID
|
2022-10-10 11:07:28 +00:00
|
|
|
return PostInfo(
|
|
|
|
_data=data,
|
|
|
|
post_id=post_id,
|
|
|
|
user_uid=user_uid,
|
|
|
|
subject=subject,
|
|
|
|
image_urls=image_urls,
|
2023-02-18 07:41:10 +00:00
|
|
|
video_urls=video_urls,
|
2022-10-10 11:07:28 +00:00
|
|
|
created_at=created_at,
|
|
|
|
)
|
2022-10-08 00:59:08 +00:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self._data[item]
|