PamGram/modules/apihelper/base.py

51 lines
1.3 KiB
Python
Raw Normal View History

2022-07-26 10:07:31 +00:00
import imghdr
2022-10-08 00:59:08 +00:00
from typing import List, Any
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-10-08 00:59:08 +00:00
class ArtworkImage(BaseModel):
art_id: int
page: int = 0
data: bytes = b""
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:
if self.is_error:
return ""
2022-07-26 10:07:31 +00:00
else:
2022-10-08 00:59:08 +00:00
imghdr.what(None, self.data)
class PostInfo(BaseModel):
_data: dict = PrivateAttr()
post_id: int
user_uid: int
subject: str
image_urls: List[str]
created_at: int
def __init__(self, _data: dict, **data: Any):
super().__init__(**data)
self._data = _data
@classmethod
def paste_data(cls, data: dict) -> "PostInfo":
image_urls = []
_data_post = data["post"]
post = _data_post["post"]
post_id = post["post_id"]
subject = post["subject"]
image_list = _data_post["image_list"]
2022-07-26 10:07:31 +00:00
for image in image_list:
2022-10-08 00:59:08 +00:00
image_urls.append(image["url"])
created_at = post["created_at"]
user = _data_post["user"] # 用户数据
user_uid = user["uid"] # 用户ID
return PostInfo(_data=data, post_id=post_id, user_uid=user_uid, subject=subject, image_urls=image_urls,
created_at=created_at)
def __getitem__(self, item):
return self._data[item]