Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,15 +40,14 @@ def search_audiobooks(topic: str, limit: int = 3) -> list[dict[str, any]]:
|
|
| 40 |
List[Dict[str, Any]]: List of audiobook metadata dictionaries.
|
| 41 |
"""
|
| 42 |
audiobooks: list[dict[str, any]] = []
|
| 43 |
-
queries: list[str] = [f"{topic}
|
| 44 |
|
| 45 |
for query in queries:
|
| 46 |
try:
|
| 47 |
request = books_service.volumes().list(
|
| 48 |
q=query,
|
| 49 |
maxResults=limit,
|
| 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
|
|
@@ -56,8 +55,12 @@ def search_audiobooks(topic: str, limit: int = 3) -> list[dict[str, any]]:
|
|
| 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 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
audiobooks.append({
|
| 62 |
"title": volume_info.get("title", "Unknown"),
|
| 63 |
"author": ", ".join(volume_info.get("authors", ["Unknown"])),
|
|
@@ -66,6 +69,7 @@ def search_audiobooks(topic: str, limit: int = 3) -> list[dict[str, any]]:
|
|
| 66 |
"estimated_minutes": volume_info.get("pageCount", 180) # Estimate: 1 page ≈ 1 minute
|
| 67 |
})
|
| 68 |
if audiobooks:
|
|
|
|
| 69 |
break # Exit if results found
|
| 70 |
except HttpError as e:
|
| 71 |
print(f"Google Books API error for query '{query}': {e}")
|
|
@@ -75,7 +79,7 @@ def search_audiobooks(topic: str, limit: int = 3) -> list[dict[str, any]]:
|
|
| 75 |
continue
|
| 76 |
|
| 77 |
if not audiobooks:
|
| 78 |
-
print(f"No audiobooks found for topic: {topic}")
|
| 79 |
return audiobooks[:limit] # Ensure no more than limit results
|
| 80 |
|
| 81 |
|
|
|
|
| 40 |
List[Dict[str, Any]]: List of audiobook metadata dictionaries.
|
| 41 |
"""
|
| 42 |
audiobooks: list[dict[str, any]] = []
|
| 43 |
+
queries: list[str] = [f"audiobook {topic}", f"{topic}", f"subject:{topic} audiobook"] # Broader queries
|
| 44 |
|
| 45 |
for query in queries:
|
| 46 |
try:
|
| 47 |
request = books_service.volumes().list(
|
| 48 |
q=query,
|
| 49 |
maxResults=limit,
|
| 50 |
+
printType="books"
|
|
|
|
| 51 |
)
|
| 52 |
response: dict[str, any] = request.execute()
|
| 53 |
print(f"API response for query '{query}': {response.get('items', [])}") # Debug output
|
|
|
|
| 55 |
for item in response.get("items", []):
|
| 56 |
volume_info: dict[str, any] = item.get("volumeInfo", {})
|
| 57 |
categories: list[str] = volume_info.get("categories", [])
|
| 58 |
+
title: str = volume_info.get("title", "").lower()
|
| 59 |
+
description: str = volume_info.get("description", "").lower()
|
| 60 |
+
# Check if audiobook (based on categories, title, or description)
|
| 61 |
+
if ("audiobook" in title or
|
| 62 |
+
"audiobook" in description or
|
| 63 |
+
any("audiobook" in cat.lower() for cat in categories)):
|
| 64 |
audiobooks.append({
|
| 65 |
"title": volume_info.get("title", "Unknown"),
|
| 66 |
"author": ", ".join(volume_info.get("authors", ["Unknown"])),
|
|
|
|
| 69 |
"estimated_minutes": volume_info.get("pageCount", 180) # Estimate: 1 page ≈ 1 minute
|
| 70 |
})
|
| 71 |
if audiobooks:
|
| 72 |
+
print(f"Found {len(audiobooks)} audiobooks for query '{query}'") # Debug output
|
| 73 |
break # Exit if results found
|
| 74 |
except HttpError as e:
|
| 75 |
print(f"Google Books API error for query '{query}': {e}")
|
|
|
|
| 79 |
continue
|
| 80 |
|
| 81 |
if not audiobooks:
|
| 82 |
+
print(f"No audiobooks found for topic: {topic} across all queries")
|
| 83 |
return audiobooks[:limit] # Ensure no more than limit results
|
| 84 |
|
| 85 |
|