Skip to content

Commit

Permalink
✨ Support min_id for WishPaginator
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao authored Dec 1, 2024
1 parent d7756ad commit 97053ad
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S"
[tool.black]
include = '\.pyi?$'
line-length = 120
target-version = ['py39', 'py310', 'py311', 'py312', 'py313']
target-version = ['py39', 'py310', 'py311', 'py312']
3 changes: 3 additions & 0 deletions simnet/client/components/self_help/starrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async def get_starrail_action_log(
authkey: str,
limit: Optional[int] = None,
end_id: int = 0,
min_id: int = 0,
*,
lang: Optional[str] = None,
) -> List[StarRailSelfHelpActionLog]:
Expand All @@ -25,13 +26,15 @@ async def get_starrail_action_log(
authkey: The authkey for the user.
limit: The number of logs to get.
end_id: The end ID for the logs.
min_id: The minimum ID for the logs.
lang: The language to get the logs in.
Returns:
List[StarRailSelfHelpActionLog]: The action logs.
"""
paginator = WishPaginator(
end_id,
min_id,
partial(
self.request_self_help,
endpoint="UserInfo/GetActionLog",
Expand Down
3 changes: 3 additions & 0 deletions simnet/client/components/self_help/zzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async def get_zzz_action_log(
authkey: str,
limit: Optional[int] = None,
end_id: int = 0,
min_id: int = 0,
*,
lang: Optional[str] = None,
) -> List[ZZZSelfHelpActionLog]:
Expand All @@ -25,13 +26,15 @@ async def get_zzz_action_log(
authkey: The authkey for the user.
limit: The number of logs to get.
end_id: The end ID for the logs.
min_id: The minimum ID for the logs.
lang: The language to get the logs in.
Returns:
List[ZZZSelfHelpActionLog]: The action logs.
"""
paginator = WishPaginator(
end_id,
min_id,
partial(
self.request_self_help,
endpoint="LoginRecord/GetList",
Expand Down
3 changes: 3 additions & 0 deletions simnet/client/components/wish/genshin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async def wish_history(
lang: Optional[str] = None,
authkey: Optional[str] = None,
end_id: int = 0,
min_id: int = 0,
banner_default_name: Optional[str] = "",
) -> List[Wish]:
"""Get the wish history for a list of banner types.
Expand All @@ -31,6 +32,7 @@ async def wish_history(
If not provided, the class default will be used.
authkey (Optional[str], optional): The authorization key for making the request.
end_id (int, optional): The ending ID of the last wish to retrieve.
min_id (int, optional): The minimum ID of the first wish to retrieve.
banner_default_name (Optional[str], optional): The default name of the banner to use.
Returns:
Expand All @@ -44,6 +46,7 @@ async def wish_history(
for banner_type in banner_types:
paginator = WishPaginator(
end_id,
min_id,
partial(
self.get_wish_page,
banner_type=banner_type,
Expand Down
3 changes: 3 additions & 0 deletions simnet/client/components/wish/starrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async def wish_history(
lang: Optional[str] = None,
authkey: Optional[str] = None,
end_id: int = 0,
min_id: int = 0,
) -> List[StarRailWish]:
"""
Get the wish history for a list of banner types.
Expand All @@ -31,6 +32,7 @@ async def wish_history(
If not provided, the class default will be used.
authkey (Optional[str], optional): The authorization key for making the request.
end_id (int, optional): The ending ID of the last wish to retrieve.
min_id (int, optional): The minimum ID of the first wish to retrieve
Returns:
List[StarRailWish]: A list of StarRailWish objects representing the retrieved wishes.
Expand All @@ -42,6 +44,7 @@ async def wish_history(
for banner_type in banner_types:
paginator = WishPaginator(
end_id,
min_id,
partial(
self.get_wish_page,
banner_type=banner_type,
Expand Down
3 changes: 3 additions & 0 deletions simnet/client/components/wish/zzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async def wish_history(
lang: Optional[str] = None,
authkey: Optional[str] = None,
end_id: int = 0,
min_id: int = 0,
) -> List[ZZZWish]:
"""
Get the wish history for a list of banner types.
Expand All @@ -31,6 +32,7 @@ async def wish_history(
If not provided, the class default will be used.
authkey (Optional[str], optional): The authorization key for making the request.
end_id (int, optional): The ending ID of the last wish to retrieve.
min_id (int, optional): The minimum ID of the first wish to retrieve
Returns:
List[ZZZWish]: A list of ZZZWish objects representing the retrieved wishes.
Expand All @@ -42,6 +44,7 @@ async def wish_history(
for banner_type in banner_types:
paginator = WishPaginator(
end_id,
min_id,
partial(
self.get_wish_page,
banner_type=banner_type,
Expand Down
21 changes: 16 additions & 5 deletions simnet/utils/paginator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextlib
from typing import List, Dict, Callable, Any, Awaitable


Expand All @@ -14,9 +15,11 @@ class WishPaginator:
def __init__(
self,
end_id: int,
min_id: int,
fetch_data: Callable[..., Awaitable[Dict[str, Any]]],
):
self.end_id = end_id
self.min_id = min_id
self.fetch_data = fetch_data

async def get(self, limit: int) -> List[Dict]:
Expand All @@ -40,13 +43,21 @@ async def get(self, limit: int) -> List[Dict]:

current_end_id = items[-1]["id"]

filtered_items = [item for item in items if item["id"] != self.end_id]
if len(filtered_items) < len(items):
all_items.extend(filtered_items)
break

filtered_items, need_break = [], False
for item in items:
if item["id"] == self.end_id:
need_break = True
continue
if self.min_id:
with contextlib.suppress(ValueError):
if int(item["id"]) <= self.min_id:
need_break = True
continue
filtered_items.append(item)
all_items.extend(filtered_items)

if need_break:
break
if limit and len(all_items) >= limit:
break

Expand Down

0 comments on commit 97053ad

Please sign in to comment.