From 424d7795bcda29b0bfbdaed61b87e60dfbf93362 Mon Sep 17 00:00:00 2001 From: C <0xicl33n@gmail.com> Date: Sun, 4 Jan 2026 11:44:30 -0800 Subject: [PATCH] pagination fix --- src/asquare.py | 86 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/src/asquare.py b/src/asquare.py index cdaaaca..1b74efd 100644 --- a/src/asquare.py +++ b/src/asquare.py @@ -125,42 +125,66 @@ async def get_item_by_variation_id(variation_id: str) -> dict: # Search LEGO category items to find which one has this variation print(f"[@DEBUG] Searching for variation {variation_id} in LEGO category") - result = await client.catalog.search( - object_types=["ITEM"], - query={ - "exact_query": { - "attribute_name": "category_id", - "attribute_value": LEGO_CAT_ID, - } - }, - limit=1000, - ) - result_json = json.loads(result.model_dump_json()) + cursor = None + page_count = 0 - if not result_json.get("objects"): - raise ValueError(f"No items found in LEGO category") + while True: + page_count += 1 + print(f"[@DEBUG] Searching page {page_count}...") - # Search through items to find which has this variation - for obj in result_json["objects"]: - item_data = obj.get("item_data", {}) - variations = item_data.get("variations", []) - - for variation in variations: - if variation.get("id") == variation_id: - # Found the parent item! - print(f"[@DEBUG] Found parent item: {obj['id']}") - return { - "id": obj["id"], - "name": item_data.get("name", ""), - "description": item_data.get("description", ""), - "category_id": item_data.get("category_id"), - "variations": variations, - "updated_at": obj.get("updated_at"), - "version": obj.get("version"), + search_params = { + "object_types": ["ITEM"], + "query": { + "exact_query": { + "attribute_name": "category_id", + "attribute_value": LEGO_CAT_ID, } + }, + "limit": 1000, + } + + if cursor: + search_params["cursor"] = cursor + + result = await client.catalog.search(**search_params) + result_json = json.loads(result.model_dump_json()) + + if not result_json.get("objects"): + if page_count == 1: + raise ValueError(f"No items found in LEGO category") + else: + # No more results + break + + # Search through items on this page to find which has this variation + for obj in result_json["objects"]: + item_data = obj.get("item_data", {}) + variations = item_data.get("variations", []) + + for variation in variations: + if variation.get("id") == variation_id: + # Found the parent item! + print( + f"[@DEBUG] Found parent item: {obj['id']} on page {page_count}" + ) + return { + "id": obj["id"], + "name": item_data.get("name", ""), + "description": item_data.get("description", ""), + "category_id": item_data.get("category_id"), + "variations": variations, + "updated_at": obj.get("updated_at"), + "version": obj.get("version"), + } + + # Check if there are more pages + cursor = result_json.get("cursor") + if not cursor: + # No more pages + break raise ValueError( - f"Variation {variation_id} not found in any LEGO category item" + f"Variation {variation_id} not found in any LEGO category item (searched {page_count} pages)" ) except Exception as e: