Update app.py
Browse files
app.py
CHANGED
@@ -18,10 +18,13 @@ import uuid
|
|
18 |
# Google Books API setup with Service Account
|
19 |
json_key_path: str = "/home/user/app/audiobookagent-aaf910cd6329.json"
|
20 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = json_key_path
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
books_service = build("books", "v1", credentials=credentials)
|
26 |
|
27 |
|
@@ -40,7 +43,7 @@ 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"
|
44 |
|
45 |
for query in queries:
|
46 |
try:
|
@@ -50,37 +53,43 @@ def search_audiobooks(topic: str, limit: int = 3) -> list[dict[str, any]]:
|
|
50 |
printType="books"
|
51 |
)
|
52 |
response: dict[str, any] = request.execute()
|
53 |
-
|
|
|
54 |
|
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", "")
|
59 |
-
description: str = volume_info.get("description", "")
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
71 |
if audiobooks:
|
72 |
-
|
73 |
-
|
|
|
74 |
except HttpError as e:
|
75 |
-
|
|
|
76 |
continue
|
77 |
except Exception as e:
|
78 |
-
|
|
|
79 |
continue
|
80 |
|
81 |
if not audiobooks:
|
82 |
-
|
83 |
-
|
|
|
84 |
|
85 |
|
86 |
@tool
|
|
|
18 |
# Google Books API setup with Service Account
|
19 |
json_key_path: str = "/home/user/app/audiobookagent-aaf910cd6329.json"
|
20 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = json_key_path
|
21 |
+
try:
|
22 |
+
credentials = service_account.Credentials.from_service_account_file(
|
23 |
+
json_key_path,
|
24 |
+
scopes=["https://www.googleapis.com/auth/books"]
|
25 |
+
)
|
26 |
+
except FileNotFoundError:
|
27 |
+
raise FileNotFoundError(f"JSON key file not found at {json_key_path}. Please upload it to /app/.")
|
28 |
books_service = build("books", "v1", credentials=credentials)
|
29 |
|
30 |
|
|
|
43 |
List[Dict[str, Any]]: List of audiobook metadata dictionaries.
|
44 |
"""
|
45 |
audiobooks: list[dict[str, any]] = []
|
46 |
+
queries: list[str] = [f"{topic}", f"audiobook {topic}", "fiction audiobook"] # Broad queries
|
47 |
|
48 |
for query in queries:
|
49 |
try:
|
|
|
53 |
printType="books"
|
54 |
)
|
55 |
response: dict[str, any] = request.execute()
|
56 |
+
with open("debug_log.txt", "a") as f:
|
57 |
+
f.write(f"API response for query '{query}': {json.dumps(response.get('items', []), indent=2)}\n")
|
58 |
|
59 |
for item in response.get("items", []):
|
60 |
volume_info: dict[str, any] = item.get("volumeInfo", {})
|
61 |
categories: list[str] = volume_info.get("categories", [])
|
62 |
+
title: str = volume_info.get("title", "")
|
63 |
+
description: str = volume_info.get("description", "")
|
64 |
+
with open("debug_log.txt", "a") as f:
|
65 |
+
f.write(f"Item: title='{title}', categories={categories}, description='{description[:100]}...'\n")
|
66 |
+
|
67 |
+
# No filtering: return all results for debugging
|
68 |
+
audiobooks.append({
|
69 |
+
"title": title or "Unknown",
|
70 |
+
"author": ", ".join(volume_info.get("authors", ["Unknown"])),
|
71 |
+
"categories": ", ".join(categories or ["Unknown"]),
|
72 |
+
"url": volume_info.get("canonicalVolumeLink", "#"),
|
73 |
+
"estimated_minutes": volume_info.get("pageCount", 180)
|
74 |
+
})
|
75 |
+
|
76 |
if audiobooks:
|
77 |
+
with open("debug_log.txt", "a") as f:
|
78 |
+
f.write(f"Found {len(audiobooks)} audiobooks for query '{query}'\n")
|
79 |
+
break
|
80 |
except HttpError as e:
|
81 |
+
with open("debug_log.txt", "a") as f:
|
82 |
+
f.write(f"Google Books API error for query '{query}': {e}\n")
|
83 |
continue
|
84 |
except Exception as e:
|
85 |
+
with open("debug_log.txt", "a") as f:
|
86 |
+
f.write(f"Unexpected error in search_audiobooks for query '{query}': {e}\n")
|
87 |
continue
|
88 |
|
89 |
if not audiobooks:
|
90 |
+
with open("debug_log.txt", "a") as f:
|
91 |
+
f.write(f"No audiobooks found for topic: {topic} across all queries\n")
|
92 |
+
return audiobooks[:limit]
|
93 |
|
94 |
|
95 |
@tool
|