If anyone is interested in a Google Books API custom action. Please feel free to use the code.
This will return:
Title, Author and Description of any book. All you need to do is enter the title.
You just to need to create an Environment Variable with your API Key.
import os
import requests
def google_books_api(book_name: str):
"""
This will return the book title and description of the book from the Google Books API. Inform the user with a message "Searching Google Books..."
Args:
book_name (string): The name of the book that should be used when sending a request to Google Book API
Envs:
API_KEY_FOR_GOOGLE_BOOKS (string): API Key for retrieve book information from Google Books
"""
api_key = os.environ['API_KEY_FOR_GOOGLE_BOOKS']
base_url = "https://www.googleapis.com/books/v1/volumes"
# Prepare the request
params = {
'q': book_name,
'key': api_key
}
try:
# Make the API request
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
if not data.get('items'):
print(f"No books found for title: {book_name}")
return None
# Get the first book's info
book_info = data['items'][0]['volumeInfo']
# Get title, author, and description
title = book_info.get('title', 'Title not found')
authors = book_info.get('authors', ['Author not found'])[0] # Get first author
description = book_info.get('description', 'Description not available')
# Print formatted result
print("\nBook Details:")
print(f"Title: {title}")
print(f"Author: {authors}")
print(f"Description: {description}")
except requests.exceptions.RequestException as e:
print(f"Error making request: {e}")
return None
except KeyError as e:
print(f"Error parsing book data: {e}")
return None
except Exception as e:
print(f"Unexpected error: {e}")
return None