Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,7 @@ books_service = build("books", "v1", credentials=credentials)
|
|
| 28 |
|
| 29 |
# I want to try connect to a google API to get some recommendation of audiobooks!
|
| 30 |
@tool
|
| 31 |
-
def search_audiobooks(topic: str, limit: int = 3) ->
|
| 32 |
"""
|
| 33 |
Search Google Play Books for audiobooks by topic.
|
| 34 |
|
|
@@ -39,8 +39,8 @@ def search_audiobooks(topic: str, limit: int = 3) -> List[Dict[str, Any]]:
|
|
| 39 |
Returns:
|
| 40 |
List[Dict[str, Any]]: List of audiobook metadata dictionaries.
|
| 41 |
"""
|
| 42 |
-
audiobooks:
|
| 43 |
-
queries:
|
| 44 |
|
| 45 |
for query in queries:
|
| 46 |
try:
|
|
@@ -50,12 +50,12 @@ def search_audiobooks(topic: str, limit: int = 3) -> List[Dict[str, Any]]:
|
|
| 50 |
printType="books",
|
| 51 |
country="US" # Optional: Restrict to US market
|
| 52 |
)
|
| 53 |
-
response:
|
| 54 |
print(f"API response for query '{query}': {response.get('items', [])}") # Debug output
|
| 55 |
|
| 56 |
for item in response.get("items", []):
|
| 57 |
-
volume_info:
|
| 58 |
-
categories:
|
| 59 |
# Check if audiobook (based on categories or title/description)
|
| 60 |
if any("audiobook" in cat.lower() for cat in categories) or "audiobook" in volume_info.get("title", "").lower():
|
| 61 |
audiobooks.append({
|
|
|
|
| 28 |
|
| 29 |
# I want to try connect to a google API to get some recommendation of audiobooks!
|
| 30 |
@tool
|
| 31 |
+
def search_audiobooks(topic: str, limit: int = 3) -> list[dict[str, any]]:
|
| 32 |
"""
|
| 33 |
Search Google Play Books for audiobooks by topic.
|
| 34 |
|
|
|
|
| 39 |
Returns:
|
| 40 |
List[Dict[str, Any]]: List of audiobook metadata dictionaries.
|
| 41 |
"""
|
| 42 |
+
audiobooks: list[dict[str, any]] = []
|
| 43 |
+
queries: list[str] = [f"{topic}+audiobook", f"subject:{topic}+audiobook"] # Try broader query first
|
| 44 |
|
| 45 |
for query in queries:
|
| 46 |
try:
|
|
|
|
| 50 |
printType="books",
|
| 51 |
country="US" # Optional: Restrict to US market
|
| 52 |
)
|
| 53 |
+
response: dict[str, any] = request.execute()
|
| 54 |
print(f"API response for query '{query}': {response.get('items', [])}") # Debug output
|
| 55 |
|
| 56 |
for item in response.get("items", []):
|
| 57 |
+
volume_info: dict[str, any] = item.get("volumeInfo", {})
|
| 58 |
+
categories: list[str] = volume_info.get("categories", [])
|
| 59 |
# Check if audiobook (based on categories or title/description)
|
| 60 |
if any("audiobook" in cat.lower() for cat in categories) or "audiobook" in volume_info.get("title", "").lower():
|
| 61 |
audiobooks.append({
|