RafaelAgreda commited on
Commit
f84e6bc
·
verified ·
1 Parent(s): b6a3811

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
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)-> list[dict[str, any]]: #it's import to specify the return type
31
- #Keep this format for the description / args / args description but feel free to modify the tool
32
- """The tool is designed to connect to the google API platform and retrieve some audiobooks recommendations
 
33
  Args:
34
- topic: the topic that I would like to use to retieve some audiobooks
35
- limit: it is a constant as I want to recommend only three audiobooks
 
 
 
36
  """
37
- request = books_service.volumes().list(
38
- q=f"subject:{topic}+audiobook",
39
- maxResults=limit,
40
- printType="books",
41
- gl="us"
42
- )
43
- response = request.execute()
44
- audiobooks = []
45
- for item in response.get("items", []):
46
- volume_info = item.get("volumeInfo", {})
47
- # Check if audiobook (based on categories or accessInfo)
48
- if "Audiobook" in volume_info.get("categories", []) or volume_info.get("accessInfo", {}).get("epub", {}).get("isAvailable", False):
49
- audiobooks.append({
50
- "title": volume_info.get("title", "Unknown"),
51
- "author": ", ".join(volume_info.get("authors", ["Unknown"])),
52
- "categories": ", ".join(volume_info.get("categories", ["Unknown"])),
53
- "url": volume_info.get("canonicalVolumeLink", "#"),
54
- "estimated_minutes": volume_info.get("pageCount", 180) # Estimate: 1 page ≈ 1 minute
55
- })
56
- return audiobooks
 
 
 
 
 
 
 
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