bookManagement/bookHelpers.js
knight f4f227b24d Implement Google Books API integration for book updates and enhance library management features
- Added a new endpoint in `index.js` to update book data using the Google Books API, allowing for real-time updates based on ISBN.
- Introduced a `rebuild_book_entries` function in `libraryManager.py` to facilitate user-driven updates of book entries from the Google Books API.
- Enhanced error handling and user prompts for better interaction during book updates.
- Updated `public/index.html` and `public/script.js` to improve the user interface and support new functionalities.
- Modified styles in `public/styles.css` to enhance the layout and usability of the checkout button and location prompt.
2024-12-12 10:32:20 -05:00

97 lines
3.4 KiB
JavaScript

const { Book, Location, Checkout, User, Sequelize, Op } = require('./models');
const axios = require('axios');
// Fetch book from the local database by ISBN
const fetchBookFromLocalDatabase = async (isbn) => {
return await Book.findOne({ where: { isbn } });
};
// Fetch book from Google Books API
const fetchBookFromGoogleBooks = async (isbn, apiKey) => {
try {
const response = await axios.get(`https://www.googleapis.com/books/v1/volumes?q=${isbn}&key=${apiKey}`);
if (response.data.items && response.data.items.length > 0) {
return library.formatGoogleBooksData(response.data.items[0]);
}
return null;
} catch (error) {
if (error.response && (error.response.status === 429 || error.response.status === 408)) {
throw error.response.status;
}
throw new Error('Google Books API Error');
}
};
// Fetch book from Open Library API
const fetchBookFromOpenLibrary = async (isbn) => {
try {
const response = await axios.get(`https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=data`);
return response.data[`ISBN:${isbn}`] ? library.formatOpenLibraryData(response.data[`ISBN:${isbn}`]) : null;
} catch (error) {
if (error.response && (error.response.status === 429 || error.response.status === 408)) {
throw error.response.status;
}
throw new Error('Open Library API Error');
}
};
// Fetch book from the Internet Archive
const fetchBookFromInternetArchive = async (isbn) => {
try {
const response = await axios.get(`https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=data`);
return response.data[`ISBN:${isbn}`] ? library.formatArchiveData(response.data[`ISBN:${isbn}`]) : null;
} catch (error) {
throw new Error('Internet Archive API Error');
}
};
// Search books in the local database by title or related fields
const searchBooksInLocalDatabase = async (title, searchDescription=false) => {
if (!title) {
// Return all books if the title is empty
return await Book.findAll();
}
const searchConditions = [
{ title: { [Op.like]: `%${title}%` } },
{ authors: { [Op.like]: `%${title}%` } },
{ publishers: { [Op.like]: `%${title}%` } },
{ subjects: { [Op.like]: `%${title}%` } }
];
if (searchDescription) {
searchConditions.push({ description: { [Op.like]: `%${title}%` } });
}
return await Book.findAll({
where: {
[Op.or]: searchConditions
}
});
};
// Search books in Open Library by title
const searchBooksInOpenLibrary = async (title) => {
try {
const response = await axios.get(`https://openlibrary.org/search.json?q=${encodeURIComponent(title)}&limit=7`);
return response.data.docs.map(result => ({
title: result.title,
authors: result.author_name || [],
publish_date: result.first_publish_year,
isbn: result.isbn ? result.isbn[0] : '',
publisher: result.publisher ? result.publisher[0] : '',
key: result.key
}));
} catch (error) {
throw new Error('Open Library API Error');
}
};
module.exports = {
fetchBookFromLocalDatabase,
fetchBookFromGoogleBooks,
fetchBookFromOpenLibrary,
fetchBookFromInternetArchive,
searchBooksInLocalDatabase,
searchBooksInOpenLibrary
};