Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,33 +27,44 @@ books_service = build("books", "v1", credentials=credentials)
|
|
| 27 |
|
| 28 |
# I want to try connect to a google API to get some recommendation of audiobooks!
|
| 29 |
@tool
|
| 30 |
-
def search_audiobooks(topic: str, limit: int = 3)->
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
Args:
|
| 34 |
-
topic:
|
| 35 |
-
limit:
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
@tool
|
| 59 |
# Function to estimate if audiobook fits time constraint
|
|
|
|
| 27 |
|
| 28 |
# I want to try connect to a google API to get some recommendation of audiobooks!
|
| 29 |
@tool
|
| 30 |
+
def search_audiobooks(topic: str, limit: int = 3) -> List[Dict[str, Any]]:
|
| 31 |
+
"""
|
| 32 |
+
Search Google Play Books for audiobooks by topic.
|
| 33 |
+
|
| 34 |
Args:
|
| 35 |
+
topic (str): Genre or topic to search (e.g., "epic fantasy").
|
| 36 |
+
limit (int): Maximum number of results to return (default: 3).
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
List[Dict[str, Any]]: List of audiobook metadata dictionaries.
|
| 40 |
"""
|
| 41 |
+
try:
|
| 42 |
+
request = books_service.volumes().list(
|
| 43 |
+
q=f"subject:{topic}+audiobook",
|
| 44 |
+
maxResults=limit,
|
| 45 |
+
printType="books",
|
| 46 |
+
country="US" # Optional: Restrict to US market
|
| 47 |
+
)
|
| 48 |
+
response: Dict[str, Any] = request.execute()
|
| 49 |
+
audiobooks: List[Dict[str, Any]] = []
|
| 50 |
+
for item in response.get("items", []):
|
| 51 |
+
volume_info: Dict[str, Any] = item.get("volumeInfo", {})
|
| 52 |
+
# Check if audiobook (based on categories or accessInfo)
|
| 53 |
+
if "Audiobook" in volume_info.get("categories", []) or volume_info.get("accessInfo", {}).get("epub", {}).get("isAvailable", False):
|
| 54 |
+
audiobooks.append({
|
| 55 |
+
"title": volume_info.get("title", "Unknown"),
|
| 56 |
+
"author": ", ".join(volume_info.get("authors", ["Unknown"])),
|
| 57 |
+
"categories": ", ".join(volume_info.get("categories", ["Unknown"])),
|
| 58 |
+
"url": volume_info.get("canonicalVolumeLink", "#"),
|
| 59 |
+
"estimated_minutes": volume_info.get("pageCount", 180) # Estimate: 1 page ≈ 1 minute
|
| 60 |
+
})
|
| 61 |
+
return audiobooks
|
| 62 |
+
except HttpError as e:
|
| 63 |
+
print(f"Google Books API error: {e}")
|
| 64 |
+
return []
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Unexpected error in search_audiobooks: {e}")
|
| 67 |
+
return []
|
| 68 |
|
| 69 |
@tool
|
| 70 |
# Function to estimate if audiobook fits time constraint
|