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
+12 -1
View File
@@ -5,6 +5,7 @@ from typing import Generator, Literal, TYPE_CHECKING
from uuid import uuid4 from uuid import uuid4
from async_pixiv import PixivClient from async_pixiv import PixivClient
from async_pixiv.error import ApiError
from telegram import InlineQueryResultPhoto, InputMediaPhoto from telegram import InlineQueryResultPhoto, InputMediaPhoto
from utils.logger import get_logger from utils.logger import get_logger
@@ -68,7 +69,7 @@ class Pixiv:
@property @property
def author_url(self) -> str: def author_url(self) -> str:
return str(self._illust.user.link) return str(self._illust.user.link).rstrip('/')
@property @property
def description(self) -> str: def description(self) -> str:
@@ -118,11 +119,16 @@ class ProcessPixiv:
async def init_client(cls, token: str) -> None: async def init_client(cls, token: str) -> None:
cls._client = PixivClient() cls._client = PixivClient()
await cls._client.login_with_token(token) await cls._client.login_with_token(token)
cls._token = token
@classmethod @classmethod
async def close_client(cls) -> None: async def close_client(cls) -> None:
await cls._client.close() await cls._client.close()
@classmethod
async def refresh_token(cls):
await cls._client.login_with_token(cls._token)
def __init__(self, url: str): def __init__(self, url: str):
self._url: str = url self._url: str = url
@@ -135,7 +141,11 @@ class ProcessPixiv:
async def _fetch_illust(self) -> Illust: async def _fetch_illust(self) -> Illust:
illust_id = self._parse_illust_id() illust_id = self._parse_illust_id()
try:
return (await self._client.ILLUST.detail(illust_id)).illust 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: def _parse_illust_id(self) -> int:
return int(self._url.split("/")[-1]) return int(self._url.split("/")[-1])
@@ -192,6 +202,7 @@ class TelegramPixiv:
def message_media_generator(self) -> Generator[TypeMessageMediaResult, None, None]: def message_media_generator(self) -> Generator[TypeMessageMediaResult, None, None]:
pixiv = self._pixiv pixiv = self._pixiv
for media in pixiv.images: for media in pixiv.images:
logger.info(str(media))
if pixiv.type in ("illust", "manga"): if pixiv.type in ("illust", "manga"):
yield InputMediaPhoto( yield InputMediaPhoto(
media=media.url, media=media.url,