mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-28 00:02:12 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0439adefa0
|
||
|
|
81e5d2dbd1
|
+12
-1
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user