2022-11-21 04:23:22 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-10-19 12:22:24 +00:00
|
|
|
import httpx
|
|
|
|
|
|
|
|
from core.config import config
|
2022-10-20 14:21:26 +00:00
|
|
|
from utils.log import logger
|
2022-10-19 12:22:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PbClient:
|
|
|
|
def __init__(self):
|
|
|
|
self.client = httpx.AsyncClient()
|
2022-10-28 07:11:14 +00:00
|
|
|
self.PB_API = config.error.pb_url
|
|
|
|
self.sunset: int = config.error.pb_sunset # 自动销毁时间 单位为秒
|
2022-10-19 12:22:24 +00:00
|
|
|
self.private: bool = True
|
2022-10-28 07:11:14 +00:00
|
|
|
self.max_lines: int = config.error.pb_max_lines
|
2022-10-19 12:22:24 +00:00
|
|
|
|
2022-11-21 04:23:22 +00:00
|
|
|
async def create_pb(self, content: str) -> Optional[str]:
|
2022-10-19 12:22:24 +00:00
|
|
|
if not self.PB_API:
|
2022-11-21 04:23:22 +00:00
|
|
|
return None
|
2022-10-20 14:21:26 +00:00
|
|
|
logger.info("正在上传日记到 pb")
|
2022-11-21 04:23:22 +00:00
|
|
|
content = "\n".join(content.splitlines()[-self.max_lines:]) + "\n"
|
2022-10-19 12:22:24 +00:00
|
|
|
data = {
|
|
|
|
"c": content,
|
|
|
|
}
|
|
|
|
if self.private:
|
|
|
|
data["p"] = "1"
|
|
|
|
if self.sunset:
|
|
|
|
data["sunset"] = self.sunset
|
2022-11-21 04:23:22 +00:00
|
|
|
result = await self.client.post(self.PB_API, data=data)
|
|
|
|
if result.is_error:
|
|
|
|
logger.warning("上传日记到 pb 失败 status_code[%s]", result.status_code)
|
|
|
|
return None
|
2022-10-20 14:21:26 +00:00
|
|
|
logger.success("上传日记到 pb 成功")
|
2022-11-21 04:23:22 +00:00
|
|
|
return result.headers.get("location")
|