Compare commits

..
2 Commits
Author SHA1 Message Date
YoursFunny 0439adefa0 add refresh token if expire 2024-06-24 15:57:30 +08:00
YoursFunny 81e5d2dbd1 add log 2024-06-24 15:47:12 +08:00
+13 -2
View File
@@ -5,6 +5,7 @@ from typing import Generator, Literal, TYPE_CHECKING
from uuid import uuid4
from async_pixiv import PixivClient
from async_pixiv.error import ApiError
from telegram import InlineQueryResultPhoto, InputMediaPhoto
from utils.logger import get_logger
@@ -68,7 +69,7 @@ class Pixiv:
@property
def author_url(self) -> str:
return str(self._illust.user.link)
return str(self._illust.user.link).rstrip('/')
@property
def description(self) -> str:
@@ -118,11 +119,16 @@ class ProcessPixiv:
async def init_client(cls, token: str) -> None:
cls._client = PixivClient()
await cls._client.login_with_token(token)
cls._token = token
@classmethod
async def close_client(cls) -> None:
await cls._client.close()
@classmethod
async def refresh_token(cls):
await cls._client.login_with_token(cls._token)
def __init__(self, url: str):
self._url: str = url
@@ -135,7 +141,11 @@ class ProcessPixiv:
async def _fetch_illust(self) -> Illust:
illust_id = self._parse_illust_id()
return (await self._client.ILLUST.detail(illust_id)).illust
try:
return (await self._client.ILLUST.detail(illust_id)).illust
except ApiError:
await self.refresh_token()
return (await self._client.ILLUST.detail(illust_id)).illust # TODO use retry here
def _parse_illust_id(self) -> int:
return int(self._url.split("/")[-1])
@@ -192,6 +202,7 @@ class TelegramPixiv:
def message_media_generator(self) -> Generator[TypeMessageMediaResult, None, None]:
pixiv = self._pixiv
for media in pixiv.images:
logger.info(str(media))
if pixiv.type in ("illust", "manga"):
yield InputMediaPhoto(
media=media.url,