feat(lolz): return last page from get_thread_posts and make post_id optional
This commit is contained in:
+13
-5
@@ -11,7 +11,7 @@ class Lolz:
|
|||||||
def __init__(self, token: str):
|
def __init__(self, token: str):
|
||||||
try:
|
try:
|
||||||
self.client = Forum(token=token, timeout=15)
|
self.client = Forum(token=token, timeout=15)
|
||||||
self.client.settings.logger.enable()
|
# self.client.settings.logger.enable()
|
||||||
|
|
||||||
except:
|
except:
|
||||||
raise
|
raise
|
||||||
@@ -22,9 +22,15 @@ class Lolz:
|
|||||||
|
|
||||||
async def get_thread_posts(
|
async def get_thread_posts(
|
||||||
self, thread_id: Union[str, int], start_page: int = 1
|
self, thread_id: Union[str, int], start_page: int = 1
|
||||||
) -> List[Dict[str, Any]]:
|
) -> tuple[List[Dict[str, Any]], int]:
|
||||||
|
"""Fetch all pages starting from start_page.
|
||||||
|
|
||||||
|
Returns (posts, last_page) so the caller can resume from last_page
|
||||||
|
on the next cycle instead of re-fetching from page 1 every time.
|
||||||
|
"""
|
||||||
all_posts = []
|
all_posts = []
|
||||||
page = start_page
|
page = start_page
|
||||||
|
last_page = start_page
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
response = await self.client.posts.list(thread_id=thread_id, page=page)
|
response = await self.client.posts.list(thread_id=thread_id, page=page)
|
||||||
@@ -34,6 +40,7 @@ class Lolz:
|
|||||||
break
|
break
|
||||||
|
|
||||||
all_posts.extend(posts)
|
all_posts.extend(posts)
|
||||||
|
last_page = page
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Получено {len(posts)} постов из темы {thread_id} на странице {page}."
|
f"Получено {len(posts)} постов из темы {thread_id} на странице {page}."
|
||||||
)
|
)
|
||||||
@@ -46,12 +53,13 @@ class Lolz:
|
|||||||
else:
|
else:
|
||||||
logger.info(f"Постов в теме {thread_id} не найдено.")
|
logger.info(f"Постов в теме {thread_id} не найдено.")
|
||||||
|
|
||||||
return all_posts
|
return all_posts, last_page
|
||||||
|
|
||||||
async def get_all_thread_posts(
|
async def get_all_thread_posts(
|
||||||
self, thread_id: Union[str, int]
|
self, thread_id: Union[str, int]
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
return await self.get_thread_posts(thread_id=thread_id, start_page=1)
|
posts, _ = await self.get_thread_posts(thread_id=thread_id, start_page=1)
|
||||||
|
return posts
|
||||||
|
|
||||||
async def get_post_comments(self, post_id: int) -> List[Dict[str, Any]]:
|
async def get_post_comments(self, post_id: int) -> List[Dict[str, Any]]:
|
||||||
response = await self.client.posts.comments.list(post_id=post_id)
|
response = await self.client.posts.comments.list(post_id=post_id)
|
||||||
@@ -60,7 +68,7 @@ class Lolz:
|
|||||||
return comments
|
return comments
|
||||||
|
|
||||||
async def has_comments(
|
async def has_comments(
|
||||||
self, post_id: Optional[int], post: Dict[str, Any] = None
|
self, post_id: Optional[int] = None, post: Dict[str, Any] = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if not post:
|
if not post:
|
||||||
if not post_id:
|
if not post_id:
|
||||||
|
|||||||
Reference in New Issue
Block a user