diff --git a/main.py b/main.py index ff7cb74..c1b68fd 100644 --- a/main.py +++ b/main.py @@ -326,21 +326,43 @@ class Tools: if not label_name: return "Error: label_name is required." - # Build query parameters + # First, get the label metadata to retrieve the href_bookmarks URI + label_result = self._make_request(f"/bookmarks/labels/{label_name}") + + if "error" in label_result: + return f"Error fetching label '{label_name}': {label_result['error']}" + + # Extract the href_bookmarks URI + href_bookmarks = label_result.get("href_bookmarks") + if not href_bookmarks: + return f"No href_bookmarks found for label '{label_name}'. Label may not exist or have no bookmarks." + + # Build query parameters for the bookmarks request params = { "limit": min(limit, 100), # Cap at 100 to prevent excessive requests "offset": offset } - # Make request to Readeck API - result = self._make_request(f"/bookmarks/labels/{label_name}", params) + # Extract the endpoint path from href_bookmarks (remove the base URL if present) + if href_bookmarks.startswith(self.valves.readeck_url): + endpoint_path = href_bookmarks[len(self.valves.readeck_url):] + if endpoint_path.startswith("/api"): + endpoint_path = endpoint_path[4:] # Remove /api prefix + else: + # Assume it's a relative path + endpoint_path = href_bookmarks + if endpoint_path.startswith("/api"): + endpoint_path = endpoint_path[4:] # Remove /api prefix - if "error" in result: - return f"Error fetching bookmarks for label '{label_name}': {result['error']}" + # Make request to get the actual bookmarks + bookmarks_result = self._make_request(endpoint_path, params) + + if "error" in bookmarks_result: + return f"Error fetching bookmarks for label '{label_name}': {bookmarks_result['error']}" # Format the response - bookmarks = result.get("bookmarks", []) - total = result.get("total", len(bookmarks)) + bookmarks = bookmarks_result.get("bookmarks", []) + total = bookmarks_result.get("total", len(bookmarks)) if not bookmarks: return f"No bookmarks found with label '{label_name}'." @@ -363,8 +385,13 @@ class Tools: return json.dumps({ "label": label_name, + "label_info": { + "name": label_result.get("name"), + "bookmark_count": label_result.get("bookmark_count"), + "href_bookmarks": href_bookmarks + }, "bookmarks": formatted_bookmarks, "total": total, "limit": params["limit"], "offset": params["offset"] - }, indent=2) + }, indent=2) \ No newline at end of file